GraphQL framework for Python
Go to file
2015-09-25 16:35:17 -07:00
graphene First relay version 2015-09-25 16:35:17 -07:00
tests First relay version 2015-09-25 16:35:17 -07:00
.gitignore First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
.travis.yml First relay version 2015-09-25 16:35:17 -07:00
LICENSE First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
README.md Simplified tests and Readme example 2015-09-24 02:26:57 -07:00
setup.cfg First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
setup.py First relay version 2015-09-25 16:35:17 -07:00
tox.ini First relay version 2015-09-25 16:35:17 -07:00

Graphene: GraphQL Object Mapper

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))
        if human:
            return Human(human)

    @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)

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