Open In App

Introspection in GraphQL

Last Updated : 26 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

Introspection in GraphQL is a powerful feature that allows users to understand and query the structure of a GraphQL schema. It enables developers to dynamically explore the available object types, mutations, queries, and fields supported by the server.

Introspection plays a crucial role in GraphQL development, providing valuable insights into the schema’s composition and facilitating tools like GraphQL IDEs to offer enhanced features such as autocomplete suggestions and schema browsing. In this article, We will learn about Introspection in GraphQL along with its working and types.

What is Introspection in GraphQL?

  • Introspection is a valuable feature in GraphQL.
  • It allows users to understand the GraphQL schema’s response type or structure.
  • Introspection enables querying the GraphQL schema to identify supported object types, mutations, queries, and fields.
  • Through introspection, users can determine the number of object types, fields, and mutations supported by the server.
  • Introspection is essential for documentation purposes, as it provides insight into the available schema and its components.
  • It enables tools like GraphQL IDEs (Integrated Development Environments) to offer autocomplete suggestions and schema browsing features.

How Does Introspection Work?

  • GraphQL introspection is typically enabled by default in most GraphQL servers.
  • When an introspection query is executed, the server analyzes its structure and metadata at runtime.
  • Metadata includes information such as fields, mutations, and object types.
  • Introspection allows the server to analyze its entire API code and provide the response structure for the requested GraphQL query.
  • This feature is useful for dynamically understanding and querying the schema, making it easier to work with GraphQL APIs.
  • GraphQL clients can use introspection to dynamically construct queries based on the server’s schema, reducing the need for manually written query strings.

Types of Introspection Query

There are different types of Introspection Query to fetch the details of types, fields, mutations, and so on. Introspection Query starts with double underscore “__“. Let us see the types of Introspection Query in GraphQL are as follows:

  1. __schema Introspection query
  2. __type Introspection query
  3. __typename Introspection Query

1. __schema Introspection query

This __schema introspection query is helpful in fetching the available query types, fields, mutations and so on. It is a special field used in fetching the response from different APIs.

Example:

Let’s have an input __schema query to fetch all available types.

Input:

{
__schema
{
types{
name
}
}
}

The above input will fetch the information of all available types in the Graphql schema.

Output:

"data" : {
"__schema" : {
"types" : [
{
"name" : "Int"
},
{
"name" : "String"
},
{
"name" : "Student"
},
{
"name" : "Teacher"
},
{
"name" : "__Type"
},
{
"name" : "__Field"
},
]
}
}

Explanation:

  • the name String and Int are Scalar type which is actually in-built type specific to assign the data type to the field.
  • the name Student and Teacher are name of the object type. These two are referred as Object.
  • the name __Type and __Field are actually one of the parts of introspection query to fetch the schema structure or response.

queryType introspection using __schema

If we specifically input the queryType inside the __schema then it will fetch only the queryTypes in the available Graphql schema. It will not fetch other schema types such as mutation, object types etc.

Example:

Let’s create an input schema to fetch only the query types available in Graphql schemas.

Input:

{
__schema {
queryType {
name
}
}
}

Output:

"data" : {
"__schema" : {
"queryType" : [
{
"name" : "StudentQuery"
},
{
"name" : "TeacherQuery"
},
]
}
}

Explanation:

  • Here, StudentQuery and TeacherQuery are name of the queries available in Graphql Schema.
  • Like queryType, you can also fetch the details of other types such as mutationType , subscriptionTypes, directives and so on.

2. __type query in introspection

If we just want to retrieve about the specific object or other types , then we can follow the below way using __type query. It helps in retrieving the schema related to the __type query only. Here, it will look only for __type query and ignore other metadata such as mutations etc.,

For Example, if we want to query about the specific object then we will have to pass the name of the object to __type query.

Input:

The below input query will fetch the details of the Student Object.

__type ( name : "Student")
{
name
kind
}

Output:

{
"data" : {
"__type" : {
"name" : "Student"
"kind" : "Object" //type of Student
}
}
}

Explanation: It fetches the schema of Student Object from API. Here, it shows the name of the object and kind of the Student.

3. __typename Query in Introspection

The __typename is used to return the name of the object that is being queried. If we mention the __typename in any of the query response then it will display the object type of that particular response. It will help the developers to know what object type is being used for the response.

Example:

The below input has __typename inside query. The __typename will fetch the object or interface or others name that’s being queried.

Input:

query {
user(login: xyz)
{
__typename
}

Output:

"data" : {
"user" : {
__typename : "Student"
}
}

The above input query has queried using the Student object.

Benefits of Introspection

Let us discuss the benefits of introspection in Graphql.

  • It helps the developers to know about the metadata schema type, query structure, mutations type, fields and so on.
  • Even though if the application has complex APIs, this introspection makes it simple to display all the requested schema type.
  • It provides simple automated API documentation for all the requested graphql schema type.
  • With help of introspection, we could easily see and analyse the errors in Graphql structure.
  • Graphql introspection is automatically enabled in most of the Graphql servers. We don’t need to manually trigger it many case.
  • With help of Introspection, we can identify the inconsistent schema. Developers can build or modify the existing query in a more accurate way.

Conclusion

Overall, introspection is a fundamental aspect of GraphQL that empowers developers to interact with schemas dynamically. By enabling users to query schema details at runtime, introspection simplifies API documentation, aids in error analysis, and enhances the development process. With introspection, developers can efficiently work with GraphQL APIs, ensuring accurate and consistent data queries and manipulations.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads