I am currently writing a third-party client for a website, but it doesn't expose interface, so I try to crawl datas by myself. The website uses GraphQL, so I use apollo-android in my project, By reading README.md of apollo-CLI, I still have trouble in generating schema.json file. Could you tell me the detailed steps of how to generate schema.json?
Asked
Active
Viewed 3,350 times
2 Answers
3
For schema.json you should have apollo-codegen which is used to send an introspection query to server and get schema.json.
For getting apollo-codegen execute following from the command-prompt to install it:
npm install apollo-codegen -g
For sending the introspection query and getting the schema.json execute following:
apollo-codegen download-schema https://api.github.com/graphql --output schema.json
Replace https://api.github.com/graphql with your link
You can then find the schema.json file saved to the folder from where you ran the above commands.

d-feverx
- 1,424
- 3
- 16
- 31

itabdullah
- 605
- 1
- 8
- 22
-
The question states " but it doesn't expose interface", in the case that the endpoint does not support responding to queries about its own structure then the OP will need to follow the techniques outlined here https://blog.apollographql.com/three-ways-to-represent-your-graphql-schema-a41f4175100d – Nigel Savage Nov 22 '19 at 00:35
1
apollo-codegen
had meanwhile be replaced with apollo
:
The 'apollo-codegen' command has been replaced with the more-powerful 'apollo' CLI. Switch to 'apollo' to ensure future updates and visit https://npm.im/apollo#code-generation for more information.
So this would be:
sudo npm install apollo -g
And it's options:
$ apollo client:download-schema --help
Download a schema from Apollo or a GraphQL endpoint in JSON or SDL format
USAGE
$ apollo client:download-schema OUTPUT
ARGUMENTS
OUTPUT [default: schema.json] Path to write the introspection result to. Can be `.graphql`, `.gql`, `.graphqls`, or `.json`
OPTIONS
-c, --config=config Path to your Apollo config file
-g, --graph=graph The ID for the graph in Apollo to operate client commands with. Overrides config file if set.
-v, --variant=variant The variant of the graph in Apollo to associate this client to
--clientName=clientName Name of the client that the queries will be attached to
--clientReferenceId=clientReferenceId Reference id for the client which will match ids from client traces, will use clientName if not provided
--clientVersion=clientVersion The version of the client that the queries will be attached to
--endpoint=endpoint The URL for the CLI use to introspect your service
--excludes=excludes Glob of files to exclude for GraphQL operations. Caveat: this doesn't currently work in watch mode
--header=header Additional header to send during introspection. May be used multiple times to add multiple headers. NOTE: The `--endpoint` flag is REQUIRED if using the `--header` flag.
--includes=includes Glob of files to search for GraphQL operations. This should be used to find queries *and* any client schema extensions
--key=key The API key to use for authentication to Apollo
--queries=queries Deprecated in favor of the includes flag
--tagName=tagName Name of the template literal tag used to identify template literals containing GraphQL
queries in Javascript/Typescript code
For example:
apollo client:download-schema --endpoint=https://api.github.com/graphql schema.json

Martin Zeitler
- 1
- 19
- 155
- 216