I'm getting data from a database through GraphQL. There are two types: Group
and Person
. Groups have a field people
which is a list of Person
objects.
I'm trying to get a schema from the server using GraphQL's built-in introspection. The problem I have is that the people
field is a non-nullable type wrapping a list-type wrapping a non-nullable type, and I have to use this wordy query:
{
__type(name: "Group") {
name
fields {
name
type {
name
kind
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
ofType {
kind
name
}
}
}
}
}
}
}
}
To get this schema:
{
"data": {
"__type": {
"name": "Group",
"fields": [
{
"name": "people",
"type": {
"name": null,
"kind": "NON_NULL",
"ofType": {
"kind": "LIST",
"name": null,
"ofType": {
"kind": "NON_NULL",
"name": null,
"ofType": {
"kind": "OBJECT",
"name": "Person",
"ofType": null
}
}
}
}
}
]
}
}
}
In addition to being an inconvenient and difficult to read query, it's not generic, and I would have to know the max-depth of wrapper types in a schema to construct it.
Is there any way to get all wrapper types, regardless of depth, in a schema?
Unfortunately, there is no way to do that generically. There is no max depth because GraphQL supports wrapping a type with a List an arbitrary amount of times. So while commonly you will only see
[String!]!
this is also a valid type
[[[[[[[[[[[[String]!]!]!]!]!]!]!]!]!]!]!]!
You pretty much have to pick a reasonable depth and roll with it. For what it's worth, this is what the official "complete" introspection query looks like. It has a depth of seven.