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
Modern Development Environment for C++: Part 2
In Part 1 of this series, I gave a quick overview of each tool in the development environment and how I came to the conclusion that the tool was a good fit. Today I’m going […]
Mock Intl and Date globals in Jest
In Javascript land, mocking the browser global objects can be a bit of a pain for tests. Searching StackOverflow gives plenty of complicated answers. Some suggesting using 3rd party mock libraries. Some that overwrite the […]
Bitbucket Parameterized Pipelines
Introduction I’d like to address how to handle lots of deployment environments withinBitBucket Pipelines. Below I’m presenting two options: Using tags to parameterize builds Using the BitBucket API to pass variables Lots of Environments There […]
AWS Cloud HSM, Docker and NGINX
There is quite a bit of easily searchable content on the security benefits of leveraging a Hardware Security Module to manage cryptographic keys, so I will leave that to the scope of another article. The […]