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
Natively Compiled Java on Google App Engine
Google App Engine is a platform-as-a-service product that is marketed as a way to get your applications into the cloud without necessarily knowing all of the infrastructure bits and pieces to do so. Google App […]
Building Better Data Visualization Experiences: Part 2 of 2
If you don't have a Ph.D. in data science, the raw data might be difficult to comprehend. This is where data visualization comes in.
Unleashing Feature Flags onto Kafka Consumers
Feature flags are a tool to strategically enable or disable functionality at runtime. They are often used to drive different user experiences but can also be useful in real-time data systems. In this post, we’ll […]
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, […]