0
0
GraphqlConceptBeginner · 4 min read

What is Schema Stitching in GraphQL: Explanation and Example

Schema stitching in GraphQL is a technique to combine multiple GraphQL schemas into a single unified schema. It allows different services or APIs to be merged so clients can query them as one, simplifying data fetching across systems.
⚙️

How It Works

Imagine you have several small shops, each with its own product list. Schema stitching is like creating a big mall directory that combines all these lists into one, so shoppers can find everything in one place without visiting each shop separately.

In GraphQL, each service has its own schema describing its data and operations. Schema stitching merges these schemas into a single schema that clients query. Behind the scenes, it forwards parts of the query to the right service and combines the results.

This helps developers build modular APIs and keep services independent while providing a seamless experience to clients.

💻

Example

This example shows how two simple GraphQL schemas can be stitched together using Apollo Server.

javascript
const { ApolloServer, gql } = require('apollo-server');
const { mergeSchemas } = require('@graphql-tools/merge');

// First schema: User service
const userTypeDefs = gql`
  type User {
    id: ID!
    name: String!
  }
  type Query {
    user(id: ID!): User
  }
`;
const userResolvers = {
  Query: {
    user: (_, { id }) => ({ id, name: 'Alice' }),
  },
};

// Second schema: Product service
const productTypeDefs = gql`
  type Product {
    id: ID!
    name: String!
    ownerId: ID!
  }
  type Query {
    product(id: ID!): Product
  }
`;
const productResolvers = {
  Query: {
    product: (_, { id }) => ({ id, name: 'Book', ownerId: '1' }),
  },
};

// Stitch schemas
const stitchedSchema = mergeSchemas({
  schemas: [userTypeDefs, productTypeDefs],
  resolvers: [userResolvers, productResolvers],
});

// Create server
const server = new ApolloServer({ schema: stitchedSchema });

server.listen().then(({ url }) => {
  console.log(`Server ready at ${url}`);
});
Output
Server ready at http://localhost:4000/
🎯

When to Use

Use schema stitching when you have multiple GraphQL services or APIs that you want to combine into one endpoint. This is common in microservices architectures where each service owns its data and schema.

It helps teams work independently on different parts of the API while providing clients a unified way to query data. For example, an e-commerce app might stitch user, product, and order services together.

However, for new projects, consider using GraphQL Federation as a modern alternative that offers more features and better support.

Key Points

  • Schema stitching merges multiple GraphQL schemas into one.
  • It allows querying multiple services through a single endpoint.
  • Useful for combining microservices or third-party APIs.
  • It forwards queries to the right service and combines results.
  • GraphQL Federation is a newer alternative to schema stitching.

Key Takeaways

Schema stitching combines multiple GraphQL schemas into a single API.
It enables clients to query data from different services seamlessly.
Ideal for microservices architectures needing unified data access.
Behind the scenes, it delegates parts of queries to appropriate services.
Consider GraphQL Federation for newer projects as a modern alternative.