GraphQL framework for Python
Go to file
2015-10-05 22:30:05 -07:00
graphene Improved Python3 integration 2015-10-05 22:30:05 -07:00
tests Improved Python3 integration 2015-10-05 22:30:05 -07:00
.gitignore First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
.travis.yml Completed Django support. Improved tests. Changed schema behavior 2015-10-01 01:54:52 -07:00
LICENSE First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
README.md Improved README text 2015-10-03 14:30:47 -07:00
setup.cfg First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
setup.py Improved syntax using pep8 style guide 2015-10-02 22:17:51 -07:00
tox.ini Improved testing 2015-09-29 23:40:40 -07:00

Graphene: Pythonic GraphQL Build Status Coverage Status

This is a library to use GraphQL in a Pythonic and easy way. It maps the models/fields to internal GraphQLlib objects without effort. Including automatic Django models conversion.

Note: This library requires installing graphqllib and graphql-relay external libraries.

Usage

Example code of a GraphQL schema using Graphene:

Schema definition

import graphene
# ...

class Character(graphene.Interface):
    id = graphene.IDField()
    name = graphene.StringField()
    friends = graphene.ListField('self')

    def resolve_friends(self, args, *_):
        return [Human(f) for f in self.instance.friends]

class Human(Character):
    homePlanet = graphene.StringField()

class Query(graphene.ObjectType):
    human = graphene.Field(Human)

schema = graphene.Schema(query=Query)

Querying

Querying graphene.Schema is as simple as:

query = '''
    query HeroNameQuery {
      hero {
        name
      }
    }
'''
result = schema.execute(query)

Relay Schema

Graphene also supports Relay, check the Starwars Relay example!

class Ship(relay.Node):
    '''A ship in the Star Wars saga'''
    name = graphene.StringField(description='The name of the ship.')

    @classmethod
    def get_node(cls, id):
        return Ship(getShip(id))


class Query(graphene.ObjectType):
    ships = relay.ConnectionField(Ship, description='The ships used by the faction.')
    node = relay.NodeField()

    @resolve_only_args
    def resolve_ships(self):
        return [Ship(s) for s in getShips()]

Django+Relay Schema

If you want to use graphene with your Django Models check the Starwars Django example!

class Ship(DjangoNode):
    class Meta:
        model = YourDjangoModelHere
        # only_fields = ('id', 'name') # Only map this fields from the model
        # excluxe_fields ('field_to_excluxe', ) # Exclude mapping this fields from the model

class Query(graphene.ObjectType):
    node = relay.NodeField()

Contributing

After cloning this repo, ensure dependencies are installed by running:

python setup.py install

After developing, the full test suite can be evaluated by running:

python setup.py test # Use --pytest-args="-v -s" for verbose mode