Currently learning GraphQL with Express and Typescript. I am trying to build a Root Schema with Queries and Mutations. I have found a way to do it but I think this isn't the best way to build out a GraphQL schema. Wondering what everyone else has seen in a professional setting as far as building a GraphQL Server.
First, building the fields out like so. The UserType
and AuthType
are GraphQLObjectTypes.
export const authMutationFields = {
login: {
type: AuthType,
args,
resolve: AuthService.login,
},
....
};
export const userMutationFields = {
deleteUser: {
type: UserType,
args: { userId: { type: GraphQLNonNull(GraphQLString) } },
resolve: UserService.deleteUser,
},
};
Then, merging these into the root query and mutation like so:
const mutationFields = () => ({ ...authMutationFields, ...userMutationFields });
const queryFields = () => ({ ...userQueryFields });
const mutation = new GraphQLObjectType({
name: "RootMutation",
description: "Root Mutation",
fields: mutationFields,
});
const query = new GraphQLObjectType({
name: "RootQuery",
description: "Root Query",
fields: queryFields,
});
export const rootSchema = new GraphQLSchema({ query, mutation });
While this works... It certainly doesn't seem like something you'd see in the wild. I could use the buildSchema but that seems like a method that doesn't scale well.
Thanks in advance!