Alexander Molodykh

Explore GraphQL using introspection queries

Sometimes you need quickly check GraphQL contract and no other tools available. Sometimes you build a script that automatically grabs available resources. Here is the set of useful GraphQL introspection quieries.

GraphQL services support introspection queries. Unfortunatelly, on production environments introspections usually blocked for sequrity reasons. But I found Deuche Bahn one if you don't have your own to play with https://bahnql.herokuapp.com/graphql

Insomnia template for below examples

Check the list of possible queries and mutations.

{
  __schema {
    queryType {
      name
      fields{
        name
      }
    }

    mutationType{
      name
      fields{
        name
      }
    }
  }
}

Or simplified version where common code moved into fragment.

{
  __schema {
    queryType {
      ...TypeFields
    }

    mutationType {                                           
      ...TypeFields
    }
  }
}

fragment TypeFields on __Type
{
  name
  fields {
    name
  }   
}

For Deuche Bahn service I got response below. I would not present all the responses - you can download insomnia app and try by yourself.

{
	"data": {
		"__schema": {
			"queryType": {
				"name": "Query",
				"fields": [
					{
						"name": "routing"
					},
					{
						"name": "stationWithEvaId"
					},
					{
						"name": "stationWithStationNumber"
					},
					{
						"name": "stationWithRill100"
					},
					{
						"name": "search"
					},
					{
						"name": "nearby"
					},
					{
						"name": "parkingSpace"
					}
				]
			},
			"mutationType": null
		}
	}
}

We can see there some queries but no mutations (mutationType: null).
Keep in mind, some nasty services would throw error for such query (when there are no mutations). Then just remove `mutationType` piece.

Good services have also documentation. Just add description field into previous fragment.

fragment TypeFields on __Type
{
  name
  fields {
    name
    description
  }   
}

Keep in mind, depreacted functions are not shown by default. But we can access them and check the reason it is deprecated.

fragment TypeFields on __Type
{
  name
  fields {
    name
    isDeprecated
    deprecationReason
  }   
}

Check the list of possible input parameters.

{
  __schema {
    queryType {
      ...TypeFields
    }

    mutationType {                                           
      ...TypeFields
    }
  }
}

fragment TypeFields on __Type
{
  name
  fields {
    name
    args {
      name
      type {
        name
      }
    defaultValue
    }
  }
}

Check the list of possible output parameters.

{
  __schema {
    queryType {
      ...TypeFields
    }

    mutationType {                                           
      ...TypeFields
    }
  }
}

fragment TypeFields on __Type
{
  name
  fields {
    name
    type {
      ofType {
        name
      }
    }
  }   
}

Check complex types.

Finally, explore the types. The argument is not always a simple string on number. It is often some complex data structures.

{
  __schema {
    queryType {
      ...TypeFields
    }

    mutationType {                                           
      ...TypeFields
    }
  }
}

fragment TypeFields on __Type
{
  name
  fields {
    name
    type {
      ofType {
        name
      }
    }
  }   
}

It was just overview of basic GraphQL cases. In real life you can have more interesting cases with inheritance, unions, enums.
Use insomnia code compleat functionality to explore them all.

All rights reserved.