我如何使用apollo-tooling获得https://example.com/graphql的schema.json

时间:2019-04-02 07:08:18

标签: graphql apollo-android

我目前正在为网站编写第三方客户端,但是它不公开界面,因此我尝试自己爬网数据。该网站使用GraphQL,因此我在项目中使用apollo-android。通过阅读apollo-CLI的README.md,在生成schema.json文件时仍然遇到困难。 您能告诉我如何生成schema.json的详细步骤吗?

2 个答案:

答案 0 :(得分:0)

对于 schema.json ,您应该具有apollo-codegen,该代码用于将自省查询发送到服务器并获取schema.json。

要获取apollo-codegen,请从命令提示符处执行以下操作以进行安装:

npm-install apollo-codegen

要发送自省查询并获取 schema.json ,请执行以下操作:

apollo-codegen下载模式https://api.github.com/graphql --output schema.json

用您的链接替换https://api.github.com/graphql

然后您可以找到 schema.json 文件,该文件保存在运行上述命令的文件夹中。

Source

答案 1 :(得分:0)

apollo-codegen同时已被apollo替代:

'apollo-codegen'命令已被功能更强大的'apollo'CLI取代。切换到“ apollo”以确保将来进行更新,并访问https://npm.im/apollo#code-generation以获取更多信息。

所以这将是:

sudo npm install apollo -g

它是选项:

$ 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

例如:

apollo client:download-schema --endpoint=https://api.github.com/graphql schema.json