Perestrelo
Perestrelo

Reputation: 233

GraphQL when using Introspection some fields dont have type name?

I´m learning GraphQL and for my use case, I need to know which field types each ObjectType has.

But when trying, some of them don´t show the type. Here is the query i´m doing:

{
 __type(name: "Todo"){
  fields{
    name
    type{
      name   
  
    }
  }
}

here is the result:

{
  "data": {
    "__type": {
      "fields": [
        {
          "name": "priority",
          "type": {
            "name": "Int"
          }
        },
        {
          "name": "_id",
          "type": {
            "name": null
          }
        },
        {
          "name": "completed",
          "type": {
            "name": "Boolean"
          }
        },
        {
          "name": "title",
          "type": {
            "name": null
          }
        },
        {
          "name": "list",
          "type": {
            "name": "List"
          }
        },
        {
          "name": "_ts",
          "type": {
            "name": null
          }
        }
      ]
    }
  }
}

Here is the schema definition

type Todo {
  priority: Int
  _id: ID!
  completed: Boolean
  title: String!
  list: List
  _ts: Long!
}

as you can see in the result, _id, title, and _ts don't show the type. I should be missing something.

Thanks in advance.

Upvotes: 4

Views: 950

Answers (1)

Herku
Herku

Reputation: 7666

List types and non-null types don't have names. Instead, you have to query the kind property and ofType to get more info:

query {
  __type(name: "Todo") {
    fields {
      name
      type {
        name
        kind
        ofType {
          name
        }
      }
    }
  }
}

This will result in something like this:

{
  "data": {
    "__type": {
      "fields": [
        {
          "name": "priority",
          "type": {
            "name": "Int",
            "kind": "SCALAR",
            "ofType": null
          }
        },
        ...
        {
          "name": "title",
          "type": {
            "name": null,
            "kind": "NON_NULL",
            "ofType": {
              "name": "String"
            }
          }
        },
        ...
      ]
    }
  }
}

Note that this can be serveral levels deep as you can have a non-nullable list of a non-nullable type. Therefore you might even want to go four levels deep:

query {
  __type(name: "Resource") {
    fields {
      name
      type {
        name
        kind
        ofType {
          name
          kind
          ofType {
            name
            kind
            ofType {
              name
              kind
            }
          }
        }
      }
    }
  }
}

Upvotes: 2

Related Questions