Skip to content

Commit 391371d

Browse files
authored
Add docs for Apollo config (#505)
* Add docs for Apollo config * Specify JSON blocks as JS to allow comments * Fix JS style config example
1 parent 8d8221d commit 391371d

1 file changed

Lines changed: 104 additions & 14 deletions

File tree

packages/apollo-cli/README.md

Lines changed: 104 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,19 @@
55
Apollo CLI brings together your GraphQL clients and servers with tools for validating your schema, linting your operations for compatibility with your server, and generating static types for improved client-side type safety.
66

77
<!-- toc -->
8-
* [Apollo CLI](#apollo-cli)
9-
* [Usage](#usage)
10-
* [Commands](#commands)
11-
* [Code Generation](#code-generation)
12-
* [Contributing](#contributing)
13-
<!-- tocstop -->
8+
9+
- [Apollo CLI](#apollo-cli)
10+
- [Usage](#usage)
11+
- [Commands](#commands)
12+
- [Configuration](#configuration)
13+
- [Code Generation](#code-generation)
14+
- [Contributing](#contributing)
15+
<!-- tocstop -->
1416

1517
# Usage
1618

1719
<!-- usage -->
20+
1821
```sh-session
1922
$ npm install -g apollo
2023
$ apollo COMMAND
@@ -26,17 +29,19 @@ USAGE
2629
$ apollo COMMAND
2730
...
2831
```
32+
2933
<!-- usagestop -->
3034

3135
# Commands
3236

3337
<!-- commands -->
34-
* [`apollo codegen:generate [OUTPUT]`](#apollo-codegengenerate-output)
35-
* [`apollo help [COMMAND]`](#apollo-help-command)
36-
* [`apollo queries:check`](#apollo-queriescheck)
37-
* [`apollo schema:check`](#apollo-schemacheck)
38-
* [`apollo schema:download OUTPUT`](#apollo-schemadownload-output)
39-
* [`apollo schema:publish`](#apollo-schemapublish)
38+
39+
- [`apollo codegen:generate [OUTPUT]`](#apollo-codegengenerate-output)
40+
- [`apollo help [COMMAND]`](#apollo-help-command)
41+
- [`apollo queries:check`](#apollo-queriescheck)
42+
- [`apollo schema:check`](#apollo-schemacheck)
43+
- [`apollo schema:download OUTPUT`](#apollo-schemadownload-output)
44+
- [`apollo schema:publish`](#apollo-schemapublish)
4045

4146
## `apollo codegen:generate [OUTPUT]`
4247

@@ -50,11 +55,11 @@ ARGUMENTS
5055
OUTPUT
5156
Directory to which generated files will be written.
5257
- For TypeScript/Flow generators, this specifies a directory relative to each source file by default.
53-
- For TypeScript/Flow generators with the "outputFlat" flag is set, and for the Swift generator, this specifies a
58+
- For TypeScript/Flow generators with the "outputFlat" flag is set, and for the Swift generator, this specifies a
5459
file or directory (absolute or relative to the current working directory) to which:
5560
- a file will be written for each query (if "output" is a directory)
5661
- all generated types will be written
57-
- For all other types, this defines a file (absolute or relative to the current working directory) to which all
62+
- For all other types, this defines a file (absolute or relative to the current working directory) to which all
5863
generated types are written.
5964
6065
OPTIONS
@@ -204,8 +209,93 @@ OPTIONS
204209
```
205210

206211
_See code: [src/commands/schema/publish.ts](https://github.com/apollographql/apollo-cli/blob/v1.5.0/src/commands/schema/publish.ts)_
212+
207213
<!-- commandsstop -->
208214

215+
# Configuration
216+
217+
The Apollo CLI and VS Code extension can be configured with an Apollo Config file. Apollo configuration is stored as a plain object and can be either specified under the `apollo` key in your `package.json` or as a separate `apollo.config.js` which exports the config data.
218+
219+
The core of any configuration is specifying schemas and queries. Schemas specify information about your backend such as where to get the schema, what endpoint to make requests against, and the Apollo Engine API key to get schema updates and stats from. Queries define which documents Apollo tooling should analyze and tie them to the schema they are targeting.
220+
221+
Let's take a look at a basic configuration file (`package.json` style):
222+
223+
```js
224+
{
225+
...
226+
"apollo": {
227+
"schemas": {
228+
"myPrimaryBackend": {
229+
"schema": "downloadedSchema.json", // if not defined the an introspection query will be run
230+
"endpoint": "http://example.com/graphql", // if not defined the schema will be downloaded from Apollo Engine
231+
"engineKey": "my-engine-key" // use this key when connecting to Apollo Engine
232+
}
233+
},
234+
"queries": [
235+
{
236+
"schema": "myPrimaryBackend", // reference the previously defined schema
237+
"includes": [ "**/*.tsx" ], // load queries from .tsx files
238+
"excludes": [ "node_modules/**" ] // don't include any matching files from node_modules
239+
}
240+
]
241+
}
242+
}
243+
```
244+
245+
Or in `apollo.config.js` style:
246+
247+
```js
248+
...
249+
250+
module.exports = {
251+
schemas: {
252+
myPrimaryBackend: {
253+
schema: "downloadedSchema.json", // if not defined the an introspection query will be run
254+
endpoint: "http://example.com/graphql", // if not defined the schema will be downloaded from Apollo Engine
255+
engineKey: "my-engine-key" // use this key when connecting to Apollo Engine
256+
}
257+
},
258+
queries: [
259+
{
260+
schema: "myPrimaryBackend", // reference the previously defined schema
261+
includes: [ "**/*.tsx" ], // load queries from .tsx files
262+
excludes: [ "node_modules/**" ] // don't include any matching files from node_modules
263+
}
264+
]
265+
}
266+
```
267+
268+
## Endpoint Configuration
269+
270+
When configuring a schema's endpoint, you can either pass in a string or an object, which allows for specifying advanced options like headers and subscription endpoints.
271+
272+
```js
273+
endpoint: {
274+
url: "http://example.com/graphql",
275+
subscriptions: "ws://example.com/graphql",
276+
headers: {
277+
cookie: "myCookie=myCookieValue"
278+
}
279+
}
280+
```
281+
282+
## Schema Dependencies
283+
284+
Schemas can also declare dependencies on eachother, which can be useful in situations like having a client-side schema for `apollo-link-state`. To declare a dependency, use the `extends` key. When working with a client-side schema, make sure to also specify the `clientSide` key to enable code-generation support.
285+
286+
```js
287+
schemas: {
288+
myServerSideSchema: {
289+
...
290+
},
291+
myClientSideSchema: {
292+
extends: "myServerSideSchema",
293+
clientSide: true
294+
...
295+
}
296+
}
297+
```
298+
209299
# Code Generation
210300

211301
## Accompanying Libraries

0 commit comments

Comments
 (0)