跳到主要内容

深度洞察:GraphQL To 内省JSON — Numio

使用 @graphql-codegen/introspection 将 GraphQL SDL 转换为标准 introspection 查询的 JSON 结果。输出包含顶层 __schema 对象,其中有根类型、包括内置类型在内的所有类型以及指令。

为需要 introspection JSON 而非 SDL 的工具(如部分 IDE 插件和客户端生成器)生成 schema.json。

GraphQL Schema

Introspection JSON

适用场景

  • 为需要 introspection JSON 而非 SDL 的工具(如部分 IDE 插件和客户端生成器)生成 schema.json。
  • 查看 introspection 数据中字段类型如何被 NON_NULL 和 LIST kind 包装。

使用方法

  1. 将 schema SDL 粘贴到 GraphQL Schema 编辑器。
  2. 等待 Introspection JSON 面板中出现 JSON;即使只有一个字段的 schema 也会生成约一千行。
  3. 复制或下载 JSON 并保存,例如保存为 schema.json。

GraphQL schema

type Query {
  hello(name: String): String!
}

Introspection JSON

{
  "__schema": {
    "queryType": {
      "name": "Query"
    },
    "mutationType": null,
    "subscriptionType": null,
    "types": [
      {
        "kind": "OBJECT",
        "name": "Query",
        "description": null,
        "fields": [
          {
            "name": "hello",
            "description": null,
            "args": [
              {
                "name": "name",
                "description": null,
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
…

功能与限制

内置标量(String、Boolean)、introspection 类型(__Schema、__Type、__Field 等)以及内置指令(include、skip、deprecated、specifiedBy)总会包含在内。

JSON 没有包裹在 "data" 键中,而是直接以 __schema 开头。

转换在浏览器的 Web Worker 中运行。本页面只接受 schema,不会查询线上 endpoint。

FAQ

可以从 URL 获取 introspection JSON 吗?

不可以。页面只转换你粘贴的 SDL,不会向服务器发送 introspection 查询。请先从服务器导出 SDL,再粘贴到这里。

会包含已废弃的字段吗?

会。每个字段和枚举值都带有 isDeprecated 和 deprecationReason,因此标记了 @deprecated 的字段会以 isDeprecated: true 出现。

为什么小 schema 的输出也这么大?

introspection 结果描述的是整个类型系统,包括内置标量、introspection 元类型和指令定义,而不仅仅是你自己的类型。

规范与参考资料