获取GraphQL整个架构查询

Ale*_*nko 53 schema graphql

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

获得所有类型:

query {
  __schema {
    queryType {
      fields {
        name
        type {
          kind
          ofType {
            kind
            name
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

如何获取类型的属性:

__type(name: "Person") {
    kind
    name
    fields {
      name
      type {
        kind
        name
        description
      }
    }
  }
Run Code Online (Sandbox Code Playgroud)

如何只在1个请求中获取具有属性的所有类型?或者更好:我如何通过mutators,enums,types获得整个架构......

sch*_*ing 72

更新

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
Run Code Online (Sandbox Code Playgroud)

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

graphql get-schema --watch
Run Code Online (Sandbox Code Playgroud)

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

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

您可以通过NPM安装它:

npm install -g get-graphql-schema
Run Code Online (Sandbox Code Playgroud)

有两种方法可以获取架构.1)GraphQL IDL格式或2)JSON内省查询格式.

GraphQL IDL格式

get-graphql-schema ENDPOINT_URL > schema.graphql
Run Code Online (Sandbox Code Playgroud)

JSON内省格式

get-graphql-schema --json ENDPOINT_URL > schema.json
Run Code Online (Sandbox Code Playgroud)

要么

get-graphql-schema -j ENDPOINT_URL > schema.json
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅以下教程:如何下载GraphQL IDL架构

  • 标记为该解决方案的答案无法使用我尝试使用的 GraphQL 服务器来实现,但该库完全满足了生成完整模式所需的要求。它可能应该标记为解决方案。 (2认同)
  • --json应该在>之前 (2认同)
  • @Catharz:在任何地方都没有问题表明OP不想使用Node或JavaScript。此外,此答案不需要使用JavaScript _libraries_;它提供了一个命令行工具,碰巧是用JavaScript编写的。 (2认同)
  • 这个答案实际上已经过时了 - 请参阅https://github.com/Urigo/graphql-cli/blob/master/docs/MIGRATION.md#get-schema-is-no-longer-available 重新标头,您在gql 配置你可以做 `schema: { YOUR/ENDPOINT: { headers: {Authorization: "Token your_token"}}}` (2认同)

小智 40

这是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
              }
            }
          }
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)


jkm*_*ale 12

更新

在厌倦了一直修改以前的脚本之后,我屈服并制作了自己的 CLI 工具gql-sdl。我仍然找不到一种可以零配置下载 GraphQL SDL 的不同工具,但希望有一个工具存在。

基本用法:

$ gql-sdl https://api.github.com/graphql -H "Authorization: Bearer ghp_[redacted]"
directive @requiredCapabilities(requiredCapabilities: [String!]) on OBJECT | SCALAR | ARGUMENT_DEFINITION | INTERFACE | INPUT_OBJECT | FIELD_DEFINITION | ENUM | ENUM_VALUE | UNION | INPUT_FIELD_DEFINITION

"""Autogenerated input type of AbortQueuedMigrations"""
input AbortQueuedMigrationsInput {
  """The ID of the organization that is running the migrations."""
  ownerId: ID!

  """A unique identifier for the client performing the mutation."""
  clientMutationId: String
}
...
Run Code Online (Sandbox Code Playgroud)

标头参数-H在技术上是可选的,但大多数 GraphQL API 需要通过标头进行身份验证。您还可以下载 JSON 响应 ( --json),但这是其他工具已经很好服务的用例。

在底层,这仍然使用GraphQL.js提供的内省查询,因此如果您希望将此功能合并到您自己的代码中,请参阅下面的示例。


之前的回答

不知何故,我无法获得任何建议的 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");
Run Code Online (Sandbox Code Playgroud)

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

将其制作成 CLI 工具本身并不太困难,但这感觉有点矫枉过正。

  • `gql-sdl` 可以很好地完成工作,无需预先配置,谢谢!用法示例:`gql-sdl http://localhost:4000/graphql -o schema-auto generated.graphql -H \"授权:持有者 <id token>"` (2认同)

hel*_*fer 10

您可以使用GraphQL-JS的内省查询来获取有关模式的所有信息:

import { introspectionQuery } from 'graphql';
Run Code Online (Sandbox Code Playgroud)

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

{
    __schema: {
        types: {
            ...fullType
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

其中使用了内省查询中的以下片段:

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
                }
              }
            }
          }
        }
      }
    }
  }
`;
Run Code Online (Sandbox Code Playgroud)

如果这看起来很复杂,那是因为字段可以被任意深度包含在nonNulls和Lists中,这意味着从技术上来说,即使上面的查询也不能反映完整的模式,如果你的字段包含超过7层(可能不是这种情况) ).

您可以在此处查看introspectionQuery的源代码.

  • 在较新版本的“graphql”中,现在是一个名为“getIntrospectionQuery()”的函数 (2认同)

has*_*s19 10

使用阿波罗cli

npx apollo schema:download --endpoint=http://localhost:4000/graphql schema.json
Run Code Online (Sandbox Code Playgroud)

  • 使用 npx apollo 客户端:download-schema --endpoint=http://localhost:4000/graphql schema.json (2认同)

Fla*_*ken 6

您可以将GraphQL-Codegen 与 ast-plugin 一起使用

\n
npm install --save graphql\nnpm install --save-dev @graphql-codegen/cli\nnpx graphql-codegen init\n
Run Code Online (Sandbox Code Playgroud)\n

按照步骤生成codegen.yml文件

\n

安装该工具后,您可以使用该插件下载模式,即schema-ast

\n

最好是按照页面上的说明安装它\xe2\x80\xa6,但基本上:

\n

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

\n

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

\n
schema:\n  - \'http://localhost:3000/graphql\'\ngenerates:\n  path/to/file.graphql:\n    plugins:\n      - schema-ast\n    config:\n      includeDirectives: true\n
Run Code Online (Sandbox Code Playgroud)\n


Far*_*han 6

我也在寻找并发现了这篇有关 GraphQL 的 Medium 文章

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

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
      }
    }
  }
}

Run Code Online (Sandbox Code Playgroud)


Wil*_*III 5

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

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


abu*_*ick 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
Run Code Online (Sandbox Code Playgroud)

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