GitHub API是否为架构查询返回无效结果?

时间:2018-11-08 17:11:42

标签: graphql

https://facebook.github.io/graphql/draft/#sec-Schema-Introspection

type __Schema {
  types: [__Type!]!
  queryType: __Type!
  mutationType: __Type
  subscriptionType: __Type
  directives: [__Directive!]!
}

type __Type {
  kind: __TypeKind!
  name: String
  description: String
...

https://developer.github.com/v4/guides/intro-to-graphql/#discovering-the-graphql-apicurl -H "Authorization: bearer token" https://api.github.com/graphql)下载的信息

(文件的开头

{
  "data": {
    "__schema": {
      "queryType": {
        "name": "Query"
      },
      "mutationType": {
        "name": "Mutation"
      },
      "subscriptionType": null,
      "types": [
        {
          "kind": "SCALAR",
          "name": "Boolean",
...

问题:

我对此进行了解释,因为GitHub模式结果无效,因为queryType未指定kindnonNullable

这个结果是否违反了架构规则?或者我缺少什么?

1 个答案:

答案 0 :(得分:1)

此响应通过验证,因为缺少的字段与返回null的字段不同。只有首先没有要求时,响应中才会缺少该字段。

如果您访问GitHub的GraphQL Explorer,则可以自己生成一个自省查询,请求kind字段作为queryType字段选择集的一部分,它将返回该字段带有非空值。

{
  __schema {
    queryType {
      kind
    }
  }
}

响应:

{
  "data": {
    "__schema": {
      "queryType": {
        "kind": "OBJECT"
      }
    }
  }
}

通过向某个端点发出GET请求来获取模式很方便,但这不是反思模式的标准方法。相反,您应该使用需要针对端点本身的任何选择集的请求。这个问题使以这种不太传统的方式进行操作的缺点变得显而易见。在这种情况下,GitHub自发进行的自省查询都会丢失一个或多个原本可能需要的字段。因为您不是进行内省查询的人,所以您不知道期望响应的形式如何。

相关问题