mirror of
https://github.com/brendan-ch/project-inter-server.git
synced 2026-04-17 07:50:31 +00:00
split query resolvers into separate files
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
import { readFileSync } from "fs";
|
||||
import { ApolloServer } from "@apollo/server";
|
||||
import { startStandaloneServer } from "@apollo/server/standalone";
|
||||
import { resolvers } from "./resolvers";
|
||||
import { MergedResolvers } from "./resolvers";
|
||||
import { ServerContext } from "./ServerContext";
|
||||
import { UnoptimizedInMemoryRepository } from "./repositories/UnoptimizedInMemoryRepository";
|
||||
import { TimedApiBasedRepositoryLoader } from "./loaders/TimedApiBasedRepositoryLoader";
|
||||
@@ -11,7 +11,7 @@ const typeDefs = readFileSync("./schema.graphqls", "utf8");
|
||||
async function main() {
|
||||
const server = new ApolloServer<ServerContext>({
|
||||
typeDefs,
|
||||
resolvers,
|
||||
resolvers: MergedResolvers,
|
||||
introspection: process.env.NODE_ENV !== "production",
|
||||
});
|
||||
|
||||
|
||||
@@ -1,29 +1,9 @@
|
||||
import { Coordinates, Eta, OrderedStop, Resolvers } from "./generated/graphql";
|
||||
import { ServerContext } from "./ServerContext";
|
||||
import { QueryResolvers } from "./resolvers/QueryResolvers";
|
||||
|
||||
export const resolvers: Resolvers<ServerContext> = {
|
||||
Query: {
|
||||
systems: async (parent, args, contextValue, info) => {
|
||||
const systems = await contextValue.repository.getSystems();
|
||||
return systems.map(({
|
||||
name,
|
||||
id
|
||||
}) => ({
|
||||
name,
|
||||
id
|
||||
}));
|
||||
},
|
||||
system: async (parent, args, contextValue, info) => {
|
||||
if (!args.id) return null;
|
||||
const system = await contextValue.repository.getSystemById(args.id);
|
||||
if (system === null) return null;
|
||||
|
||||
return {
|
||||
name: system.name,
|
||||
id: system.id,
|
||||
};
|
||||
}
|
||||
},
|
||||
export const MergedResolvers: Resolvers<ServerContext> = {
|
||||
...QueryResolvers,
|
||||
System: {
|
||||
routes: async (parent, args, contextValue, info) => {
|
||||
return await contextValue.repository.getRoutesBySystemId(parent.id);
|
||||
20
src/resolvers/QueryResolvers.ts
Normal file
20
src/resolvers/QueryResolvers.ts
Normal file
@@ -0,0 +1,20 @@
|
||||
import { ServerContext } from "../ServerContext";
|
||||
import { Resolvers } from "../generated/graphql";
|
||||
|
||||
export const QueryResolvers: Resolvers<ServerContext> = {
|
||||
Query: {
|
||||
systems: async (parent, args, contextValue, info) => {
|
||||
return await contextValue.repository.getSystems();
|
||||
},
|
||||
system: async (parent, args, contextValue, info) => {
|
||||
if (!args.id) return null;
|
||||
const system = await contextValue.repository.getSystemById(args.id);
|
||||
if (system === null) return null;
|
||||
|
||||
return {
|
||||
name: system.name,
|
||||
id: system.id,
|
||||
};
|
||||
}
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user