【问题标题】:Get required/non-null field on introspection自省时获取必填/非空字段
【发布时间】:2019-12-01 16:20:42
【问题描述】:

我正在使用自省来查询 InputTypes 以一般地创建一个表单来更改实体。

query introspection($updateInputName: String!) {
  __type(name: $updateInputName) {
    inputFields {
      name
      type {
        name
        kind
        ofType {
          kind
          name
        }
      }
    }
  }
}

据我了解,type.kind 信息应该为必填/非空字段返回 NON_NULL。但我收到 SCALAR,...

如何获取查询的inputField的必填字段信息?

【问题讨论】:

    标签: graphql


    【解决方案1】:

    如果字段不可为空,kind 字段确实会解析为 NON_NULL。如果您看到SCALAR,则该字段可以为空。

    以下是每种包装器类型组合的自省结果:

    Int

    "type": {
      "name": "Int",
      "kind": "SCALAR",
      "ofType": null
    }
    

    Int!

    "type": {
      "name": null,
      "kind": "NON_NULL",
      "ofType": {
        "name": "Int",
        "kind": "SCALAR",
        "ofType": null
      }
    }
    

    [Int!]

    "type": {
      "name": null,
      "kind": "LIST",
      "ofType": {
        "name": null,
        "kind": "NON_NULL",
        "ofType": {
          "name": "Int",
          "kind": "SCALAR",
          "ofType": null
        }
      },
    
    }
    

    [Int!]!

    "type": {
      "name": null,
      "kind": "NON_NULL",
      "ofType": {
        "name": null,
        "kind": "LIST",
        "ofType": {
          "name": null,
          "kind": "NON_NULL",
          "ofType": {
            "name": "Int",
            "kind": "SCALAR",
            "ofType": null
          }
        },
      }
    }
    

    【讨论】:

    • 你是对的。我正在查看错误的架构类型并感到困惑。
    猜你喜欢
    • 2020-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-23
    • 2013-04-18
    • 1970-01-01
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多