The main purpose of GraphQL Config Extensions is to pass information to extension's consumer in order to extend the behavior of GraphQL Config's logic.
GraphQL Config ships TypeScript declaration files so let's make use of them in following examples.
How to write extensions
In order to make sure you write extensions correctly, import and use the
GraphQLExtensionDeclaration type from
graphql-config package. Thanks to TypeScript, you get autocompletion and in-editor validation.
The main requirement of an extension is its name. Providing a name lets GraphQL Config to match the extension with its namespace in the config file.
GraphQL Config lets you intercept the GraphQL Schema loading process which may be helpful when dealing with custom directives like in Relay or Apollo Federation. We call it Middlewares.
As a GraphQL tool author you will likely want to load the config and register your extension in order to understand user's configuration.
Now that everything is ready, GraphQL Config understands there's the Inspector extension.
In order to access information stored in the config file, do the following:
GraphQLSchema is straightforward: each project has
getSchema(): Promise<GraphQLSchema> method.
GraphQL Config is able to generate a schema not only as
GraphQLSchema object, but also as a
DocumentNode. (For more info, read the API reference of
It's also capable of loading operations and fragments.
In previous examples, we pointed GraphQL Config to the
schema.graphql file. GraphQL Config, by default, understands the Introspection result stored in JSON file, GraphQL files (
.gqls) and the document returned by any functioning GraphQL endpoint (specified by URL).
It's now possible thanks to loaders.
For simplicity, we're going to use only the one responsible for extracting GraphQL SDL from code files.
Let's say you have GraphQL SDL modularized across multiple TypeScript files, written like this:
CodeFileLoader you can extract those GraphQL pieces:
There are two kinds of loaders. One is responsible for handling schemas, and the other covers Operations and Fragments (we call them both
To read more about loaders, please check "Loaders" chapter