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.

One thought on “Scaling Apollo GraphQL

Leave a Reply

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

*

*