在内省时选择非必填字段

Get selection of non required field on introspection

我在 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

看来我对内省的查询有误。将 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
      }
    }
  }
}

问题已解决。