Skip to main content

GraphQL implementation for Python

Project description

GraphQL-core

GraphQL for Python.

This library is a port of graphql-js to Python and currently is up-to-date with release 0.6.0.

PyPI version Build Status Coverage Status Public Slack Discussion

See more complete documentation at http://graphql.org/ and http://graphql.org/docs/api-reference-graphql/.

For questions, ask Stack Overflow.

Getting Started

An overview of the GraphQL language is available in the README for the Specification for GraphQL.

The overview describes a simple set of GraphQL examples that exist as tests in this repository. A good way to get started is to walk through that README and the corresponding tests in parallel.

Using graphql-core

Install from pip:

pip install graphql-core

GraphQL.js provides two important capabilities: building a type schema, and serving queries against that type schema.

First, build a GraphQL type schema which maps to your code base.

from graphql import (
    graphql,
    GraphQLSchema,
    GraphQLObjectType,
    GraphQLField,
    GraphQLString
)

schema = GraphQLSchema(
  query= GraphQLObjectType(
    name='RootQueryType',
    fields={
      'hello': GraphQLField(
        type= GraphQLString,
        resolver=lambda *_: 'world'
      )
    }
  )
)

This defines a simple schema with one type and one field, that resolves to a fixed value. The resolve function can return a value, a promise, or an array of promises. A more complex example is included in the top level tests directory.

Then, serve the result of a query against that type schema.

query = '{ hello }'

result = graphql(schema, query)

# Prints
# {'hello': 'world'} (as OrderedDict)

print result.data

This runs a query fetching the one field defined. The graphql function will first ensure the query is syntactically and semantically valid before executing it, reporting errors otherwise.

query = '{ boyhowdy }'

result = graphql(schema, query)

# Prints
# [GraphQLError('Cannot query field "boyhowdy" on type "RootQueryType".',)]

print result.errors

Executors

The graphql query is executed, by default, synchronously (using SyncExecutor). However the following executors are available if we want to resolve our fields in parallel:

  • graphql.execution.executors.asyncio.AsyncioExecutor: This executor executes the resolvers in the Python asyncio event loop.
  • graphql.execution.executors.gevent.GeventExecutor: This executor executes the resolvers in the Gevent event loop.
  • graphql.execution.executors.process.ProcessExecutor: This executor executes each resolver as a process.
  • graphql.execution.executors.thread.ThreadExecutor: This executor executes each resolver in a Thread.
  • graphql.execution.executors.sync.SyncExecutor: This executor executes each resolver synchronusly (default).

Usage

You can specify the executor to use via the executor keyword argument in the grapqhl.execution.execute function.

from graphql.execution.execute import execute

execute(schema, ast, executor=SyncExecutor())

Contributing

After cloning this repo, create a virtualenv and ensure dependencies are installed by running:

virtualenv venv
source venv/bin/activate
pip install -e ".[test]"

Well-written tests and maintaining good test coverage is important to this project. While developing, run new and existing tests with:

py.test PATH/TO/MY/DIR/test_test.py # Single file
py.test PATH/TO/MY/DIR/ # All tests in directory

Add the -s flag if you have introduced breakpoints into the code for debugging. Add the -v ("verbose") flag to get more detailed test output. For even more detailed output, use -vv. Check out the pytest documentation for more options and test running controls.

GraphQL-core supports several versions of Python. To make sure that changes do not break compatibility with any of those versions, we use tox to create virtualenvs for each python version and run tests with that version. To run against all python versions defined in the tox.ini config file, just run:

tox

If you wish to run against a specific version defined in the tox.ini file:

tox -e py36

Tox can only use whatever versions of python are installed on your system. When you create a pull request, Travis will also be running the same tests and report the results, so there is no need for potential contributors to try to install every single version of python on their own system ahead of time. We appreciate opening issues and pull requests to make GraphQL-core even more stable & useful!

Main Contributors

License

MIT License

Project details


Release history Release notifications | RSS feed

This version

2.3

Download files

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

Source Distribution

graphql-core-2.3.tar.gz (97.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

graphql_core-2.3-py2.py3-none-any.whl (251.5 kB view details)

Uploaded Python 2Python 3

File details

Details for the file graphql-core-2.3.tar.gz.

File metadata

  • Download URL: graphql-core-2.3.tar.gz
  • Upload date:
  • Size: 97.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/2.7.15

File hashes

Hashes for graphql-core-2.3.tar.gz
Algorithm Hash digest
SHA256 535585f0dc451fd3f3ef7df34781856e26d9effd5c50e677bae3cabfb883abc7
MD5 1974dc314fdd3b656f35d1a281d27584
BLAKE2b-256 b9cedeead67defdac0f117e2c99bd6aad1c98e41433330a48c9f890e0a0dc4a2

See more details on using hashes here.

File details

Details for the file graphql_core-2.3-py2.py3-none-any.whl.

File metadata

  • Download URL: graphql_core-2.3-py2.py3-none-any.whl
  • Upload date:
  • Size: 251.5 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.41.1 CPython/2.7.15

File hashes

Hashes for graphql_core-2.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 c1b1a9f57434ef65bf5379c8a6b53755c2e7a38bd77bf01687f51c3361ac041b
MD5 5c04d37c13afd40692e10499cf147e19
BLAKE2b-256 111f22e70738a072bc8aecef18a62fcecc7e25f80fea9d5929480b9edb149a7b

See more details on using hashes here.

Supported by

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