【问题标题】:获取 GraphQL 整个架构查询
【发布时间】:2016-09-20 17:47:06
【问题描述】:

我想从服务器获取架构。 我可以获取具有类型的所有实体,但无法获取属性。

获取所有类型:

query {
  __schema {
    queryType {
      fields {
        name
        type {
          kind
          ofType {
            kind
            name
          }
        }
      }
    }
  }
}

如何获取类型的属性:

__type(name: "Person") {
    kind
    name
    fields {
      name
      type {
        kind
        name
        description
      }
    }
  }

如何仅在 1 个请求中获取具有属性的所有类型?或者更好:如何使用 mutators、enums、types 获得整个架构 ...

【问题讨论】:

  • 我结束了使用'graphql'中的introspectionQuery;如底部所述。没关系。

标签: schema graphql


【解决方案1】:

更新

现在推荐使用graphql-cli 来获取和更新架构。

以下命令将帮助您入门:

# install via NPM
npm install -g graphql-cli

# Setup your .graphqlconfig file (configure endpoints + schema path)
graphql init

# Download the schema from the server
graphql get-schema

您甚至可以通过运行以下命令来监听架构更改并不断更新您的架构:

graphql get-schema --watch

如果您只想下载 GraphQL 架构,请使用以下方法:

获取 GraphQL 架构的最简单方法是使用 CLI 工具 get-graphql-schema

你可以通过 NPM 安装它:

npm install -g get-graphql-schema

有两种方法可以获取您的架构。 1)GraphQL IDL 格式或 2)JSON 自省查询格式。

GraphQL IDL 格式

get-graphql-schema ENDPOINT_URL > schema.graphql

JSON 内省格式

get-graphql-schema ENDPOINT_URL --json > schema.json

get-graphql-schema ENDPOINT_URL -j > schema.json

更多信息可以参考以下教程:How to download the GraphQL IDL Schema

【讨论】:

  • 标记为解决方案的答案无法通过我尝试使用的 GraphQL 服务器实现,但该库完全符合生成完整模式所需的功能。它可能应该被标记为解决方案。
  • --json 应该放在 > 之前
  • @Catharz:问题没有说明 OP 不想使用 Node 或 JavaScript。此外,这个答案不需要使用 JavaScript libraries;它提供了一个命令行工具,恰好是用 JavaScript编写
  • 我按照步骤操作,graphql get-schema 没有写入 schema.graphql 文件。但是,它会输出到屏幕上。我不知道为什么。
  • 这个答案实际上已经过时了 - 请参阅 github.com/Urigo/graphql-cli/blob/master/docs/… 重新标头,您可以在您的 gql 配置中执行 schema: { YOUR/ENDPOINT: { headers: {Authorization: "Token your_token"}}}
【解决方案2】:

这是GraphiQL 使用的查询(网络捕获):

query IntrospectionQuery {
  __schema {
    queryType {
      name
    }
    mutationType {
      name
    }
    subscriptionType {
      name
    }
    types {
      ...FullType
    }
    directives {
      name
      description
      locations
      args {
        ...InputValue
      }
    }
  }
}

fragment FullType on __Type {
  kind
  name
  description
  fields(includeDeprecated: true) {
    name
    description
    args {
      ...InputValue
    }
    type {
      ...TypeRef
    }
    isDeprecated
    deprecationReason
  }
  inputFields {
    ...InputValue
  }
  interfaces {
    ...TypeRef
  }
  enumValues(includeDeprecated: true) {
    name
    description
    isDeprecated
    deprecationReason
  }
  possibleTypes {
    ...TypeRef
  }
}

fragment InputValue on __InputValue {
  name
  description
  type {
    ...TypeRef
  }
  defaultValue
}

fragment TypeRef on __Type {
  kind
  name
  ofType {
    kind
    name
    ofType {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
              }
            }
          }
        }
      }
    }
  }
}

【讨论】:

  • Postwoman 使用相同的查询,但 UI 仅在您单击“获取架构”按钮时显示相关类型。
【解决方案3】:

您可以使用 GraphQL-JS 的自省查询来获取您想了解的有关架构的所有信息:

import { introspectionQuery } from 'graphql';

如果你只想要类型的信息,你可以使用这个:

{
    __schema: {
        types: {
            ...fullType
        }
    }
}

使用自省查询中的以下片段:

fragment FullType on __Type {
    kind
    name
    description
    fields(includeDeprecated: true) {
      name
      description
      args {
        ...InputValue
      }
      type {
        ...TypeRef
      }
      isDeprecated
      deprecationReason
    }
    inputFields {
      ...InputValue
    }
    interfaces {
      ...TypeRef
    }
    enumValues(includeDeprecated: true) {
      name
      description
      isDeprecated
      deprecationReason
    }
    possibleTypes {
      ...TypeRef
    }
  }
  fragment InputValue on __InputValue {
    name
    description
    type { ...TypeRef }
    defaultValue
  }
  fragment TypeRef on __Type {
    kind
    name
    ofType {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                }
              }
            }
          }
        }
      }
    }
  }
