Skip to main content

GraphQL client for Python

Project description

GQL

This is a GraphQL client for Python. Plays nicely with graphene, graphql-core, graphql-js and any other GraphQL implementation compatible with the spec.

GQL architecture is inspired by React-Relay and Apollo-Client.

travis pyversion pypi Anaconda-Server Badge coveralls

Installation

$ pip install gql

Usage

The example below shows how you can execute queries against a local schema.

from gql import gql, Client

from .someSchema import SampleSchema


client = Client(schema=SampleSchema)
query = gql('''
    {
      hello
    }
''')

client.execute(query)

If you want to add additional headers when executing the query, you can specify these in a transport object:

from gql import Client
from gql.transport.requests import RequestsHTTPTransport

from .someSchema import SampleSchema

client = Client(transport=RequestsHTTPTransport(
     url='/graphql', headers={'Authorization': 'token'}), schema=SampleSchema)

To execute against a graphQL API. (We get the schema by using introspection).

from gql import gql, Client
from gql.transport.requests import RequestsHTTPTransport

sample_transport=RequestsHTTPTransport(
    url='https://countries.trevorblades.com/',
    use_json=True,
    headers={
        "Content-type": "application/json",
    },
    verify=False,
    retries=3,
)

client = Client(
    transport=sample_transport,
    fetch_schema_from_transport=True,
)

query = gql('''
    query getContinents {
      continents {
        code
        name
      }
    }
''')

client.execute(query)

If you have a local schema stored as a schema.graphql file, you can do:

from graphql import build_ast_schema, parse
from gql import gql, Client

with open('path/to/schema.graphql') as source:
    document = parse(source.read())

schema = build_ast_schema(document)

client = Client(schema=schema)
query = gql('''
    {
      hello
    }
''')

client.execute(query)

Contributing

See CONTRIBUTING.md

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for gql, version 2.0.0
Filename, size File type Python version Upload date Hashes
Filename, size gql-2.0.0-py2.py3-none-any.whl (10.3 kB) File type Wheel Python version py2.py3 Upload date Hashes View
Filename, size gql-2.0.0.tar.gz (27.8 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page