Skip to main content

Graphene: Python DSL for GraphQL

Project description

Graphene is a Python library for creating GraphQL schemas/types easly. It maps the models/fields to internal GraphQL objects without effort. Including automatic Django models conversion.


For instaling graphene, just run this command in your shell

pip install graphene


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)


Querying graphene.Schema is as simple as:

query = '''
    query HeroNameQuery {
      hero {
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.')

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

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


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

python install

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

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

Project details

Release history Release notifications | RSS feed

This version


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for graphene, version 0.1.2
Filename, size File type Python version Upload date Hashes
Filename, size graphene-0.1.2.tar.gz (18.5 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page