`;

如果这看起来很复杂,那是因为字段可以任意深度包装在非空值和列表中,这意味着从技术上讲,如果您的字段被包装在 7 层以上(这可能不是),即使上面的查询也不能反映完整的架构t 案例)。

你可以查看introspectionQueryhere的源代码。

【讨论】:

  • 这应该在顶部!
  • 在较新版本的 graphql 中,现在这是一个名为 getIntrospectionQuery() 的函数
【解决方案4】:

使用apollo cli

npx apollo schema:download --endpoint=http://localhost:4000/graphql schema.json

【讨论】:

【解决方案5】:

您可以使用 Hasura 的 graphqurl 实用程序

npm install -g graphqurl

gq <endpoint> --introspect > schema.graphql

# or if you want it in json
gq <endpoint> --introspect --format json > schema.json

完整文档:https://github.com/hasura/graphqurl

【讨论】:

    【解决方案6】:

    您可以使用以下命令下载远程 GraphQL 服务器的架构。命令成功后,您应该会在当前工作目录中看到一个名为 schema.json 的新文件。

    ~$ npx apollo-cli download-schema $GRAPHQL_URL --output schema.json

    【讨论】:

      【解决方案7】:

      你可以使用 IntelliJ 插件JS GraphQL 然后 IDEA 会要求你创建两个文件“graphql.config.json”和“graphql.schema.json”

      然后您可以编辑“graphql.config.json”以指向您的本地或远程 GraphQL 服务器:

      "schema": {
      "README_request" : "To request the schema from a url instead, remove the 'file' JSON property above (and optionally delete the default graphql.schema.json file).",
      "request": {
        "url" : "http://localhost:4000",
        "method" : "POST",
        "README_postIntrospectionQuery" : "Whether to POST an introspectionQuery to the url. If the url always returns the schema JSON, set to false and consider using GET",
        "postIntrospectionQuery" : true,
        "README_options" : "See the 'Options' section at https://github.com/then/then-request",
        "options" : {
          "headers": {
            "user-agent" : "JS GraphQL"
          }
        }
      }
      

      之后,IDEA 插件将自动从 GraphQL 服务器加载架构并在控制台中显示架构 json,如下所示:

      Loaded schema from 'http://localhost:4000': {"data":{"__schema":{"queryType":{"name":"Query"},"mutationType":{"name":"Mutation"},"subscriptionType":null,"types":[{"kind":"OBJECT","name":"Query","description":"","fields":[{"name":"launche
      

      【讨论】:

      • 这个插件的第 2 版即将推出(现在是测试版:github.com/jimkyndemeyer/js-graphql-intellij-plugin/releases/…)。不是graphql.config.json 文件,而是.graphqlconfig 文件。您必须将字段 schemaPath 设置为不存在的文件(它将在下载架构后自动创建)并将 url 设置为 GraphQL 远程服务器。接下来,在“Schemas and project structure”选项卡中(在“GraphQL”选项卡中),双击选定的“Endpoint”并单击“Get GraphQL Schema from Endpoint (introspection)”。在前面提到的文件中将下载架构。
      【解决方案8】:

      您可以使用GraphQL-Codegen with the ast-plugin

      npm install --save graphql
      npm install --save-dev @graphql-codegen/cli
      npx graphql-codegen init
      

      按照步骤生成codegen.yml文件

      安装工具后,您可以使用插件下载架构schema-ast

      最好按照页面上的说明进行安装……但基本上:

      npm install --save-dev @graphql-codegen/schema-ast

      然后配置codegen.yml 文件以设置哪些模式是/是事实来源以及将下载的模式文件放在哪里:

      schema:
        - 'http://localhost:3000/graphql'
      generates:
        path/to/file.graphql:
          plugins:
            - schema-ast
          config:
            includeDirectives: true
      

      【讨论】:

        【解决方案9】:

        不知何故,我无法获得任何建议的 CLI 工具来以 GraphQL 的模式定义语言 (SDL) 而非自省结果 JSON 输出模式。我最终拼凑了一个非常快速的 Node 脚本,让 GraphQL 库为我做这件事:

        const fs = require("fs");
        const { buildClientSchema, getIntrospectionQuery, printSchema } = require("graphql");
        const fetch = require("node-fetch");
        
        async function saveSchema(endpoint, filename) {
            const response = await fetch(endpoint, {
                method: "POST",
                headers: { "Content-Type": "application/json" },
                body: JSON.stringify({ query: getIntrospectionQuery() })
            });
            const graphqlSchemaObj = buildClientSchema((await response.json()).data);
            const sdlString = printSchema(graphqlSchemaObj);
            fs.writeFileSync(filename, sdlString);
        }
        
        saveSchema("https://example.com/graphql", "schema.graphql");
        

        getIntrospectionQuery() 具有获取所有内容所需的完整自省查询,然后 buildClientSchema()printSchema() 将 JSON 混乱转换为 GraphQL SDL。

        把它变成一个 CLI 工具本身并不难,但感觉有点矫枉过正。

        【讨论】:

          【解决方案10】:

          我也在找这个Medium article on GraphQL

          以下查询返回了有关架构、查询及其输入和输出参数类型的许多详细信息。

          fragment FullType on __Type {
            kind
            name
            fields(includeDeprecated: true) {
              name
              args {
                ...InputValue
              }
              type {
                ...TypeRef
              }
              isDeprecated
              deprecationReason
            }
            inputFields {
              ...InputValue
            }
            interfaces {
              ...TypeRef
            }
            enumValues(includeDeprecated: true) {
              name
              isDeprecated
              deprecationReason
            }
            possibleTypes {
              ...TypeRef
            }
          }
          fragment InputValue on __InputValue {
            name
            type {
              ...TypeRef
            }
            defaultValue
          }
          fragment TypeRef on __Type {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                  ofType {
                    kind
                    name
                    ofType {
                      kind
                      name
                      ofType {
                        kind
                        name
                        ofType {
                          kind
                          name
                        }
                      }
                    }
                  }
                }
              }
            }
          }
          query IntrospectionQuery {
            __schema {
              queryType {
                name
              }
              mutationType {
                name
              }
              types {
                ...FullType
              }
              directives {
                name
                locations
                args {
                  ...InputValue
                }
              }
            }
          }
          
          

          【讨论】:

            【解决方案11】:

            参考https://stackoverflow.com/a/42010467/10189759

            想指出,如果需要身份验证,您可能不能只使用从graphql init生成的配置文件

            您可能必须执行类似的操作,例如,使用 github graphql API

            {
              "projects": {
                "graphqlProjectTestingGraphql": {
                  "schemaPath": "schema.graphql",
                  "extensions": {
                    "endpoints": {
                      "dev": {
                        "url": "https://api.github.com/graphql",
                        "headers": {
                          "Authorization": "Bearer <Your token here>"
                        }
                      }
                    }
                  }
                }
              }
            }
            

            【讨论】:

              【解决方案12】:

              如果你想自己做,请阅读以下代码:

              有一个模块化的最先进的工具「graphql-cli」,考虑看看它。它使用包「graphql」的 buildClientSchema 从自省数据构建 IDL .graphql 文件。

              【解决方案13】:

              graphql npm 包的 IntrospectionQuery does

              query IntrospectionQuery {
                  __schema {
                      queryType {
                          name
                      }
                      mutationType {
                          name
                      }
                      subscriptionType {
                          name
                      }
                      types {
                          ...FullType
                      }
                      directives {
                          name
                          description
              
                          locations
                          args {
                              ...InputValue
                          }
                      }
                  }
              }
              
              fragment FullType on __Type {
                  kind
                  name
                  description
              
                  fields(includeDeprecated: true) {
                      name
                      description
                      args {
                          ...InputValue
                      }
                      type {
                          ...TypeRef
                      }
                      isDeprecated
                      deprecationReason
                  }
                  inputFields {
                      ...InputValue
                  }
                  interfaces {
                      ...TypeRef
                  }
                  enumValues(includeDeprecated: true) {
                      name
                      description
                      isDeprecated
                      deprecationReason
                  }
                  possibleTypes {
                      ...TypeRef
                  }
              }
              
              fragment InputValue on __InputValue {
                  name
                  description
                  type {
                      ...TypeRef
                  }
                  defaultValue
              }
              
              fragment TypeRef on __Type {
                  kind
                  name
                  ofType {
                      kind
                      name
                      ofType {
                          kind
                          name
                          ofType {
                              kind
                              name
                              ofType {
                                  kind
                                  name
                                  ofType {
                                      kind
                                      name
                                      ofType {
                                          kind
                                          name
                                          ofType {
                                              kind
                                              name
                                          }
                                      }
                                  }
                              }
                          }
                      }
                  }
              }
              

              source

              【讨论】:

                【解决方案14】:

                【讨论】:

                  猜你喜欢
                  • 2022-08-20
                  • 2018-04-07
                  • 2020-01-20
                  • 2019-05-11
                  • 2018-12-07
                  • 1970-01-01
                  • 2020-02-16
                  • 2017-11-10
                  • 2021-05-18
                  相关资源
                  最近更新 更多