__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.
Introspection in GraphQL
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.