UNPKG

35.5 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.22.0 linux-x64 node-v10.18.1
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 -t, --tag=tag The published service tag for this client
65 --clientName=clientName Name of the client that the queries will be attached to
66
67 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will use
68 clientName if not provided
69
70 --clientVersion=clientVersion The version of the client that the queries will be attached to
71
72 --endpoint=endpoint The url of your service
73
74 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently
75 work in watch mode
76
77 --header=header Additional header to send to server for introspectionQuery. May be used
78 multiple times to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED
79 if using the `--header` flag.
80
81 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
82 queries *and* any client schema extensions
83
84 --key=key The API key for the Apollo Engine service
85
86 --queries=queries Deprecated in favor of the includes flag
87
88 --tagName=tagName Name of the template literal tag used to identify template literals containing
89 GraphQL queries in Javascript/Typescript code
90```
91
92_See code: [src/commands/client/check.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/check.ts)_
93
94## `apollo client:codegen [OUTPUT]`
95
96Generate static types for GraphQL queries. Can use the published schema in Apollo Graph Manager or a downloaded schema.
97
98```
99USAGE
100 $ apollo client:codegen [OUTPUT]
101
102ARGUMENTS
103 OUTPUT
104 Directory to which generated files will be written.
105 - For TypeScript/Flow generators, this specifies a directory relative to each source file by default.
106 - For TypeScript/Flow generators with the "outputFlat" flag is set, and for the Swift generator, this specifies a
107 file or directory (absolute or relative to the current working directory) to which:
108 - a file will be written for each query (if "output" is a directory)
109 - all generated types will be written
110 - For all other types, this defines a file (absolute or relative to the current working directory) to which all
111 generated types are written.
112
113OPTIONS
114 -c, --config=config Path to your Apollo config file
115 -t, --tag=tag The published service tag for this client
116
117 --[no-]addTypename [default: true] Automatically add __typename to your queries, can be unset
118 with --no-addTypename
119
120 --clientName=clientName Name of the client that the queries will be attached to
121
122 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will
123 use clientName if not provided
124
125 --clientVersion=clientVersion The version of the client that the queries will be attached to
126
127 --customScalarsPrefix=customScalarsPrefix Include a prefix when using provided types for custom scalars
128
129 --endpoint=endpoint The url of your service
130
131 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't
132 currently work in watch mode
133
134 --globalTypesFile=globalTypesFile By default, TypeScript will put a file named "globalTypes.ts" inside the
135 "output" directory. Set "globalTypesFile" to specify a different path.
136 Alternatively, set "tsFileExtension" to modify the extension of the file,
137 for example "d.ts" will output "globalTypes.d.ts"
138
139 --header=header Additional header to send to server for introspectionQuery. May be used
140 multiple times to add multiple headers. NOTE: The `--endpoint` flag is
141 REQUIRED if using the `--header` flag.
142
143 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
144 queries *and* any client schema extensions
145
146 --key=key The API key for the Apollo Engine service
147
148 --localSchemaFile=localSchemaFile Path to one or more local GraphQL schema file(s), as introspection result
149 or SDL. Supports comma-separated list of paths (ex.
150 `--localSchemaFile=schema.graphql,extensions.graphql`)
151
152 --mergeInFieldsFromFragmentSpreads Merge fragment fields onto its enclosing type
153
154 --namespace=namespace The namespace to emit generated code into.
155
156 --omitDeprecatedEnumCases Omit deprecated enum cases from generated code [Swift only]
157
158 --only=only Parse all input files, but only output generated code for the specified
159 file [Swift only]
160
161 --operationIdsPath=operationIdsPath Path to an operation id JSON map file. If specified, also stores the
162 operation ids (hashes) as properties on operation types [currently
163 Swift-only]
164
165 --outputFlat By default, TypeScript/Flow will put each generated file in a directory
166 next to its source file using the value of the "output" as the directory
167 name. Set "outputFlat" to put all generated files in the directory relative
168 to the current working directory defined by "output".
169
170 --passthroughCustomScalars Use your own types for custom scalars
171
172 --queries=queries Deprecated in favor of the includes flag
173
174 --suppressSwiftMultilineStringLiterals Prevents operations from being rendered as multiline strings [Swift only]
175
176 --tagName=tagName Name of the template literal tag used to identify template literals
177 containing GraphQL queries in Javascript/Typescript code
178
179 --target=target (required) Type of code generator to use (swift | typescript | flow |
180 scala)
181
182 --tsFileExtension=tsFileExtension By default, TypeScript will output "ts" files. Set "tsFileExtension" to
183 specify a different file extension, for example "d.ts"
184
185 --useFlowExactObjects Use Flow exact objects for generated types [flow only]
186
187 --useFlowReadOnlyTypes Use read only types for generated types [flow only]. **Deprecated in favor
188 of `useReadOnlyTypes`.**
189
190 --useReadOnlyTypes Use read only types for generated types [flow | typescript]
191
192 --watch Watch for file changes and reload codegen
193
194ALIASES
195 $ apollo codegen:generate
196```
197
198_See code: [src/commands/client/codegen.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/codegen.ts)_
199
200## `apollo client:download-schema OUTPUT`
201
202Download a schema from Apollo Graph Manager or a GraphQL endpoint in JSON or SDL format
203
204```
205USAGE
206 $ apollo client:download-schema OUTPUT
207
208ARGUMENTS
209 OUTPUT [default: schema.json] Path to write the introspection result to. Can be `.graphql`, `.gql`, `.graphqls`, or
210 `.json`
211
212OPTIONS
213 -c, --config=config Path to your Apollo config file
214 -t, --tag=tag The published service tag for this client
215 --clientName=clientName Name of the client that the queries will be attached to
216
217 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will use
218 clientName if not provided
219
220 --clientVersion=clientVersion The version of the client that the queries will be attached to
221
222 --endpoint=endpoint The url of your service
223
224 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently
225 work in watch mode
226
227 --header=header Additional header to send to server for introspectionQuery. May be used
228 multiple times to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED
229 if using the `--header` flag.
230
231 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
232 queries *and* any client schema extensions
233
234 --key=key The API key for the Apollo Engine service
235
236 --queries=queries Deprecated in favor of the includes flag
237
238 --tagName=tagName Name of the template literal tag used to identify template literals containing
239 GraphQL queries in Javascript/Typescript code
240```
241
242_See code: [src/commands/client/download-schema.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/download-schema.ts)_
243
244## `apollo client:extract OUTPUT`
245
246Extract queries from a client
247
248```
249USAGE
250 $ apollo client:extract OUTPUT
251
252ARGUMENTS
253 OUTPUT [default: manifest.json] Path to write the extracted queries to
254
255OPTIONS
256 -c, --config=config Path to your Apollo config file
257 -t, --tag=tag The published service tag for this client
258 --clientName=clientName Name of the client that the queries will be attached to
259
260 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will use
261 clientName if not provided
262
263 --clientVersion=clientVersion The version of the client that the queries will be attached to
264
265 --endpoint=endpoint The url of your service
266
267 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently
268 work in watch mode
269
270 --header=header Additional header to send to server for introspectionQuery. May be used
271 multiple times to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED
272 if using the `--header` flag.
273
274 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
275 queries *and* any client schema extensions
276
277 --key=key The API key for the Apollo Engine service
278
279 --preserveStringAndNumericLiterals Disable redaction of string and numerical literals. Without this flag, these
280 values will be replaced with empty strings (`''`) and zeroes (`0`)
281 respectively. This redaction is intended to avoid inadvertently outputting
282 potentially personally identifiable information (e.g. embedded passwords or
283 API keys) into operation manifests
284
285 --queries=queries Deprecated in favor of the includes flag
286
287 --tagName=tagName Name of the template literal tag used to identify template literals containing
288 GraphQL queries in Javascript/Typescript code
289```
290
291_See code: [src/commands/client/extract.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/extract.ts)_
292
293## `apollo client:push`
294
295Register operations with Apollo, adding them to the safelist
296
297```
298USAGE
299 $ apollo client:push
300
301OPTIONS
302 -c, --config=config Path to your Apollo config file
303 -t, --tag=tag The published service tag for this client
304 --clientName=clientName Name of the client that the queries will be attached to
305
306 --clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will use
307 clientName if not provided
308
309 --clientVersion=clientVersion The version of the client that the queries will be attached to
310
311 --endpoint=endpoint The url of your service
312
313 --excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently
314 work in watch mode
315
316 --header=header Additional header to send to server for introspectionQuery. May be used
317 multiple times to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED
318 if using the `--header` flag.
319
320 --includes=includes Glob of files to search for GraphQL operations. This should be used to find
321 queries *and* any client schema extensions
322
323 --key=key The API key for the Apollo Engine service
324
325 --queries=queries Deprecated in favor of the includes flag
326
327 --tagName=tagName Name of the template literal tag used to identify template literals containing
328 GraphQL queries in Javascript/Typescript code
329```
330
331_See code: [src/commands/client/push.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/client/push.ts)_
332
333## `apollo help [COMMAND]`
334
335display help for apollo
336
337```
338USAGE
339 $ apollo help [COMMAND]
340
341ARGUMENTS
342 COMMAND command to show help for
343
344OPTIONS
345 --all see all commands in CLI
346```
347
348_See code: [@oclif/plugin-help](https://github.com/oclif/plugin-help/blob/v2.2.3/src/commands/help.ts)_
349
350## `apollo plugins`
351
352list installed plugins
353
354```
355USAGE
356 $ apollo plugins
357
358OPTIONS
359 --core show core plugins
360
361EXAMPLE
362 $ apollo plugins
363```
364
365_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.7.9/src/commands/plugins/index.ts)_
366
367## `apollo plugins:install PLUGIN...`
368
369installs a plugin into the CLI
370
371```
372USAGE
373 $ apollo plugins:install PLUGIN...
374
375ARGUMENTS
376 PLUGIN plugin to install
377
378OPTIONS
379 -f, --force yarn install with force flag
380 -h, --help show CLI help
381 -v, --verbose
382
383DESCRIPTION
384 Can be installed from npm or a git url.
385
386 Installation of a user-installed plugin will override a core plugin.
387
388 e.g. If you have a core plugin that has a 'hello' command, installing a user-installed plugin with a 'hello' command
389 will override the core plugin implementation. This is useful if a user needs to update core plugin functionality in
390 the CLI without the need to patch and update the whole CLI.
391
392ALIASES
393 $ apollo plugins:add
394
395EXAMPLES
396 $ apollo plugins:install myplugin
397 $ apollo plugins:install https://github.com/someuser/someplugin
398 $ apollo plugins:install someuser/someplugin
399```
400
401_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.7.9/src/commands/plugins/install.ts)_
402
403## `apollo plugins:link PLUGIN`
404
405links a plugin into the CLI for development
406
407```
408USAGE
409 $ apollo plugins:link PLUGIN
410
411ARGUMENTS
412 PATH [default: .] path to plugin
413
414OPTIONS
415 -h, --help show CLI help
416 -v, --verbose
417
418DESCRIPTION
419 Installation of a linked plugin will override a user-installed or core plugin.
420
421 e.g. If you have a user-installed or core plugin that has a 'hello' command, installing a linked plugin with a 'hello'
422 command will override the user-installed or core plugin implementation. This is useful for development work.
423
424EXAMPLE
425 $ apollo plugins:link myplugin
426```
427
428_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.7.9/src/commands/plugins/link.ts)_
429
430## `apollo plugins:uninstall PLUGIN...`
431
432removes a plugin from the CLI
433
434```
435USAGE
436 $ apollo plugins:uninstall PLUGIN...
437
438ARGUMENTS
439 PLUGIN plugin to uninstall
440
441OPTIONS
442 -h, --help show CLI help
443 -v, --verbose
444
445ALIASES
446 $ apollo plugins:unlink
447 $ apollo plugins:remove
448```
449
450_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.7.9/src/commands/plugins/uninstall.ts)_
451
452## `apollo plugins:update`
453
454update installed plugins
455
456```
457USAGE
458 $ apollo plugins:update
459
460OPTIONS
461 -h, --help show CLI help
462 -v, --verbose
463```
464
465_See code: [@oclif/plugin-plugins](https://github.com/oclif/plugin-plugins/blob/v1.7.9/src/commands/plugins/update.ts)_
466
467## `apollo service:check`
468
469Check a service against known operation workloads to find breaking changes
470
471```
472USAGE
473 $ apollo service:check
474
475OPTIONS
476 -c, --config=config Path to your Apollo config file
477 -t, --tag=tag The published tag to check this service against
478 --endpoint=endpoint The url of your service
479
480 --header=header Additional header to send to server for
481 introspectionQuery. May be used multiple times to add
482 multiple headers. NOTE: The `--endpoint` flag is
483 REQUIRED if using the `--header` flag.
484
485 --json Output result in json, which can then be parsed by CLI
486 tools such as jq.
487
488 --key=key The API key for the Apollo Engine service
489
490 --localSchemaFile=localSchemaFile Path to one or more local GraphQL schema file(s), as
491 introspection result or SDL. Supports comma-separated
492 list of paths (ex.
493 `--localSchemaFile=schema.graphql,extensions.graphql`)
494
495 --markdown Output result in markdown.
496
497 --queryCountThreshold=queryCountThreshold Minimum number of requests within the requested time
498 window for a query to be considered.
499
500 --queryCountThresholdPercentage=queryCountThresholdPercentage Number of requests within the requested time window for
501 a query to be considered, relative to total request
502 count. Expected values are between 0 and 0.05 (minimum
503 5% of total request volume)
504
505 --serviceName=serviceName Provides the name of the implementing service for a
506 federated graph. This flag will indicate that the
507 schema is a partial schema from a federated service
508
509 --validationPeriod=validationPeriod The size of the time window with which to validate the
510 schema against. You may provide a number (in seconds),
511 or an ISO8601 format duration for more granularity
512 (see: https://en.wikipedia.org/wiki/ISO_8601#Durations)
513
514ALIASES
515 $ apollo schema:check
516```
517
518_See code: [src/commands/service/check.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/check.ts)_
519
520## `apollo service:delete`
521
522Delete a federated service from Apollo Graph Manager and recompose remaining services
523
524```
525USAGE
526 $ apollo service:delete
527
528OPTIONS
529 -c, --config=config Path to your Apollo config file
530 -t, --tag=tag The variant of the service to delete
531 --endpoint=endpoint The url of your service
532
533 --header=header Additional header to send to server for introspectionQuery. May be used multiple times to
534 add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag.
535
536 --key=key The API key for the Apollo Engine service
537
538 --serviceName=serviceName (required) Provides the name of the implementing service for a federated graph
539```
540
541_See code: [src/commands/service/delete.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/delete.ts)_
542
543## `apollo service:download OUTPUT`
544
545Download the schema from your GraphQL endpoint.
546
547```
548USAGE
549 $ apollo service:download OUTPUT
550
551ARGUMENTS
552 OUTPUT [default: schema.json] Path to write the introspection result to
553
554OPTIONS
555 -c, --config=config Path to your Apollo config file
556 -k, --skipSSLValidation Allow connections to an SSL site without certs
557 -t, --tag=tag [default: current] The published tag to check this service against
558 --endpoint=endpoint The url of your service
559
560 --header=header Additional header to send to server for introspectionQuery. May be used multiple times to add
561 multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag.
562
563 --key=key The API key for the Apollo Engine service
564
565ALIASES
566 $ apollo schema:download
567```
568
569_See code: [src/commands/service/download.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/download.ts)_
570
571## `apollo service:list`
572
573List the services in a graph
574
575```
576USAGE
577 $ apollo service:list
578
579OPTIONS
580 -c, --config=config Path to your Apollo config file
581 -t, --tag=tag The published tag to list the services from
582 --endpoint=endpoint The url of your service
583
584 --header=header Additional header to send to server for introspectionQuery. May be used multiple times to add
585 multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag.
586
587 --key=key The API key for the Apollo Engine service
588```
589
590_See code: [src/commands/service/list.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/list.ts)_
591
592## `apollo service:push`
593
594Push a service to Apollo Graph Manager
595
596```
597USAGE
598 $ apollo service:push
599
600OPTIONS
601 -c, --config=config Path to your Apollo config file
602 -t, --tag=tag [default: current] The tag to publish this service to
603 --endpoint=endpoint The url of your service
604
605 --header=header Additional header to send to server for introspectionQuery. May be used multiple
606 times to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the
607 `--header` flag.
608
609 --key=key The API key for the Apollo Engine service
610
611 --localSchemaFile=localSchemaFile Path to one or more local GraphQL schema file(s), as introspection result or SDL.
612 Supports comma-separated list of paths (ex.
613 `--localSchemaFile=schema.graphql,extensions.graphql`)
614
615 --serviceName=serviceName Provides the name of the implementing service for a federated graph
616
617 --serviceRevision=serviceRevision Provides a unique revision identifier for a change to an implementing service on a
618 federated service push. The default of this is a git sha
619
620 --serviceURL=serviceURL Provides the url to the location of the implementing service for a federated graph
621
622ALIASES
623 $ apollo schema:publish
624```
625
626_See code: [src/commands/service/push.ts](https://github.com/apollographql/apollo-tooling/blob/master/packages/apollo/src/commands/service/push.ts)_
627<!-- commandsstop -->
628
629# Configuration
630
631The 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/.
632
633> 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
634
635You'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:
636
637```js
638module.exports = {
639 client: {
640 name: "My Client Project",
641 service: "my-service-name"
642 }
643};
644```
645
646# Code Generation
647
648## Accompanying Libraries
649
650See [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.
651
652## `gql` template support
653
654If 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.
655
656The tag name is configurable using the CLI `--tagName` option.
657
658## Typescript and Flow
659
660When using the codegen command with Typescript or Flow, make sure to add the `__typename` introspection field to every selection set within your graphql operations.
661
662If 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.
663
664## Why is the \_\_typename field required?
665
666Using 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`
667as the discriminant.
668
669For example, given a schema:
670
671```graphql
672...
673
674interface Character {
675 name: String!
676}
677
678type Human implements Character {
679 homePlanet: String
680}
681
682type Droid implements Character {
683 primaryFunction: String
684}
685
686...
687```
688
689Whenever a field of type `Character` is encountered, it could be either a Human or Droid. Human and Droid objects
690will 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.
691
692Given this query:
693
694```graphql
695query Characters {
696 characters(episode: NEW_HOPE) {
697 name
698
699 ... on Human {
700 homePlanet
701 }
702
703 ... on Droid {
704 primaryFunction
705 }
706 }
707}
708```
709
710Apollo Codegen will generate a union type for Character.
711
712```javascript
713export type CharactersQuery = {
714 characters: Array<
715 | {
716 __typename: "Human",
717 name: string,
718 homePlanet: ?string
719 }
720 | {
721 __typename: "Droid",
722 name: string,
723 primaryFunction: ?string
724 }
725 >
726};
727```
728
729This type can then be used as follows to ensure that all possible types are handled:
730
731```javascript
732function CharacterFigures({ characters }: CharactersQuery) {
733 return characters.map(character => {
734 switch (character.__typename) {
735 case "Human":
736 return (
737 <HumanFigure
738 homePlanet={character.homePlanet}
739 name={character.name}
740 />
741 );
742 case "Droid":
743 return (
744 <DroidFigure
745 primaryFunction={character.primaryFunction}
746 name={character.name}
747 />
748 );
749 }
750 });
751}
752```
753
754# Contributing
755
756[![Build Status](https://circleci.com/gh/apollographql/apollo-tooling.svg?style=svg)](https://circleci.com/gh/apollographql/apollo-tooling)
757
758This 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.
759
760Running tests locally:
761
762```
763npm install
764npm test
765```
766
767You can also run `npm` commands within package folders after you have bootstrapped the repository (part of `npm install`).
768
769> 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
770
771## Nock tests
772
773To display the debugging messages for nock, run the following command:
774
775```bash
776DEBUG=nock.* npm test
777```
778
779It 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:
780
781```
782.stdout({ print: true })
783```
784
785## Active Development / Debugging
786
787To 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`:
788
789```
790node --inspect-brk=9002 packages/apollo/bin/run <command>
791```
792
793If 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.
794
795## Regenerating Mocked Network Data
796
797Some 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 engine 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.
798
799
800## Publishing
801
8021. 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.
8032. Run `npm run release:version-bump -- <bumpish>`
804 - Can use major, minor, patch, prepatch, etc for the bump type. If not used, the command will prompt and ask for the bump type.
805 - This command updates git tags locally and on GitHub
8063. Run npm run release:start-ci-publish locally
807 - Pushes a `publish/XXXXXXXXXX` tag to GitHub to trigger circle CI build
8084. Circle will notify the appropriate Apollo team slack channel when ready to release
809 - Slack channel member will confirm through the Apollo Deploy Bot
810 - Circle will release to all channels (npm, cli binary to s3, vscode marketplace)
811 - Another slack bot notification will confirm success of release.
812
813## Maintainers
814
815- [@JakeDawkins](https://github.com/jakedawkins) (Apollo)
816- [@jbaxleyiii](https://github.com/jbaxleyiii) (Apollo)