Graphql 获取内省时非必填字段的选择
我在JS中将GraphQL与Apollo服务器和客户端一起使用,并尝试反思我的模式 我有一个类似的模式:Graphql 获取内省时非必填字段的选择,graphql,introspection,Graphql,Introspection,我在JS中将GraphQL与Apollo服务器和客户端一起使用,并尝试反思我的模式 我有一个类似的模式: input LocationInput { lat: Float lon: Float } input CreateCityInput { name: String! location: LocationInput } 我用一种自省的方式对此提出质疑,比如: fragment InputTypeRef on __Type { kind name ofType
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
}
}
]
}
}
}
可以看到:lat
和lon
缺失。如果我在CreateCityInput
中根据需要设置LocationInput
(location:LocationInput!
),我将收到缺少的lat
和lon
如果不需要haven
LocationInput
,我如何查询lat
和lon
?似乎我对内省的查询有误。将输入字段
部分从类型的中移到更高级别可以解决此问题:
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
}
}
}
}
解决了这个问题