【问题标题】:Get selection of non required field on introspection在自省中选择非必填字段
【发布时间】:2020-04-06 03:21:47
【问题描述】:

我在 JS 中将 GraphQL 与 Apollo 服务器和客户端一起使用,并尝试自省我的架构。

简化后我有一个类似的架构:

input LocationInput {
  lat: Float
  lon: Float
}

input CreateCityInput {
  name: String!
  location: LocationInput
}

我用这样的内省来查询这个:

fragment InputTypeRef on __Type {
  kind
  name
  ofType {
    kind
    name
    inputFields {
      name
      type {
        name
        kind
        ofType {
          kind
          name
          inputFields {
            name
            type {
              name
              kind
            }
          }
        }
      }
    }
  }
}

query CreateCityInputFields {
  input: __type(name: "CreateCityInput") {
    inputFields {
      name
      description
      type {
        ...InputTypeRef
      }
    }
  }
}

结果我收到:

{
  "data": {
    "input": {
      "inputFields": [
        {
          "name": "name",
          "description": "",
          "type": {
            "kind": "NON_NULL",
            "name": null,
            "ofType": {
              "kind": "SCALAR",
              "name": "String",
              "inputFields": null
            }
          }
        },
        {
          "name": "location",
          "description": "",
          "type": {
            "kind": "INPUT_OBJECT",
            "name": "LocationInput",
            "ofType": null
          }
        }
      ]
    }
  }
}

如您所见:latlon 不见了。如果我在CreateCityInput 中根据需要设置LocationInput (location: LocationInput!),我会收到缺少的latlon

如何查询latlon 而不需要LocationInput

【问题讨论】:

    标签: graphql introspection


    【解决方案1】:

    似乎我对自省的查询有误。将inputField 部分从ofType 移出到上层解决了这个问题:

      kind
      name
      inputFields {
        name
        description
        type {
          kind
          name
          ofType {
            kind
            name
          }
        }
      }
      ofType {
        kind
        name
        inputFields {
          name
          description
          type {
            kind
            name
            ofType {
              kind
              name
            }
          }
        }
      }
    }
    
    query CreateCityInputFields {
      input: __type(name: "CreateCityInput") {
        inputFields {
          name
          description
          type {
            ...InputTypeRef
          }
        }
      }
    }
    

    解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多