【问题标题】:How could I get schema.json of https://example.com/graphql using apollo-tooling如何使用 apollo-tooling 获取 https://example.com/graphql 的 schema.json
【发布时间】:2020-10-18 16:20:21
【问题描述】:

我目前正在为一个网站写一个第三方客户端,但是它没有暴露接口,所以我尝试自己爬取数据。该网站使用GraphQL,所以我在我的项目中使用apollo-android,通过阅读apollo-CLI的README.md,我仍然无法生成schema.json文件。 能否告诉我如何生成schema.json的详细步骤?

【问题讨论】:

    标签: graphql apollo-android


    【解决方案1】:

    对于 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 文件保存到您运行上述命令的文件夹中。

    Source

    【讨论】:

    • 问题陈述“但它不公开接口”,如果端点不支持响应关于其自身结构的查询,那么 OP 将需要遵循此处概述的技术@987654323 @
    【解决方案2】:

    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
    

    【讨论】:

    • 注意:apollo 目前不适用于GraphQl v > 15。
    猜你喜欢
    • 2019-09-01
    • 2018-04-06
    • 2020-11-12
    • 2016-02-04
    • 2017-10-05
    • 2020-09-11
    • 2019-07-19
    • 2018-04-25
    • 2020-09-13
    相关资源
    最近更新 更多