What is Introspection in GraphQL?

What is Introspection in GraphQL

Why is Introspection Useful?

Provides clients a deep view of the schema

Build Awesome Tools

Self Documentation

Introspection Queries

To demonstrate and learn GraphQL introspection queries, I am going to use the GitHub API that is available to the public. You can follow along by opening https://developer.github.com/v4/explorer/. Make sure you are signed in to your GitHub account to run the introspection queries.

On the GitHub GraphQL explorer, we can start typing our queries on the left side and hit play to see the JSON response on the right side. We can also browse through the documentation of the API on the right side.

Query schema’s type

query getSchmemaTypes {
__schema {
types {
name
}
}
}

The JSON response to this query is quite lengthy from the GitHub schema, below is the snapshot of the response.

{
"data": {
"__schema": {
"types": [
{
"name": "AcceptEnterpriseAdministratorInvitationInput"
},
{
"name": "AcceptEnterpriseAdministratorInvitationPayload"
},
{
"name": "AcceptTopicSuggestionInput"
},
{
"name": "AcceptTopicSuggestionPayload"
}
......
]
}
}
}

Query supported queries and mutations

query getSupportedQueries {
__schema {
queryType {
fields{
name
}
}
}
}

The JSON response below shows the queries supported by the API.

{
"data": {
"__schema": {
"queryType": {
"fields": [
{
"name": "codeOfConduct"
},
{
"name": "codesOfConduct"
},
{
"name": "enterprise"
},
{
"name": "enterpriseAdministratorInvitation"
},
{
"name": "enterpriseAdministratorInvitationByToken"
},
{
"name": "license"
},
{
"name": "licenses"
},
{
"name": "marketplaceCategories"
},
{
"name": "marketplaceCategory"
},
{
"name": "marketplaceListing"
},
{
"name": "marketplaceListings"
},
{
"name": "meta"
},
{
"name": "node"
},
{
"name": "nodes"
},
{
"name": "organization"
},
{
"name": "rateLimit"
},
{
"name": "relay"
},
{
"name": "repository"
},
{
"name": "repositoryOwner"
},
.....
}
}
}
}

Query Property Types

query getRepositoryOwnerType {
__type(name:"RepositoryOwner") {
description
fields {
name
}
}
}

Here is the JSON Response. You can see all the fields that are part of the type RepositoryOwner.

{
"data": {
"__type": {
"description": "Represents an owner of a Repository.",
"fields": [
{
"name": "avatarUrl"
},
{
"name": "id"
},
{
"name": "login"
},
{
"name": "repositories"
},
{
"name": "repository"
},
{
"name": "resourcePath"
},
{
"name": "url"
}
]
}
}
}

Query Directives

query getDirectives {
__schema {
directives {
name
description
}
}
}

The JSON response shows that the GitHub schema supports three directives which are: include, skip and deprecated.

{
"data": {
"__schema": {
"directives": [
{
"name": "include",
"description": "Directs the executor to include this field or fragment only when the `if` argument is true."
},
{
"name": "skip",
"description": "Directs the executor to skip this field or fragment when the `if` argument is true."
},
{
"name": "deprecated",
"description": "Marks an element of a GraphQL schema as no longer supported."
}
]
}
}
}

Conclusion

Other resources:

I hope you enjoyed this article. See you again with more articles. If you liked this post, don’t forget to share it with your network. You can follow me on twitter @AdhithiRavi for more updates or if you have any questions.

This story was originally published at https://programmingwithmosh.com/

Software Consultant, Author, Speaker, React Native|React|GraphQL Dev & Indian Classical Musician http://adhithiravichandran.com/

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store