• Stars
    star
    375
  • Rank 110,340 (Top 3 %)
  • Language
    Python
  • License
    MIT License
  • Created over 8 years ago
  • Updated over 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

GraphQL base implementation for Python (legacy version – see graphql-core for the current one)

GraphQL-core 2

⚠️ This is the repository of GraphQL for Python 2 (legacy version).

The repository for the current version is available at github.com/graphql-python/graphql-core.

Build Status Coverage Status

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

GraphQL-core 2 supports Python version 2.7, 3.5, 3.6, 3.7 and 3.8.

GraphQL.js is the JavaScript reference implementation for GraphQL, a query language for APIs created by Facebook.

See also the GraphQL documentation at graphql.org and graphql.org/graphql-js/graphql/.

For questions regarding GraphQL, 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 2

Install from pip:

pip install "graphql-core<3"

GraphQL-core 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 resolver 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 import parse
from graphql.execution import execute
from graphql.execution.executors.sync import SyncExecutor

ast = parse('{ hello }')

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

print result.data

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:

pytest PATH/TO/MY/DIR/test_test.py # Single file
pytest 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 2 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

More Repositories

1

graphene

GraphQL framework for Python
Python
7,978
star
2

graphene-django

Build powerful, efficient, and flexible GraphQL APIs with seamless Django integration.
Python
4,235
star
3

gql

A GraphQL client in Python
Python
1,474
star
4

flask-graphql

Adds GraphQL support to your Flask application.
Python
1,316
star
5

graphene-sqlalchemy

Graphene SQLAlchemy integration
Python
971
star
6

graphql-core

A Python 3.6+ port of the GraphQL.js reference implementation of GraphQL.
Python
498
star
7

graphene-mongo

Graphene MongoEngine integration
Python
285
star
8

graphql-ws

GraphQL websockets
Python
271
star
9

graphene-pydantic

Integrate GraphQL with your Pydantic models
Python
220
star
10

swapi-graphene

GraphQL Starwars API using Graphene and Django
Python
172
star
11

sanic-graphql

Adds GraphQL support to your Sanic app.
Python
149
star
12

graphql-relay-py

A library to help construct a graphql-py server supporting react-relay
Python
145
star
13

aiohttp-graphql

Adds GraphQL support to your aiohttp app.
Python
118
star
14

graphene-gae

GraphQL Support for Google AppEngine [DEPRECATED - Looking for maintainers]
Python
117
star
15

graphql-server

This is the core package for using GraphQL in a custom server easily
Python
114
star
16

gql-next

A Python GraphQL Client library providing ability to validate and make type-safe GraphQL calls
Python
76
star
17

graphene-tornado

Python
52
star
18

graphene-federation

Federation implementation for Graphene.
Python
40
star
19

webob-graphql

GraphQL integration for WebOb based frameworks: Pyramid, Pylons...
Python
28
star
20

GraphQL-SublimeText

GraphQL language syntax for SublimeText
17
star
21

graphene-python.org

Graphene-Python.org official website
JavaScript
6
star