Configuring Apollo projects
How to configure Apollo VS Code and CLI with apollo.config.js
Apollo projects are configured using an apollo.config.js
file at the root of your project. Many Apollo tools leverage your Apollo config, reducing the net amount of configuration you need to do in your project in the end.
If you're using one of our workflow tools like the Apollo CLI or the Apollo VS Code extension, you'll need to have an apollo.config.js
project to get the features those tools bring.
There are two types of projects, client
and service
. When a client
key is found in the config, a project is treated as a client project, and only client:*
commands may be run against it. The same is true for service
projects. The apollo.config.js
file is meant to describe configuration for a single project only. If used in a monorepo with multiple projects, there should be a separate config file for each project.
This document describes all the options available in the Apollo config and defines which are required versus optional.
Client projects
Client projects are configured through a top level client
key in the config.
module.exports = {client: { ... },};
client.service
Required - the Apollo CLI and VS Code extension rely on knowledge of your schema to show you "IntelliSense" (for example, autocomplete on fields, metrics annotations, query validation).
There are a few different ways you can link your client to a schema:
- Use the Apollo schema registry
- With a remote endpoint (from a running server)
- With a local schema file
Option 1: Use the Apollo schema registry
To link your client to a schema through the Apollo schema registry, you'll need to have at least one version of your schema published to the registry.
With GraphOS Studio set up, you can point your client directly to your graph's schema by specifying your graph's name in your Apollo config, like so:
module.exports = {client: {service: 'my-apollo-service' // the name of your graph in Studio}};
ⓘ NOTE
You must have a published schema for features like VS Code IntelliSense, which requires knowledge of your schema, to work properly.
If you're tracking different versions of your schema in the registry using graph variants, you can link your client to a specific variant like so:
module.exports = {client: {service: 'my-apollo-service@staging' // "staging" is the graph variant we're using}};
If a graph variant is not specified, Apollo tools will fall back to the default value of current
.
Option 2: Link a schema from a remote endpoint
Remote endpoints can be used to pull down a schema from a running service. This can be configured like so:
module.exports = {client: {service: {name: 'github',url: 'https://api.github.com/graphql',// optional headersheaders: {authorization: 'Bearer lkjfalkfjadkfjeopknavadf'},// optionally turn off SSL validation checkskipSSLValidation: true}}};
Option 3: Link a schema from a local file
In some cases you may have a locally generated file with your schema that you want to link. This can be either a .graphql
file with the schema in SDL form or a saved introspection result in .json
. To link your client project to a local schema file, configure it like so:
module.exports = {client: {service: {name: 'my-service-name',localSchemaFile: './path/to/schema.graphql'}}};
client.includes
Optional - by default, Apollo tools will look under a ./src
directory to find all operations and SDL to extract.
Client projects often contain client-side schema definitions for local state with Apollo Client. To make sure the Apollo CLI and VS Code extension can find these files and read them correctly, you may need to tell Apollo which folders to look for your schema and queries in like so:
module.exports = {client: {includes: ['./imports/**/*.js'], // array of glob patternsservice: ...},};
client.excludes
Optional - by default, Apollo tools will exclude **/node_modules
and **/__tests___
when looking for your queries and schema files.
If you want Apollo to ignore any of your other folders when looking for queries and schema definitions, adjust your config like so:
module.exports = {client: {excludes: ['**/__tests__/**/*'], // array of glob patternsservice: ...},};
client.tagName
Optional - custom tagged template literal.
When using GraphQL with JavaScript or TypeScript projects, it is common to use the gql
tagged template literal to write out operations. Apollo tools will be looking through your files for the gql
tag to extract your queries, so if you use a different template literal, you can configure it like so:
module.exports = {client: {tagName: "graphql",service: ...}};
client.addTypename
Optional - Apollo will by default add the __typename
field to all your operations automatically and to all your generated types during codegen.
GraphQL clients like Apollo Client often add the __typename
field to operations automatically when they're sent over the wire. This can come in really handy for things like caching, but it can be turned off by adding addTypename: false
to the client config:
module.exports = {client: {addTypename: false,service: ...}};
💡 TIP
For consistency, we recommend that you keep this option consistent with how your ApolloClient
is configured.
client.clientOnlyDirectives
, client.clientSchemaDirectives
Optional - By default, Apollo projects support the following client-side directives:
@client
for local state@rest
for usingapollo-link-rest
@connection
for custom pagination with Apollo Client@type
for dynamic type names withapollo-link-rest
Client side applications can use custom directives on their queries that aren't meant to be sent to the server. Configuration of client side directives beyond the defaults listed above can be set up like so:
module.exports = {client: {clientOnlyDirectives: ["connection", "type"],clientSchemaDirectives: ["client", "rest"],service: ...}};
clientOnlyDirectives
are directives that should be stripped out of the operation before being sent to the server. An example of this is the @connection
directive.
clientSchemaDirectives
are directives that indicate a portion of the operation that is not meant to be sent to the server. These directives are removed as well as the fields they are placed on. An example of this type of directive is the @client
directive.
Server projects
Server projects are configured through a top level service
key in the config.
module.exports = {service: { ... },};
Defining a service
key in your Apollo config will provide the Apollo CLI with the information it needs to perform commands like apollo service:push
and apollo service:check
. You can set up the schema for your service to load in one of two ways:
- Using a remote endpoint
- Using a local schema file
Option 1: Remote endpoint
Remote endpoints can be used to pull down a schema from a running service. This can be configured like so:
module.exports = {service: {endpoint: {url: 'https://api.github.com/graphql', // defaults to http://localhost:4000headers: {// optionalauthorization: 'Bearer lkjfalkfjadkfjeopknavadf'},skipSSLValidation: true // optional, disables SSL validation check}}};
Option 2: Local schema
In some cases you may have a locally generated file with your schema that you want to link. This can be either a .graphql
file with the schema in SDL form or a saved introspection result in .json
. To link your client project to a local schema file, configure it like so:
module.exports = {service: {localSchemaFile: './path/to/schema.graphql'}};