Skip to Content
Schema Stitching
DocsGetting StartedBasic Example

Basic Example

In this example, we’ll stitch together two very simple schemas representing a system of users and posts. You can find more examples and use-cases in the Handbook

import { makeExecutableSchema } from '@graphql-tools/schema' import { stitchSchemas } from '@graphql-tools/stitch' let postsSchema = makeExecutableSchema({ typeDefs: /* GraphQL */ ` type Post { id: ID! text: String userId: ID! } type Query { postById(id: ID!): Post postsByUserId(userId: ID!): [Post]! } `, resolvers: { // ... } }) let usersSchema = makeExecutableSchema({ typeDefs: /* GraphQL */ ` type User { id: ID! email: String } type Query { userById(id: ID!): User } `, resolvers: { // ... } }) // setup subschema configurations export const postsSubschema = { schema: postsSchema } export const usersSubschema = { schema: usersSchema } // build the combined schema export const gatewaySchema = stitchSchemas({ subschemas: [postsSubschema, usersSubschema] })

This process builds two GraphQL schemas, places them each into subschema configuration wrappers (discussed below), and then passes the subschemas to stitchSchemas to produce one combined schema with the following root fields:

type Query { postById(id: ID!): Post postsByUserId(userId: ID!): [Post]! userById(id: ID!): User }

We now have a single gateway schema that allows data from either subschema to be requested in the same query.

Last updated on