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.
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
            }
          }
        }
      }
    }
  }
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With