0
0
GraphQLquery~5 mins

Schema testing in GraphQL

Choose your learning style9 modes available
Introduction

Schema testing helps make sure your GraphQL API is set up correctly. It checks that the types and fields match what you expect.

When you create a new GraphQL API and want to confirm the schema is correct.
When you add new types or fields and want to verify they work as planned.
When you want to catch mistakes early before clients use your API.
When you update your API and want to ensure nothing broke.
When you want to document your API structure clearly.
Syntax
GraphQL
query IntrospectionQuery {
  __schema {
    types {
      name
      kind
      fields {
        name
        type {
          name
          kind
        }
      }
    }
  }
}
This query asks the GraphQL server to describe its schema.
You can use tools like Apollo or GraphiQL to run this query.
Examples
This example fetches the name of the root query type in the schema.
GraphQL
query {
  __schema {
    queryType {
      name
    }
  }
}
This example fetches details about the "User" type and its fields.
GraphQL
query {
  __type(name: "User") {
    name
    fields {
      name
      type {
        name
        kind
      }
    }
  }
}
Sample Program

This query checks the "Book" type in the schema, listing its fields and their types.

GraphQL
query {
  __type(name: "Book") {
    name
    fields {
      name
      type {
        name
        kind
      }
    }
  }
}
OutputSuccess
Important Notes

Schema testing helps catch errors before clients use your API.

Use introspection queries to explore your schema anytime.

Keep your schema documentation updated as you change your API.

Summary

Schema testing checks your GraphQL API structure.

Use introspection queries to see types and fields.

It helps keep your API reliable and clear.