进行内省时选择非必填字段

法比安

我在JS中将GraphQL与Apollo Server和Client一起使用,并尝试自省我的架构。

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

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丢失了。如果我LocationInput按照要求(location: LocationInput!)进行设置,则会CreateCityInput收到缺少的latlon

我如何可以查询latlon没有避风港LocationInput要求?

法比安

好像我对内省的查询有误。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
      }
    }
  }
}

解决了问题。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章