How to get GraphQL schema with Python?

Question:

There’s lots of GUI clients like GraphQL Playground, GraphiQl, etc. with ability of getting GraphQL schema from the URL. How can I get the schema with Python?

Asked By: Igor Zaliznyak

||

Answers:

From the spec:

A GraphQL server supports introspection over its schema. This schema is queried using GraphQL itself, creating a powerful platform for tool‐building… The schema introspection system is accessible from the meta‐fields __schema and __type which are accessible from the type of the root of a query operation.

Tools like GraphQL Playground and GraphiQL utilize introspection to get information about a schema. You don’t need any additional tools or libraries to make an introspection query — since it’s just a GraphQL query, you’ll make the request the same way you make any other request to the endpoint (using requests for example).

Here’s a complete introspection query from graphql-core:

introspection_query = """
  query IntrospectionQuery {
    __schema {
      queryType { name }
      mutationType { name }
      subscriptionType { name }
      types {
        ...FullType
      }
      directives {
        name
        description
        locations
        args {
          ...InputValue
        }
      }
    }
  }
  fragment FullType on __Type {
    kind
    name
    description
    fields(includeDeprecated: true) {
      name
      description
      args {
        ...InputValue
      }
      type {
        ...TypeRef
      }
      isDeprecated
      deprecationReason
    }
    inputFields {
      ...InputValue
    }
    interfaces {
      ...TypeRef
    }
    enumValues(includeDeprecated: true) {
      name
      description
      isDeprecated
      deprecationReason
    }
    possibleTypes {
      ...TypeRef
    }
  }
  fragment InputValue on __InputValue {
    name
    description
    type { ...TypeRef }
    defaultValue
  }
  fragment TypeRef on __Type {
    kind
    name
    ofType {
      kind
      name
      ofType {
        kind
        name
        ofType {
          kind
          name
          ofType {
            kind
            name
            ofType {
              kind
              name
              ofType {
                kind
                name
                ofType {
                  kind
                  name
                }
              }
            }
          }
        }
      }
    }
  }
"""
Answered By: Daniel Rearden

The graphql-core has utilities to get you the query, and convert the query result. Here is an example snippet that print the resulting schema in sdl:

from graphqlclient import GraphQLClient
from pprint import PrettyPrinter
from graphql import get_introspection_query, build_client_schema, print_schema

def main():
    pp = PrettyPrinter(indent=4)
    client = GraphQLClient('http://swapi.graph.cool/')
    query_intros = get_introspection_query(descriptions=True)
    intros_result = client.execute(query_intros, variables=None, operationName=None)
    client_schema = build_client_schema(intros_result.get('data', None))
    sdl = print_schema(client_schema)
    print(sdl)
    pp.pprint(sdl)

I was looking for the same and found the above in the end.

Answered By: minghua

This will create a json file with your schema.

import json

introspection_dict = your_schema_object.introspect()

# Or save the schema into some file
with open("schema.json", "w") as fp:
    json.dump(introspection_dict, fp)
Answered By: Vaibhav Rai