【问题标题】:GraphQL is not self documentingGraphQL 不是自我记录的
【发布时间】:2021-05-27 15:55:57
【问题描述】:

我已经为我正在使用 webonyx/graphql-php 处理的项目创建了一个 GraphQL API。我正在使用 Altair Chrome 插件对其进行测试。 API 本身运行良好,我可以毫无问题地获取数据,但是 Altair 没有显示任何类型的 API 文档。当我点击重新加载按钮时,我可以在网络选项卡中看到 Altair 请求一个 IntrospectionQuery 并且我的服务器确实回复了一个大的 JSON 文件,但是,Altair 中没有出现任何文档。因为我在使用其他 GraphQL API 时确实有文档,所以我很确定问题出在我这边,但我不知道我做错了什么。

找到随附的 JSON(因大小限制而减少):

{
  "code": 200,
  "status": "success",
  "message": 0,
  "data": {
    "data": {
      "__schema": {
        "queryType": {
          "name": "Query"
        },
        "mutationType": null,
        "subscriptionType": null,
        "types": [
          {
            "kind": "OBJECT",
            "name": "Query",
            "description": "Base Query Object",
            "fields": [
              {
                "name": "senders",
                "description": "A list of senders",
                "args": [
                  {
                    "name": "offset",
                    "description": "Offset from where to look",
                    "type": {
                      "kind": "SCALAR",
                      "name": "Int",
                      "ofType": null
                    },
                    "defaultValue": "0"
                  },
                  {
                    "name": "limit",
                    "description": "Limit the number of senders (max 50)",
                    "type": {
                      "kind": "SCALAR",
                      "name": "Int",
                      "ofType": null
                    },
                    "defaultValue": "50"
                  },
                  {
                    "name": "fsk",
                    "description": "Agerating (either 12 or 16)",
                    "type": {
                      "kind": "SCALAR",
                      "name": "Int",
                      "ofType": null
                    },
                    "defaultValue": "16"
                  }
                ],
                "type": {
                  "kind": "LIST",
                  "name": null,
                  "ofType": {
                    "kind": "OBJECT",
                    "name": "sender",
                    "ofType": null
                  }
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "sender",
                "description": "A single sender by ID or Name",
                "args": [
                  {
                    "name": "id",
                    "description": null,
                    "type": {
                      "kind": "SCALAR",
                      "name": "Int",
                      "ofType": null
                    },
                    "defaultValue": null
                  },
                  {
                    "name": "name",
                    "description": "Get a sender by name",
                    "type": {
                      "kind": "SCALAR",
                      "name": "String",
                      "ofType": null
                    },
                    "defaultValue": null
                  }
                ],
                "type": {
                  "kind": "OBJECT",
                  "name": "sender",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              }
            ],
            "inputFields": null,
            "interfaces": [],
            "enumValues": null,
            "possibleTypes": null
          },
          {
            "kind": "OBJECT",
            "name": "sender",
            "description": "A single sender",
            "fields": [
              {
                "name": "id",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "Int",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "name",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "sexuality",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "country",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "sexualPreferences",
                "description": null,
                "args": [],
                "type": {
                  "kind": "LIST",
                  "name": null,
                  "ofType": {
                    "kind": "SCALAR",
                    "name": "String",
                    "ofType": null
                  }
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "age",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "Int",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "status",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "score",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "Int",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "ddi",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "slogan",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "zip",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "relationship",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "occupation",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "height",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "Int",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "hairLength",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "hairColor",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "languages",
                "description": null,
                "args": [],
                "type": {
                  "kind": "LIST",
                  "name": null,
                  "ofType": {
                    "kind": "SCALAR",
                    "name": "String",
                    "ofType": null
                  }
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "gender",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "figure",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "breastSize",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "description",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "preview12",
                "description": null,
                "args": [],
                "type": {
                  "kind": "OBJECT",
                  "name": "Image",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "preview16",
                "description": null,
                "args": [],
                "type": {
                  "kind": "OBJECT",
                  "name": "Image",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "images",
                "description": null,
                "args": [],
                "type": {
                  "kind": "LIST",
                  "name": null,
                  "ofType": {
                    "kind": "OBJECT",
                    "name": "Image",
                    "ofType": null
                  }
                },
                "isDeprecated": false,
                "deprecationReason": null
              }
            ],
            "inputFields": null,
            "interfaces": [],
            "enumValues": null,
            "possibleTypes": null
          },
          {
            "kind": "OBJECT",
            "name": "Image",
            "description": "A sender image",
            "fields": [
              {
                "name": "id",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "Int",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "fsk",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "Int",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "type",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "String",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              },
              {
                "name": "urls",
                "description": null,
                "args": [],
                "type": {
                  "kind": "SCALAR",
                  "name": "SourceSet",
                  "ofType": null
                },
                "isDeprecated": false,
                "deprecationReason": null
              }
            ],
            "inputFields": null,
            "interfaces": [],
            "enumValues": null,
            "possibleTypes": null
          },
          {
            "kind": "SCALAR",
            "name": "SourceSet",
            "description": null,
            "fields": null,
            "inputFields": null,
            "interfaces": null,
            "enumValues": null,
            "possibleTypes": null
          },
        ],
      }
    }
  }
}

【问题讨论】:

  • 当我为我的 PureScript GraphQL 实现构建模式自省时,我使用的是 GraphQL Playground。 Playground 让您打开 devtools,我通常会在那里看到 JavaScript 或 GraphQL 错误。就像“无法读取 null 的属性 x”。然后我会知道缺少哪个字段。也许你至少可以在 Playground 上试一试,看看它是否在那里工作。

标签: php graphql self-documenting


【解决方案1】:

经过大量搜索后,解决方案非常简单 - 我的响应对象将数据包装成 JSON 格式,因此它以 data->data->__schema 的形式出现,它应该是 data->__schema。

在输出解决问题之前简单地打开它。

【讨论】:

    猜你喜欢
    • 2017-01-23
    • 1970-01-01
    • 2018-06-18
    • 1970-01-01
    • 2018-06-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-18
    • 2016-03-11
    相关资源
    最近更新 更多