What is @graphql-tools/stitch?
@graphql-tools/stitch is a part of the GraphQL Tools ecosystem that allows you to combine multiple GraphQL schemas into a single unified schema. This is particularly useful for microservices architectures where each service has its own GraphQL schema, and you want to expose a single GraphQL endpoint to clients.
What are @graphql-tools/stitch's main functionalities?
Schema Stitching
This feature allows you to combine multiple GraphQL schemas into a single unified schema. The code sample demonstrates how to stitch together a user schema and a product schema.
const { stitchSchemas } = require('@graphql-tools/stitch');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const userSchema = makeExecutableSchema({
typeDefs: `
type User {
id: ID!
name: String!
}
type Query {
user(id: ID!): User
}
`,
resolvers: {
Query: {
user: () => ({ id: '1', name: 'John Doe' })
}
}
});
const productSchema = makeExecutableSchema({
typeDefs: `
type Product {
id: ID!
name: String!
}
type Query {
product(id: ID!): Product
}
`,
resolvers: {
Query: {
product: () => ({ id: '1', name: 'Laptop' })
}
}
});
const stitchedSchema = stitchSchemas({
subschemas: [
{ schema: userSchema },
{ schema: productSchema }
]
});
console.log(stitchedSchema.getTypeMap());
Schema Delegation
Schema delegation allows you to forward a query from one schema to another. The code sample demonstrates how to delegate a query to the stitched schema to fetch user data.
const { delegateToSchema } = require('@graphql-tools/stitch');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const userSchema = makeExecutableSchema({
typeDefs: `
type User {
id: ID!
name: String!
}
type Query {
user(id: ID!): User
}
`,
resolvers: {
Query: {
user: () => ({ id: '1', name: 'John Doe' })
}
}
});
const productSchema = makeExecutableSchema({
typeDefs: `
type Product {
id: ID!
name: String!
}
type Query {
product(id: ID!): Product
}
`,
resolvers: {
Query: {
product: () => ({ id: '1', name: 'Laptop' })
}
}
});
const stitchedSchema = stitchSchemas({
subschemas: [
{ schema: userSchema },
{ schema: productSchema }
]
});
const result = delegateToSchema({
schema: stitchedSchema,
operation: 'query',
fieldName: 'user',
args: { id: '1' }
});
result.then(data => console.log(data));
Schema Merging
Schema merging allows you to combine multiple schemas into one. The code sample demonstrates how to merge a user schema and a product schema into a single schema.
const { mergeSchemas } = require('@graphql-tools/merge');
const { makeExecutableSchema } = require('@graphql-tools/schema');
const userSchema = makeExecutableSchema({
typeDefs: `
type User {
id: ID!
name: String!
}
type Query {
user(id: ID!): User
}
`,
resolvers: {
Query: {
user: () => ({ id: '1', name: 'John Doe' })
}
}
});
const productSchema = makeExecutableSchema({
typeDefs: `
type Product {
id: ID!
name: String!
}
type Query {
product(id: ID!): Product
}
`,
resolvers: {
Query: {
product: () => ({ id: '1', name: 'Laptop' })
}
}
});
const mergedSchema = mergeSchemas({
schemas: [userSchema, productSchema]
});
console.log(mergedSchema.getTypeMap());
Other packages similar to @graphql-tools/stitch
apollo-server
Apollo Server is a community-maintained open-source GraphQL server that works with any GraphQL schema. It provides a straightforward setup for creating a GraphQL server and supports schema stitching through its federation capabilities. Compared to @graphql-tools/stitch, Apollo Server is more focused on providing a complete server solution with additional features like caching, monitoring, and more.
graphql-yoga
GraphQL Yoga is a fully-featured GraphQL server with focus on easy setup, performance, and great developer experience. It supports schema stitching and merging, similar to @graphql-tools/stitch, but also includes built-in support for subscriptions and other advanced features. GraphQL Yoga is designed to be a more opinionated and feature-rich alternative to setting up a GraphQL server from scratch.
merge-graphql-schemas
merge-graphql-schemas is a utility library for merging GraphQL schemas. It provides a simple API for combining multiple schema files into a single executable schema. While it offers similar functionality to @graphql-tools/stitch in terms of schema merging, it is more lightweight and focused solely on the merging aspect without additional features like schema delegation.