UNPKG

38.6 kBMarkdownView Raw
1# Apollo CLI
2
3[![GitHub license](https://img.shields.io/badge/license-MIT-lightgrey.svg?maxAge=2592000)](https://raw.githubusercontent.com/apollographql/apollo-tooling/master/LICENSE) [![npm](https://img.shields.io/npm/v/apollo.svg)](https://www.npmjs.com/package/apollo) [![Get on Slack](https://img.shields.io/badge/spectrum-join-orange.svg)](https://spectrum.chat/apollo?tab=posts)
4
5Apollo 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.
6
7<!-- toc -->
8* [Apollo CLI](#apollo-cli)
9* [Usage](#usage)
10* [Commands](#commands)
11* [Configuration](#configuration)
12* [Code Generation](#code-generation)
13* [Contributing](#contributing)
14<!-- tocstop -->
15
16# Usage
17
18**Disclaimer**: The following API documentation is only for the latest version released on NPM, and may not be accurate for previous or future versions.
19
20<!-- usage -->
21```sh-session
22$ npm install -g apollo
23$ apollo COMMAND
24running command...
25$ apollo (-v|--version|version)
26apollo/2.31.2 linux-x64 node-v10.23.0
27$ apollo --help [COMMAND]
28USAGE
29 $ apollo COMMAND
30...
31```
32<!-- usagestop -->
33
34# Commands
35
36<!-- commands -->
37* [`apollo client:check`](#apollo-clientcheck)
38* [`apollo client:codegen [OUTPUT]`](#apollo-clientcodegen-output)
39* [`apollo client:download-schema OUTPUT`](#apollo-clientdownload-schema-output)
40* [`apollo client:extract OUTPUT`](#apollo-clientextract-output)
41* [`apollo client:push`](#apollo-clientpush)
42* [`apollo help [COMMAND]`](#apollo-help-command)
43* [`apollo plugins`](#apollo-plugins)
44* [`apollo plugins:install PLUGIN...`](#apollo-pluginsinstall-plugin)
45* [`apollo plugins:link PLUGIN`](#apollo-pluginslink-plugin)
46* [`apollo plugins:uninstall PLUGIN...`](#apollo-pluginsuninstall-plugin)
47* [`apollo plugins:update`](#apollo-pluginsupdate)
48* [`apollo service:check`](#apollo-servicecheck)
49* [`apollo service:delete`](#apollo-servicedelete)
50* [`apollo service:download OUTPUT`](#apollo-servicedownload-output)
51* [`apollo service:list`](#apollo-servicelist)
52* [`apollo service:push`](#apollo-servicepush)
53
54## `apollo client:check`
55
56Check a client project against a pushed service
57
58```
59USAGE
60 $ apollo client:check
61
62OPTIONS
63 -c, --config=config Path to your Apollo config file
64
65 -g, --graph=graph The ID for the graph in Apollo to operate client commands with. Overrides
66 config file if set.
67
68 -v, --variant=variant The variant of the graph in Apollo to associate this client to
69
70 --clientName=clientName Name of the client that the queries will be attached to
71
72 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will use
73 clientName if not provided
74
75 --clientVersion=clientVersion The version of the client that the queries will be attached to
76
77 --endpoint=endpoint The URL for the CLI use to introspect your service
78
79 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently
80 work in watch mode
81
82 --header=header Additional header to send during introspection. May be used multiple times to
83 add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the
84 `--header` flag.
85
86 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
87 queries *and* any client schema extensions
88
89 --key=key The API key to use for authentication to Apollo
90
91 --queries=queries Deprecated in favor of the includes flag
92
93 --tagName=tagName Name of the template literal tag used to identify template literals containing
94 GraphQL queries in Javascript/Typescript code
95```
96
97_See code: [src/commands/client/check.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/check.ts)_
98
99## `apollo client:codegen [OUTPUT]`
100
101Generate static types for GraphQL queries. Can use the published schema in the Apollo registry or a downloaded schema.
102
103```
104USAGE
105 $ apollo client:codegen [OUTPUT]
106
107ARGUMENTS
108 OUTPUT
109 Directory to which generated files will be written.
110 - For TypeScript/Flow generators, this specifies a directory relative to each source file by default.
111 - For TypeScript/Flow generators with the "outputFlat" flag is set, and for the Swift generator, this specifies a
112 file or directory (absolute or relative to the current working directory) to which:
113 - a file will be written for each query (if "output" is a directory)
114 - all generated types will be written
115 - For all other types, this defines a file (absolute or relative to the current working directory) to which all
116 generated types are written.
117
118OPTIONS
119 -c, --config=config Path to your Apollo config file
120
121 -g, --graph=graph The ID for the graph in Apollo to operate client commands with. Overrides
122 config file if set.
123
124 -v, --variant=variant The variant of the graph in Apollo to associate this client to
125
126 --[no-]addTypename [default: true] Automatically add __typename to your queries, can be unset
127 with --no-addTypename
128
129 --clientName=clientName Name of the client that the queries will be attached to
130
131 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will
132 use clientName if not provided
133
134 --clientVersion=clientVersion The version of the client that the queries will be attached to
135
136 --customScalarsPrefix=customScalarsPrefix Include a prefix when using provided types for custom scalars
137
138 --endpoint=endpoint The URL for the CLI use to introspect your service
139
140 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't
141 currently work in watch mode
142
143 --globalTypesFile=globalTypesFile By default, TypeScript will put a file named "globalTypes.ts" inside the
144 "output" directory. Set "globalTypesFile" to specify a different path.
145 Alternatively, set "tsFileExtension" to modify the extension of the file,
146 for example "d.ts" will output "globalTypes.d.ts"
147
148 --header=header Additional header to send during introspection. May be used multiple times
149 to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using
150 the `--header` flag.
151
152 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
153 queries *and* any client schema extensions
154
155 --key=key The API key to use for authentication to Apollo
156
157 --localSchemaFile=localSchemaFile Path to one or more local GraphQL schema file(s), as introspection result
158 or SDL. Supports comma-separated list of paths (ex.
159 `--localSchemaFile=schema.graphql,extensions.graphql`)
160
161 --mergeInFieldsFromFragmentSpreads Merge fragment fields onto its enclosing type
162
163 --namespace=namespace The namespace to emit generated code into.
164
165 --omitDeprecatedEnumCases Omit deprecated enum cases from generated code [Swift only]
166
167 --only=only Parse all input files, but only output generated code for the specified
168 file [Swift only]
169
170 --operationIdsPath=operationIdsPath Path to an operation id JSON map file. If specified, also stores the
171 operation ids (hashes) as properties on operation types [currently
172 Swift-only]
173
174 --outputFlat By default, TypeScript/Flow will put each generated file in a directory
175 next to its source file using the value of the "output" as the directory
176 name. Set "outputFlat" to put all generated files in the directory relative
177 to the current working directory defined by "output".
178
179 --passthroughCustomScalars Use your own types for custom scalars
180
181 --queries=queries Deprecated in favor of the includes flag
182
183 --suppressSwiftMultilineStringLiterals Prevents operations from being rendered as multiline strings [Swift only]
184
185 --tagName=tagName Name of the template literal tag used to identify template literals
186 containing GraphQL queries in Javascript/Typescript code
187
188 --target=target (required) Type of code generator to use (swift | typescript | flow | scala
189 | json | json-modern (exposes raw json types))
190
191 --tsFileExtension=tsFileExtension By default, TypeScript will output "ts" files. Set "tsFileExtension" to
192 specify a different file extension, for example "d.ts"
193
194 --useFlowExactObjects Use Flow exact objects for generated types [flow only]
195
196 --useFlowReadOnlyTypes Use read only types for generated types [flow only]. **Deprecated in favor
197 of `useReadOnlyTypes`.**
198
199 --useReadOnlyTypes Use read only types for generated types [flow | typescript]
200
201 --watch Watch for file changes and reload codegen
202
203ALIASES
204 $ apollo codegen:generate
205```
206
207_See code: [src/commands/client/codegen.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/codegen.ts)_
208
209## `apollo client:download-schema OUTPUT`
210
211Download a schema from Apollo or a GraphQL endpoint in JSON or SDL format
212
213```
214USAGE
215 $ apollo client:download-schema OUTPUT
216
217ARGUMENTS
218 OUTPUT [default: schema.json] Path to write the introspection result to. Can be `.graphql`, `.gql`, `.graphqls`, or
219 `.json`
220
221OPTIONS
222 -c, --config=config Path to your Apollo config file
223
224 -g, --graph=graph The ID for the graph in Apollo to operate client commands with. Overrides
225 config file if set.
226
227 -v, --variant=variant The variant of the graph in Apollo to associate this client to
228
229 --clientName=clientName Name of the client that the queries will be attached to
230
231 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will use
232 clientName if not provided
233
234 --clientVersion=clientVersion The version of the client that the queries will be attached to
235
236 --endpoint=endpoint The URL for the CLI use to introspect your service
237
238 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently
239 work in watch mode
240
241 --header=header Additional header to send during introspection. May be used multiple times to
242 add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the
243 `--header` flag.
244
245 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
246 queries *and* any client schema extensions
247
248 --key=key The API key to use for authentication to Apollo
249
250 --queries=queries Deprecated in favor of the includes flag
251
252 --tagName=tagName Name of the template literal tag used to identify template literals containing
253 GraphQL queries in Javascript/Typescript code
254```
255
256_See code: [src/commands/client/download-schema.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/download-schema.ts)_
257
258## `apollo client:extract OUTPUT`
259
260Extract queries from a client
261
262```
263USAGE
264 $ apollo client:extract OUTPUT
265
266ARGUMENTS
267 OUTPUT [default: manifest.json] Path to write the extracted queries to
268
269OPTIONS
270 -c, --config=config Path to your Apollo config file
271
272 -g, --graph=graph The ID for the graph in Apollo to operate client commands with. Overrides
273 config file if set.
274
275 -v, --variant=variant The variant of the graph in Apollo to associate this client to
276
277 --clientName=clientName Name of the client that the queries will be attached to
278
279 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will use
280 clientName if not provided
281
282 --clientVersion=clientVersion The version of the client that the queries will be attached to
283
284 --endpoint=endpoint The URL for the CLI use to introspect your service
285
286 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently
287 work in watch mode
288
289 --header=header Additional header to send during introspection. May be used multiple times to
290 add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the
291 `--header` flag.
292
293 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
294 queries *and* any client schema extensions
295
296 --key=key The API key to use for authentication to Apollo
297
298 --preserveStringAndNumericLiterals Disable redaction of string and numerical literals. Without this flag, these
299 values will be replaced with empty strings (`''`) and zeroes (`0`)
300 respectively. This redaction is intended to avoid inadvertently outputting
301 potentially personally identifiable information (e.g. embedded passwords or
302 API keys) into operation manifests
303
304 --queries=queries Deprecated in favor of the includes flag
305
306 --tagName=tagName Name of the template literal tag used to identify template literals containing
307 GraphQL queries in Javascript/Typescript code
308```
309
310_See code: [src/commands/client/extract.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/extract.ts)_
311
312## `apollo client:push`
313
314Register operations with Apollo, adding them to the safelist
315
316```
317USAGE
318 $ apollo client:push
319
320OPTIONS
321 -c, --config=config Path to your Apollo config file
322
323 -g, --graph=graph The ID for the graph in Apollo to operate client commands with. Overrides
324 config file if set.
325
326 -v, --variant=variant The variant of the graph in Apollo to associate this client to
327
328 --clientName=clientName Name of the client that the queries will be attached to
329
330 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will use
331 clientName if not provided
332
333 --clientVersion=clientVersion The version of the client that the queries will be attached to
334
335 --endpoint=endpoint The URL for the CLI use to introspect your service
336
337 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently
338 work in watch mode
339
340 --header=header Additional header to send during introspection. May be used multiple times to
341 add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the
342 `--header` flag.
343
344 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
345 queries *and* any client schema extensions
346
347 --key=key The API key to use for authentication to Apollo
348
349 --queries=queries Deprecated in favor of the includes flag
350
351 --tagName=tagName Name of the template literal tag used to identify template literals containing
352 GraphQL queries in Javascript/Typescript code
353```
354
355_See code: [src/commands/client/push.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/push.ts)_
356
357## `apollo help [COMMAND]`
358
359display help for apollo
360
361```
362USAGE
363 $ apollo help [COMMAND]
364
365ARGUMENTS
366 COMMAND command to show help for
367
368OPTIONS
369 --all see all commands in CLI
370```
371
372_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v2.2.3/src/commands/help.ts)_
373
374## `apollo plugins`
375
376list installed plugins
377
378```
379USAGE
380 $ apollo plugins
381
382OPTIONS
383 --core show core plugins
384
385EXAMPLE
386 $ apollo plugins
387```
388
389_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.9.1/src/commands/plugins/index.ts)_
390
391## `apollo plugins:install PLUGIN...`
392
393installs a plugin into the CLI
394
395```
396USAGE
397 $ apollo plugins:install PLUGIN...
398
399ARGUMENTS
400 PLUGIN plugin to install
401
402OPTIONS
403 -f, --force yarn install with force flag
404 -h, --help show CLI help
405 -v, --verbose
406
407DESCRIPTION
408 Can be installed from npm or a git url.
409
410 Installation of a user-installed plugin will override a core plugin.
411
412 e.g. If you have a core plugin that has a 'hello' command, installing a user-installed plugin with a 'hello' command
413 will override the core plugin implementation. This is useful if a user needs to update core plugin functionality in
414 the CLI without the need to patch and update the whole CLI.
415
416ALIASES
417 $ apollo plugins:add
418
419EXAMPLES
420 $ apollo plugins:install myplugin
421 $ apollo plugins:install https://github.com/someuser/someplugin
422 $ apollo plugins:install someuser/someplugin
423```
424
425_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.9.1/src/commands/plugins/install.ts)_
426
427## `apollo plugins:link PLUGIN`
428
429links a plugin into the CLI for development
430
431```
432USAGE
433 $ apollo plugins:link PLUGIN
434
435ARGUMENTS
436 PATH [default: .] path to plugin
437
438OPTIONS
439 -h, --help show CLI help
440 -v, --verbose
441
442DESCRIPTION
443 Installation of a linked plugin will override a user-installed or core plugin.
444
445 e.g. If you have a user-installed or core plugin that has a 'hello' command, installing a linked plugin with a 'hello'
446 command will override the user-installed or core plugin implementation. This is useful for development work.
447
448EXAMPLE
449 $ apollo plugins:link myplugin
450```
451
452_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.9.1/src/commands/plugins/link.ts)_
453
454## `apollo plugins:uninstall PLUGIN...`
455
456removes a plugin from the CLI
457
458```
459USAGE
460 $ apollo plugins:uninstall PLUGIN...
461
462ARGUMENTS
463 PLUGIN plugin to uninstall
464
465OPTIONS
466 -h, --help show CLI help
467 -v, --verbose
468
469ALIASES
470 $ apollo plugins:unlink
471 $ apollo plugins:remove
472```
473
474_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.9.1/src/commands/plugins/uninstall.ts)_
475
476## `apollo plugins:update`
477
478update installed plugins
479
480```
481USAGE
482 $ apollo plugins:update
483
484OPTIONS
485 -h, --help show CLI help
486 -v, --verbose
487```
488
489_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.9.1/src/commands/plugins/update.ts)_
490
491## `apollo service:check`
492
493Check a service against known operation workloads to find breaking changes
494
495```
496USAGE
497 $ apollo service:check
498
499OPTIONS
500 -c, --config=config Path to your Apollo config file
501
502 -g, --graph=graph The ID of the graph in Apollo to check your proposed
503 schema changes against. Overrides config file if set.
504
505 -v, --variant=variant The variant to check the proposed schema against
506
507 --author=author The author to associate with this proposed schema
508
509 --branch=branch The branch name to associate with this check
510
511 --commitId=commitId The SHA-1 hash of the commit to associate with this
512 check
513
514 --endpoint=endpoint The URL for the CLI use to introspect your service
515
516 --header=header Additional header to send during introspection. May be
517 used multiple times to add multiple headers. NOTE: The
518 `--endpoint` flag is REQUIRED if using the `--header`
519 flag.
520
521 --ignoreFailures Exit with status 0 when the check completes, even if
522 errors are found
523
524 --json Output result in json, which can then be parsed by CLI
525 tools such as jq.
526
527 --key=key The API key to use for authentication to Apollo
528
529 --localSchemaFile=localSchemaFile Path to one or more local GraphQL schema file(s), as
530 introspection result or SDL. Supports comma-separated
531 list of paths (ex.
532 `--localSchemaFile=schema.graphql,extensions.graphql`)
533
534 --markdown Output result in markdown.
535
536 --queryCountThreshold=queryCountThreshold Minimum number of requests within the requested time
537 window for a query to be considered.
538
539 --queryCountThresholdPercentage=queryCountThresholdPercentage Number of requests within the requested time window for
540 a query to be considered, relative to total request
541 count. Expected values are between 0 and 0.05 (minimum
542 5% of total request volume)
543
544 --serviceName=serviceName Provides the name of the implementing service for a
545 federated graph. This flag will indicate that the
546 schema is a partial schema from a federated service
547
548 --validationPeriod=validationPeriod The size of the time window with which to validate the
549 schema against. You may provide a number (in seconds),
550 or an ISO8601 format duration for more granularity
551 (see: https://en.wikipedia.org/wiki/ISO_8601#Durations)
552
553ALIASES
554 $ apollo schema:check
555```
556
557_See code: [src/commands/service/check.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/check.ts)_
558
559## `apollo service:delete`
560
561Delete a federated service from Apollo and recompose remaining services
562
563```
564USAGE
565 $ apollo service:delete
566
567OPTIONS
568 -c, --config=config Path to your Apollo config file
569
570 -g, --graph=graph The ID of the graph in Apollo for which to delete an implementing service. Overrides config
571 file if set.
572
573 -v, --variant=variant The variant to delete the implementing service from
574
575 -y, --yes Bypass confirmation when deleting a service
576
577 --endpoint=endpoint The URL for the CLI use to introspect your service
578
579 --header=header Additional header to send during introspection. May be used multiple times to add multiple
580 headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag.
581
582 --key=key The API key to use for authentication to Apollo
583
584 --serviceName=serviceName (required) Provides the name of the implementing service for a federated graph
585```
586
587_See code: [src/commands/service/delete.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/delete.ts)_
588
589## `apollo service:download OUTPUT`
590
591Download the schema from your GraphQL endpoint.
592
593```
594USAGE
595 $ apollo service:download OUTPUT
596
597ARGUMENTS
598 OUTPUT [default: schema.json] Path to write the introspection result to. Supports .json output only.
599
600OPTIONS
601 -c, --config=config Path to your Apollo config file
602
603 -g, --graph=graph The ID of the graph in the Apollo registry for which to download the schema for. Overrides
604 config file if provided.
605
606 -k, --skipSSLValidation Allow connections to an SSL site without certs
607
608 -v, --variant=variant The variant to download the schema of
609
610 --endpoint=endpoint The URL for the CLI use to introspect your service
611
612 --header=header Additional header to send during introspection. May be used multiple times to add multiple
613 headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag.
614
615 --key=key The API key to use for authentication to Apollo
616
617ALIASES
618 $ apollo schema:download
619```
620
621_See code: [src/commands/service/download.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/download.ts)_
622
623## `apollo service:list`
624
625List the services in a graph
626
627```
628USAGE
629 $ apollo service:list
630
631OPTIONS
632 -c, --config=config Path to your Apollo config file
633
634 -g, --graph=graph The ID of the graph in the Apollo registry for which to list implementing services. Overrides
635 config file if set.
636
637 -v, --variant=variant The variant to list implementing services for
638
639 --endpoint=endpoint The URL for the CLI use to introspect your service
640
641 --header=header Additional header to send during introspection. May be used multiple times to add multiple
642 headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag.
643
644 --key=key The API key to use for authentication to Apollo
645```
646
647_See code: [src/commands/service/list.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/list.ts)_
648
649## `apollo service:push`
650
651Push a service definition to Apollo
652
653```
654USAGE
655 $ apollo service:push
656
657OPTIONS
658 -c, --config=config Path to your Apollo config file
659
660 -g, --graph=graph The ID of the graph in Apollo to publish your service to. Overrides config file if
661 set.
662
663 -v, --variant=variant The variant to publish your service to in Apollo
664
665 --author=author The author to associate with this publication
666
667 --branch=branch The branch name to associate with this publication
668
669 --commitId=commitId The SHA-1 hash of the commit to associate with this publication
670
671 --endpoint=endpoint The URL for the CLI use to introspect your service
672
673 --header=header Additional header to send during introspection. May be used multiple times to add
674 multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header`
675 flag.
676
677 --key=key The API key to use for authentication to Apollo
678
679 --localSchemaFile=localSchemaFile Path to one or more local GraphQL schema file(s), as introspection result or SDL.
680 Supports comma-separated list of paths (ex.
681 `--localSchemaFile=schema.graphql,extensions.graphql`)
682
683 --serviceName=serviceName Provides the name of the implementing service for a federated graph
684
685 --serviceRevision=serviceRevision Provides a unique revision identifier for a change to an implementing service on a
686 federated service push. The default of this is a git sha
687
688 --serviceURL=serviceURL Provides the url to the location of the implementing service for a federated graph
689
690ALIASES
691 $ apollo schema:publish
692```
693
694_See code: [src/commands/service/push.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/push.ts)_
695<!-- commandsstop -->
696
697# Configuration
698
699The Apollo CLI and VS Code extension can be configured with an Apollo config file. Apollo configuration is stored as a plain object in an `apollo.config.js` file which exports the configuration. For more information about configuring an Apollo project, see: https://www.apollographql.com/docs/devtools/apollo-config/.
700
701> Note: the use of the `apollo` key in the project's package.json file for configuration is deprecated, and will no longer be supported in Apollo v3
702
703You'll need to set up your Apollo configuration for all the features of the Apollo CLI and VS Code extension to work correctly. For full details on how to do that, [visit our docs](https://www.apollographql.com/docs/devtools/apollo-config/). A basic configuration (`apollo.config.js` style) looks something like this:
704
705```js
706module.exports = {
707 client: {
708 name: "My Client Project",
709 service: "my-service-name"
710 }
711};
712```
713
714# Code Generation
715
716## Accompanying Libraries
717
718See [Apollo iOS](https://github.com/apollographql/apollo-ios) for details on the mapping from GraphQL results to Swift types, as well as runtime support for executing queries and mutations. For Scala, see [React Apollo Scala.js](https://github.com/apollographql/react-apollo-scalajs) for details on how to use generated Scala code in a Scala.js app with Apollo Client.
719
720## `gql` template support
721
722If the source file for generation is a JavaScript or TypeScript file, the codegen will try to extrapolate the queries inside the [gql tag](https://github.com/apollographql/graphql-tag) templates.
723
724The tag name is configurable using the CLI `--tagName` option.
725
726## Typescript and Flow
727
728When using the codegen command with Typescript or Flow, make sure to add the `__typename` introspection field to every selection set within your graphql operations.
729
730If you're using a client like `apollo-client` that does this automatically for your GraphQL operations, pass in the `--addTypename` option to `apollo codegen:generate` to make sure the generated Typescript and Flow types have the `__typename` field as well. This is required to ensure proper type generation support for `GraphQLUnionType` and `GraphQLInterfaceType` fields.
731
732## Why is the \_\_typename field required?
733
734Using the type information from the GraphQL schema, we can infer the possible types for fields. However, in the case of a `GraphQLUnionType` or `GraphQLInterfaceType`, there are multiple types that are possible for that field. This is best modeled using a disjoint union with the `__typename`
735as the discriminant.
736
737For example, given a schema:
738
739```graphql
740...
741
742interface Character {
743 name: String!
744}
745
746type Human implements Character {
747 homePlanet: String
748}
749
750type Droid implements Character {
751 primaryFunction: String
752}
753
754...
755```
756
757Whenever a field of type `Character` is encountered, it could be either a Human or Droid. Human and Droid objects
758will have a different set of fields. Within your application code, when interacting with a `Character` you'll want to make sure to handle both of these cases.
759
760Given this query:
761
762```graphql
763query Characters {
764 characters(episode: NEW_HOPE) {
765 name
766
767 ... on Human {
768 homePlanet
769 }
770
771 ... on Droid {
772 primaryFunction
773 }
774 }
775}
776```
777
778Apollo Codegen will generate a union type for Character.
779
780```javascript
781export type CharactersQuery = {
782 characters: Array<
783 | {
784 __typename: "Human",
785 name: string,
786 homePlanet: ?string
787 }
788 | {
789 __typename: "Droid",
790 name: string,
791 primaryFunction: ?string
792 }
793 >
794};
795```
796
797This type can then be used as follows to ensure that all possible types are handled:
798
799```javascript
800function CharacterFigures({ characters }: CharactersQuery) {
801 return characters.map(character => {
802 switch (character.__typename) {
803 case "Human":
804 return (
805 <HumanFigure
806 homePlanet={character.homePlanet}
807 name={character.name}
808 />
809 );
810 case "Droid":
811 return (
812 <DroidFigure
813 primaryFunction={character.primaryFunction}
814 name={character.name}
815 />
816 );
817 }
818 });
819}
820```
821
822# Contributing
823
824[![Build Status](https://circleci.com/gh/apollographql/apollo-tooling.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-tooling)
825
826This repo is composed of multiple packages managed by Lerna. The `apollo-cli` contains the core CLI commands. The `apollo-codegen-core` package contains all the compiler APIs needed to implement code generation support for new languages. The other `apollo-codegen-*` packages implement code generation support for individual languages.
827
828Running tests locally:
829
830```
831npm install
832npm test
833```
834
835You can also run `npm` commands within package folders after you have bootstrapped the repository (part of `npm install`).
836
837> Note: if you have issues, try `npm run clean && npm i` to get a fresh install of the packages. Occasionally problems arise when removed dependencies stay around
838
839## Nock tests
840
841To display the debugging messages for nock, run the following command:
842
843```bash
844DEBUG=nock.* npm test
845```
846
847It can also be helpful to print standard out during testing. To enable logging, add the following configuration to the `stdout` function call during test creation:
848
849```
850.stdout({ print: true })
851```
852
853## Active Development / Debugging
854
855To simplify the development process, you may want to step through and debug commands whose behavior you're modifying. To do this, run the executable with node in debug mode like so, where `<command>` is a valid CLI command like `client:check` or `service:push`:
856
857```
858node --inspect-brk=9002 packages/apollo/bin/run <command>
859```
860
861If you're using VS Code, you can run the included "Attach to CLI Debugger" launch task and debug right from VS Code! Otherwise, you may use the Chrome inspector or [other Node debugger](https://nodejs.org/en/docs/guides/debugging-getting-started/) of your choice.
862
863## Regenerating Mocked Network Data
864
865Some integration tests rely on mocked server data (service:check for example). Mock data is generated by making real network requests and recording those requests with [`nock`'s recording feature](https://github.com/nock/nock#recording). Stop mocking network calls and add `nock.recorder.rec()` before network calls are made. For `service:check`, change `apiKey` to a real Apollo API key. Then run the tests and nock will output code to mock requests to the console. You can (and probably should) pare down the request to be less brittle (by only checking for an operation name, for example). See [`src/commands/service/__tests__/check.test.ts`](./packages/apollo/src/commands/service/__tests__/check.test.ts) for an example of how a mocked network request will look.
866
867
868## Publishing
869
8701. Make sure the `CHANGELOG.md` is updated with all changes committed since the last release. Make sure the versions for each package to update are correct, and there's a blank `Upcoming` section for future work.
8712. Run `npm run release:version-bump -- <bumpish>`
872 - Can use major, minor, patch, prepatch, etc for the bump type. If not used, the command will prompt and ask for the bump type.
873 - This command updates git tags locally and on GitHub
8743. Run npm run release:start-ci-publish locally
875 - Pushes a `publish/XXXXXXXXXX` tag to GitHub to trigger circle CI build
8764. Circle will notify the appropriate Apollo team slack channel when ready to release
877 - Slack channel member will confirm through the Apollo Deploy Bot
878 - Circle will release to all channels (npm, cli binary to s3, vscode marketplace)
879 - Another slack bot notification will confirm success of release.
880
881## Maintainers
882
883- [@JakeDawkins](https://github.com/jakedawkins) (Apollo)