zyzhang
zyzhang

Reputation: 75

How could I get schema.json of https://example.com/graphql using apollo-tooling

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?

Upvotes: 3

Views: 3617

Answers (2)

Martin Zeitler
Martin Zeitler

Reputation: 76799

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

Upvotes: 1

itabdullah
itabdullah

Reputation: 607

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.

Source

Upvotes: 4

Related Questions