GraphQL framework for Python
Go to file
Syrus Akbary 9e101898f1 Merge branch 'refs/heads/master' into 1.0
Conflicts:
	graphene/core/fields.py
2015-10-25 20:21:06 -07:00
graphene Merge branch 'refs/heads/master' into 1.0 2015-10-25 20:21:06 -07:00
tests Added field description based on function doc 2015-10-25 19:49:28 -07:00
.gitignore First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
.travis.yml Fixed tests 2015-10-08 09:23:55 -07:00
LICENSE First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
README.md Improved Readme 2015-10-10 15:20:52 -07:00
README.rst Improved README 2015-10-06 23:13:10 -07:00
setup.cfg First working version of Graphene 😃 2015-09-24 02:11:50 -07:00
setup.py Updated version 2015-10-23 01:52:31 -07:00
tox.ini Improved relay resolvers 2015-10-20 21:35:56 -07:00

Graphene Logo Graphene Build Status Coverage Status

Graphene is a Python library for building GraphQL schemas/types fast and easily.

  • Easy to use: It maps the models/fields to internal GraphQL objects without effort.
  • Relay: Graphene has builtin support for Relay
  • Django: Automatic Django models conversion. See an example Django implementation

Installation

For instaling graphene, just run this command in your shell

pip install graphene

Usage

Example code of a GraphQL schema using Graphene:

Schema definition

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)

Then 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):
    name = graphene.StringField()

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


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

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
        # exclude_fields ('field_to_exclude', ) # 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