Jul 11, 2019

Scaling Apollo GraphQL

The newest versions of Apollo Server are introducing Apollo Gateway as a way of horizontal scaling using services. Up to this point, GraphQL Schemas were merged together into one big schema, called Schema Stitching, making it impossible to scale out. Little bit of an oversight on the part of Apollo Server, but they are catching up!

To show how this works, we will create two schemas, and mash them together with the mergeSchemas function in graphql-tools.

import {
  makeExecutableSchema,
  mergeSchemas,
} from 'graphql-tools';

const chickenSchema = makeExecutableSchema({
  typeDefs: `
    type Chicken {
      id: ID!
      name: String
    }

    type Query {
      chickenById(id: ID!): Chicken
      chickenByEgg(id: ID!): Chicken
    }
  `
});

const eggSchema = makeExecutableSchema({
  typeDefs: `
    type Egg {
      id: ID!
      weight: Int
    }

    type Query {
      eggById(id: ID!): Egg
      eggsForChicken(id: ID!) [Egg]
    }
  `
});

export const schema = mergeSchemas({
  schemas: [
    chickenSchema,
    eggSchema,
  ],
});

This big ol’ schema is passed to ApolloServer, and it’s ready to query.

Consider the cases where:

  • Some schemas get more requests than others
  • Some schemas take more time to query (more complex data structures or maybe calling slow external services)
  • You want some schemas to respond faster than others

If any of these factors matter in your case, scaling out, or horizontally will help in a big way. The typical methods include microservice architecture or just a round robin approach. The microservice way, simply put, is an auto scaling architecture that scales based on need, orchestrated by some Serverless platform such as AWS Lambas, Google Cloud Functions, Azure Functions or IBM’s OpenWhisk. If you want to stay simple, you can boot a bunch of services behind an Nginx server, and let Nginx evenly distribute the requests. Either way, both methods are much faster and cheaper than monolithic vertical scaling.

Now that we know that we want to scale out, how can we do that with Apollo? Well, as you can see in the example above, all the schemas and work is provided by one big Apollo Server. Apollo Gateway is what we want. The Gateway takes a list of external services and federates requests. This is called Schema Federation. The amazing part of Apollo Gateway is the automatic resolution for schemas that reference other schemas. Schema’s don’t have to be directly aware of, or import any sibling schemas.

Anyway, let’s make an Apollo Server that scales.

A Chicken Service that references the Egg Service.

const typeDefs = gql`
  type Egg @key(fields: "eggId") {
    eggId: ID!
  }

  type Chicken {
    chickenID: ID
    name: String
    eggs: [Egg]
  }
`;

const resolvers = {
  Chicken: {
    __resolveReference(chickenID){
      return queryADatabase(chickenID)
    },
   eggs(fetchedEggs) {
      // transform your fetched egg however you want
      return fetchedEggs
   }
  }
};

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

An Egg Service that references the Chicken Service.

const typeDefs = gql`
  type Chicken @key(fields: "chickenID") {
    chickenID: ID!
  }

  type Egg {
    eggID: ID
    name: String
    chicken: Chicken
  }
`;

const resolvers = {
  Egg: {
    __resolveReference(chickenId){
      return queryADatabase(chickenId)
    },
   chicken(fetchedChicken) {
      // transform your fetched chicken however you want
      return fetchedChicken
   }
  }
};

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

Now, boot your gateway!

const gateway = new ApolloGateway({
  serviceList: [
    { name: 'chicken', url: 'https://chicken-service-url/graphql' },
    { name: 'egg', url: 'https://egg-service-url/graphql' }
  ]
});

  const { schema, executor } = await gateway.load();

  const server = new ApolloServer({
    schema,
    executor
  });

That’s it! Now, use any architecture or platform you’d like to scale your schema services.

About the Author

Corey Webster profile.

Corey Webster

Sr. Consultant

One thought on “Scaling Apollo GraphQL

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Blog Posts
Performance Test Liquibase Update
When doing a liquibase update to a database if you’re having performance issues, it can be hard to find out which updates are causing problems. If you need to measure the time to apply each […]
TICK Stack Monitoring for the Non-Technical
TICK – Telegraf, Influx, Chronograf, and Kapacitor – is a method of monitoring your systems and applications. In this article, I discuss in non-technical terms what the difference is between TICK and Prometheus Grafana A […]
Design Systems, Part 1 • Introduction
Business leaders need a practical guide to plan and execute Design System Initiatives. The aim of this series is to be that guide. This installment introduces terms and definitions as a primer on Design Systems.
ML for Translating Dysarthria Speech (Pre-Part 1)
What is Dysarthria? Per the Mayo Clinic, Dysarthria occurs when the muscles you use for speech are weak or you have difficulty controlling them. Dysarthria often causes slurred or slow speech that can be difficult […]