What is @graphql-tools/schema?
The @graphql-tools/schema package is a utility library that helps with the creation and manipulation of GraphQL schemas. It provides functions to make it easier to work with GraphQL schema definitions, including merging type definitions, adding resolvers, and more.
What are @graphql-tools/schema's main functionalities?
makeExecutableSchema
This feature allows you to combine type definitions and resolvers to create an executable GraphQL schema.
const { makeExecutableSchema } = require('@graphql-tools/schema');
const typeDefs = `
type Query {
hello: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
const schema = makeExecutableSchema({
typeDefs,
resolvers,
});
addResolversToSchema
This feature enables you to add new resolvers to an existing GraphQL schema.
const { addResolversToSchema } = require('@graphql-tools/schema');
const schema = makeExecutableSchema({ typeDefs });
const resolvers = {
Query: {
hello: () => 'Hello world!',
},
};
addResolversToSchema({ schema, resolvers });
addSchemaLevelResolver
This feature allows you to add a resolver function that runs before any other resolvers for each operation.
const { addSchemaLevelResolver } = require('@graphql-tools/schema');
const schema = makeExecutableSchema({ typeDefs, resolvers });
const rootResolver = (root, args, context, info) => { /*...*/ };
addSchemaLevelResolver(schema, rootResolver);
Other packages similar to @graphql-tools/schema
graphql-tools
This is a more comprehensive package that includes @graphql-tools/schema as a part of its toolkit. It provides additional utilities for mocking and stitching GraphQL schemas.
apollo-server
While not a direct alternative, Apollo Server uses similar concepts and includes the ability to create an executable schema. It is a full-featured GraphQL server that integrates well with @graphql-tools/schema.
merge-graphql-schemas
This package provides functionality to merge GraphQL type definitions and resolvers. It is similar to some of the features in @graphql-tools/schema but is more focused on the merging aspect.