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.

Source Distribution

gql-2.0.0.tar.gz (27.8 kB view details)

Uploaded Source

Built Distribution

gql-2.0.0-py2.py3-none-any.whl (10.3 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file gql-2.0.0.tar.gz.

File metadata

  • Download URL: gql-2.0.0.tar.gz
  • Upload date:
  • Size: 27.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0

File hashes

Hashes for gql-2.0.0.tar.gz
Algorithm Hash digest
SHA256 fe8d3a08047f77362ddfcfddba7cae377da2dd66f5e61c59820419c9283d4fb5
MD5 b67bbf0e6181ef7b05441fd0aba8a603
BLAKE2b-256 4e3c8c84c49dce30d7847a8e2274517ba42af9090a38ffc2f4a6b644d36d7f63

See more details on using hashes here.

Provenance

File details

Details for the file gql-2.0.0-py2.py3-none-any.whl.

File metadata

  • Download URL: gql-2.0.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 10.3 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/46.4.0 requests-toolbelt/0.9.1 tqdm/4.46.0 CPython/3.8.0

File hashes

Hashes for gql-2.0.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 35032ddd4bfe6b8f3169f806b022168932385d751eacc5c5f7122e0b3f4d6b88
MD5 720d5f9d0c9c4a7154ef53d3d70dca1a
BLAKE2b-256 550ce9cb15ae9589a6a87a1c329c92e5a6f90cd1759d2afc6e2ac3acbc9a5fc9

See more details on using hashes here.

Provenance

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page