// Tutorial //

Introspection Queries With GraphQL

Published on April 23, 2017
Default avatar
By Alligator.io
Developer and author at DigitalOcean.
Introspection Queries With GraphQL

With GraphQL you can run introspection queries to learn about the available fields and types of a GraphQL schema. That introspection capability is also what gives GraphiQL the ability to provide documentation about the schema and autocompletion.

Let’s use the Star Wars SWAPI public API endpoint to run a few introspection queries against.

__type

First let’s run a query to enquire about the Film type using the built-in __type:

query FilmType {
  __type(name: "Film") {
    kind
    name
    fields {
      name
      description
      type {
        name
      }
    }
  }
}
  • Kind gives us the enum value for the type, like OBJECT, SCALAR or INTERFACE.
  • Name gives us the name of the type.
  • Description, well, gives us the description!

And here’s what the response looks like:

{
  "data": {
    "__type": {
      "kind": "OBJECT",
      "name": "Film",
      "fields": [
        {
          "name": "title",
          "description": "The title of this film.",
          "type": {
            "name": "String"
          }
        },
        {
          "name": "episodeID",
          "description": "The episode number of this film.",
          "type": {
            "name": "Int"
          }
        },
        ...

Notice the use of the built-in __type (of type __Type) here to get information on the type of a particular object or interface.


Here’s another example using a fragment to learn even more about a particular type:

query LearnAboutFilm {
  __type(name: "Film") {
    ...AboutType
  }
}

fragment AboutType on __Type {
  fields {
    name
    description
    args {
      name
      description
    }
  }
  interfaces {
    name
    description
  }
  inputFields {
    name
    description
  }
  possibleTypes {
    kind
    name
    fields {
      name
      description
      type {
        kind
        name
        description
      }
    }
  }
}

__schema

With __schema we can ask the server about the schema itself. Let’s look at an example:

query LearnAboutSchema {
  __schema {
    types {
      name
      kind
    }
    queryType {
      fields {
        name
        description
      }
    }
  }
}

And the response:

{
  "data": {
    "__schema": {
      "types": [
        {
          "name": "Root",
          "kind": "OBJECT"
        },
        {
          "name": "String",
          "kind": "SCALAR"
        },
        {
          "name": "Int",
          "kind": "SCALAR"
        },
        ...
        "queryType": {
        "fields": [
          {
            "name": "allFilms",
            "description": null
          },
          {
            "name": "film",
            "description": null
          },
          ...

__typename

__typename can be used as part of regular queries to enquire about the type of a particular field:

query LearnAboutFilm {
  allFilms {
    films {
      __typename
      title
    }
  }
  film (id: "ZmlsbXM6Mw==") {
    __typename
    title
  }
  starship(id: "c3RhcnNoaXBzOjc1") {
    __typename
    name
    model
  }
}

And here’s the response:

{
  "data": {
    "allFilms": {
      "films": [
        {
          "__typename": "Film",
          "title": "A New Hope"
        },
        {
          "__typename": "Film",
          "title": "The Empire Strikes Back"
        },
        ...
      ]
    },
    "film": {
      "__typename": "Film",
      "title": "Return of the Jedi"
    },
    "starship": {
      "__typename": "Starship",
      "name": "V-wing",
      "model": "Alpha-3 Nimbus-class V-wing starfighter"
    }
  }
}

Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up
About the authors
Default avatar
Developer and author at DigitalOcean.

Still looking for an answer?

Was this helpful?
Leave a comment