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.

Related Blog Posts
A security model for developers
Software security is more important than ever, but developing secure applications is more confusing than ever. TLS, mTLS, RBAC, SAML, OAUTH, OWASP, GDPR, SASL, RSA, JWT, cookie, attack vector, DDoS, firewall, VPN, security groups, exploit, […]
Building Better Data Visualization Experiences: Part 1 of 2
Through direct experience with data scientists, business analysts, lab technicians, as well as other UX professionals, I have found that we need a better understanding of the people who will be using our data visualization products in order to build them. Creating a product utilizing data with the goal of providing insight is fundamentally different from a typical user-centric web experience, although traditional UX process methods can help.
Kafka Schema Evolution With Java Spring Boot and Protobuf
In this blog I will be demonstrating Kafka schema evolution with Java, Spring Boot and Protobuf.  This app is for tutorial purposes, so there will be instances where a refactor could happen. I tried to […]
Redis Bitmaps: Storing state in small places
Redis is a popular open source in-memory data store that supports all kinds of abstract data structures. In this post and in an accompanying example Java project, I am going to explore two great use […]