How to find available operations in GraphQL URL?

Viewed 2040

Pardon for such vague question. I have a URL http://example.com/graphql. I want to know what operations are available. Is there anyway to find it out?

1 Answers

GraphQL has the introspection system which allow you to query its schema.You can construct GraphQL query to explore its types.

For example ,to find out all the query and mutation root fields , you can POST the following JSON body to http://example.com/graphql with content type set to application/json

{
   "query" : "{__schema {queryType {name fields {name}}mutationType {name fields {name}}}}"
}

Or using GET and putting the GraphQL query to the query parameter query :

http://example.com/graphql/?query={__schema {queryType {name fields {name}}mutationType {name fields {name}}}}

which will return something likes:

{
  "data": {
    "__schema": {
      "queryType": {
        "name": "Query",
        "fields": [
          {
            "name": "searchFoo"
          },
          {
            "name": "searchBar"
          }

        ]
      },
      "mutationType": {
        "name": "Mutation",
        "fields": [
          {
            "name": "createFoo"
          },
          {
            "name": "updateFoo"
          }
        ]
      }
    }
  }

But the much easier way is to use a GraphQL client such as GraphiQL or Insomnia or ..... which most probably will provide you an interactive UI to exploring the GraphQL Schema and has some auto-complete/hint feature for assisting you to construct the GraphQL query.

Related