GraphQL framework for Python
Go to file
Syrus Akbary b5f49b1014 Merge pull request #2 from syrusakbary/django
Django Models integration
2015-10-01 01:57:52 -07:00
graphene Completed Django support. Improved tests. Changed schema behavior 2015-10-01 01:54:52 -07:00
tests Completed Django support. Improved tests. Changed schema behavior 2015-10-01 01:54:52 -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 ObjectType instances 2015-09-27 20:38:44 -07:00
setup.cfg First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
setup.py Improved testing 2015-09-29 23:40:40 -07:00
tox.ini Improved testing 2015-09-29 23:40:40 -07:00

Graphene: Python DSL for GraphQL

This is a library to use GraphQL in Python in a easy way. It will map the models/fields to internal GraphQL-py objects without effort.

Build Status Coverage Status

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')
    appearsIn = graphene.ListField(Episode)

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

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


class Droid(Character):
    primaryFunction = graphene.StringField()


class Query(graphene.ObjectType):
    hero = graphene.Field(Character,
        episode = graphene.Argument(Episode)
    )
    human = graphene.Field(Human,
        id = graphene.Argument(graphene.String)
    )
    droid = graphene.Field(Droid,
        id = graphene.Argument(graphene.String)
    )

    @resolve_only_args
    def resolve_hero(self, episode):
        return wrap_character(getHero(episode))

    @resolve_only_args
    def resolve_human(self, id):
        return wrap_character(getHuman(id))

    @resolve_only_args
    def resolve_droid(self, id):
        return wrap_character(getDroid(id))


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)[/tests/starwars_relay]!

import graphene
from graphene import relay

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 Faction(relay.Node):
    '''A faction in the Star Wars saga'''
    name = graphene.StringField(description='The name of the faction.')
    ships = relay.ConnectionField(Ship, description='The ships used by the faction.')

    @resolve_only_args
    def resolve_ships(self, **kwargs):
        return [Ship(getShip(ship)) for ship in self.instance.ships]

    @classmethod
    def get_node(cls, id):
        return Faction(getFaction(id)


class Query(graphene.ObjectType):
    rebels = graphene.Field(Faction)
    empire = graphene.Field(Faction)
    node = relay.NodeField()

    @resolve_only_args
    def resolve_rebels(self):
        return Faction(getRebels())

    @resolve_only_args
    def resolve_empire(self):
        return Faction(getEmpire())


Schema = graphene.Schema(query=Query)

# Later on, for querying
Schema.execute('''rebels { name }''')

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