我目前正在为一个网站写一个第三方客户端,但是它没有暴露接口,所以我尝试自己爬取数据。该网站使用 GraphQL,所以我在项目中使用了apollo-android,通过阅读apollo-CLI的 README.md ,我仍然无法生成 schema.json 文件。你能告诉我如何生成schema.json的详细步骤吗?
问问题
2962 次
2 回答
3
对于schema.json,您应该有 apollo-codegen,它用于向服务器发送自省查询并获取 schema.json。
要获取 apollo-codegen,请从命令提示符执行以下命令以安装它:
npm install apollo-codegen -g
要发送自省查询并获取schema.json ,请执行以下操作:
apollo-codegen download-schema https://api.github.com/graphql --output schema.json
将https://api.github.com/graphql替换为您的链接
然后,您可以找到保存到运行上述命令的文件夹中的schema.json文件。
于 2019-10-18T05:01:07.103 回答
1
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
于 2020-10-18T08:06:24.937 回答