Combining Multiple GraphQL Services

Tutorial 3 of 5

1. Introduction

1.1 Brief Explanation of the Tutorial's Goal

This tutorial aims to guide you through the process of combining multiple GraphQL services into a single schema using schema stitching and schema federation methods.

1.2 What the User Will Learn

By the end of this tutorial, you will be able to:
- Understand schema stitching and schema federation.
- Combine multiple GraphQL services into a single schema.
- Handle potential challenges during the process.

1.3 Prerequisites

Before starting this tutorial, you should have a basic understanding of:
- JavaScript (ES6)
- GraphQL basics
- Node.js and npm

2. Step-by-Step Guide

2.1 Schema Stitching

Schema stitching is the process of creating a single GraphQL schema from multiple underlying GraphQL APIs.

2.1.1 Concept

The main idea behind schema stitching is to create a new schema, then extend it for each existing schema and finally merge them all together.

2.1.2 Example

// Import required libraries
const { mergeSchemas } = require('graphql-tools');
const userSchema = require('./user');
const productSchema = require('./product');

// Merge the schemas
const schema = mergeSchemas({
  schemas: [
    userSchema,
    productSchema,
  ],
});

In the example above, we import two schemas (userSchema and productSchema) and then merge them into one schema using the mergeSchemas function from the graphql-tools library.

2.2 Schema Federation

Schema federation is a more advanced method of combining schemas. It allows you to import types from one service into another and to split your business logic into smaller services.

2.2.1 Concept

Federation involves creating a gateway and services. The gateway fetches the schema of each service and combines them into one.

2.2.2 Example

// Import required libraries
const { ApolloServer, gql } = require('apollo-server');
const { buildFederatedSchema } = require('@apollo/federation');

// Define a schema
const typeDefs = gql`
  type Query {
    me: User
  }

  type User @key(fields: "id") {
    id: ID!
    name: String
  }
`;

// Provide resolver functions for your schema fields
const resolvers = {
  Query: {
    me() {
      return { id: "1", name: "Alice" };
    }
  },
  User: {
    __resolveReference(user, { dataSources }) {
      return dataSources.usersAPI.getUser(user.id);
    }
  }
};

// Build the federated schema
const server = new ApolloServer({
  schema: buildFederatedSchema([{ typeDefs, resolvers }])
});

server.listen().then(({ url }) => {
  console.log(`Server ready at ${url}`);
});

3. Code Examples

3.1 Example 1: Simple Schema Stitching

This example shows a simple schema stitching process with two schemas: userSchema and productSchema.

const { mergeSchemas } = require('graphql-tools');
const userSchema = require('./user');
const productSchema = require('./product');

const schema = mergeSchemas({
  schemas: [
    userSchema,
    productSchema,
  ],
});

3.2 Example 2: Simple Schema Federation

This example shows a simple schema federation process.

const { ApolloServer, gql } = require('apollo-server');
const { buildFederatedSchema } = require('@apollo/federation');

const typeDefs = gql`
  type Query {
    me: User
  }

  type User @key(fields: "id") {
    id: ID!
    name: String
  }
`;

const resolvers = {
  Query: {
    me() {
      return { id: "1", name: "Alice" };
    }
  },
  User: {
    __resolveReference(user, { dataSources }) {
      return dataSources.usersAPI.getUser(user.id);
    }
  }
};

const server = new ApolloServer({
  schema: buildFederatedSchema([{ typeDefs, resolvers }])
});

server.listen().then(({ url }) => {
  console.log(`Server ready at ${url}`);
});

4. Summary

In this tutorial, we have learned about two methods of combining multiple GraphQL services into a single schema: schema stitching and schema federation.

The next steps for learning would be to explore each method in deeper detail, and to practice by creating and combining more complex schemas.

For additional resources, check out the official GraphQL documentation and the Apollo Federation documentation.

5. Practice Exercises

  1. Exercise 1: Create two simple GraphQL services and combine them using schema stitching.
  2. Exercise 2: Create three GraphQL services and combine them using schema federation.
  3. Exercise 3: Create a GraphQL gateway that combines multiple services.

Solutions:

  1. You can follow the example given in section 3.1 to create and stitch two simple schemas.
  2. For schema federation, you can follow the example given in section 3.2. The only difference would be that you'll need to create one more type and resolver for the third service.
  3. Creating a GraphQL gateway involves defining a new ApolloServer and passing an instance of ApolloGateway as the schema. You can find more detailed information and examples in the Apollo Federation documentation.