This article assumes some working knowledge with server-side GraphQL, in particular Apollo Server. It is part of a series on enterprise grade GraphQL hosted by smartive.

Why Explore the Internals of Apollo Server

Perhaps you feel you need to know more about the GraphQL schema:

What does Apollo Server do with the GraphQL schema?

Where/how can I access it from within a resolver?

How can I transform the GraphQL schema?

Can I generate the schema (or parts of it) programmatically?

Perhaps what’s puzzling you are the queries received by the GraphQL server:

How can I analyze the query received from the client?

How can I relate the query to the schema (e.g. to find custom validation directives)?

How can I use the query to write more complex resolvers (e.g. recursively joining data in an Almighty Root Resolver)?

To answer these and many more advanced GraphQL questions, you need to become fluent in the internals of Apollo Server. More precisely, you need to understand how Apollo Server employs certain core GraphQL libraries, and how to use them to your advantage.

Abandon Hope All Ye Who Enter Here (Without TypeScript)

Warning: We are entering a scarcely documented area of GraphQL land. You are mostly on your own. TypeScript turned out to be essential for the exploration of these internals. There’s nothing like ctrl-click (other than console.log ) to further your insight in these matters.

Understanding GraphQL Schemas

There are a number of ways to define your GraphQL schema. The most straightforward is a schema.graphql file.

schema.graphql

You can then define some resolvers and feed everything to Apollo Server.

OK, but what does Apollo do with that information? Where can I access it? How can I modify it?

Executable Schemas

What Apollo does in the background is:

Parse the schema string into an Abstract Syntax Tree (AST) Transform the AST into a GraphQLSchema Add the resolver functions to the GraphQLSchema object

In other words, the above Apollo initialization is equivalent to the following:

Think about it this way: the AST is a representation of the schema document, while the GraphQLSchema object is a data structure that can resolve GraphQL queries. Note that both these types are defined in the graphql package, the core implementation of the GraphQL language.

Getting the Schema and the AST in a Resolver