graphene/README.md

101 lines
2.4 KiB
Markdown
Raw Normal View History

2015-10-07 09:13:10 +03:00
# Graphene [![Build Status](https://travis-ci.org/graphql-python/graphene.svg?branch=master)](https://travis-ci.org/graphql-python/graphene) [![Coverage Status](https://coveralls.io/repos/graphql-python/graphene/badge.svg?branch=master&service=github)](https://coveralls.io/github/graphql-python/graphene?branch=master)
2015-09-24 12:11:50 +03:00
2015-10-07 09:13:10 +03:00
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](#djangorelay-schema) conversion.
2015-09-24 12:11:50 +03:00
## Installation
For instaling graphene, just run this command in your shell
```bash
pip install graphene
```
2015-10-01 19:40:51 +03:00
2015-09-24 12:17:56 +03:00
## Usage
Example code of a GraphQL schema using Graphene:
### Schema definition
```python
class Character(graphene.Interface):
id = graphene.IDField()
name = graphene.StringField()
2015-09-24 12:26:57 +03:00
friends = graphene.ListField('self')
2015-09-24 12:17:56 +03:00
def resolve_friends(self, args, *_):
return [Human(f) for f in self.instance.friends]
2015-09-24 12:17:56 +03:00
class Human(Character):
homePlanet = graphene.StringField()
class Query(graphene.ObjectType):
human = graphene.Field(Human)
2015-09-24 12:17:56 +03:00
schema = graphene.Schema(query=Query)
2015-09-24 12:17:56 +03:00
```
2015-10-07 09:13:10 +03:00
Then Querying `graphene.Schema` is as simple as:
2015-09-24 12:17:56 +03:00
```python
query = '''
query HeroNameQuery {
hero {
name
}
}
'''
result = schema.execute(query)
2015-09-24 12:17:56 +03:00
```
2015-09-26 09:31:53 +03:00
### Relay Schema
Graphene also supports Relay, check the [Starwars Relay example](tests/starwars_relay)!
2015-09-26 09:31:53 +03:00
```python
class Ship(relay.Node):
2015-10-07 09:13:10 +03:00
name = graphene.StringField()
2015-09-26 09:31:53 +03:00
@classmethod
def get_node(cls, id):
2015-10-07 09:13:10 +03:00
return Ship(your_ship_instance)
2015-09-26 09:31:53 +03:00
class Query(graphene.ObjectType):
2015-10-07 09:13:10 +03:00
ships = relay.ConnectionField(Ship)
2015-09-26 09:31:53 +03:00
node = relay.NodeField()
```
2015-09-26 09:31:53 +03:00
### Django+Relay Schema
2015-09-26 09:31:53 +03:00
If you want to use graphene with your Django Models check the [Starwars Django example](tests/starwars_django)!
2015-09-26 09:31:53 +03:00
```python
class Ship(DjangoNode):
class Meta:
model = YourDjangoModelHere
# only_fields = ('id', 'name') # Only map this fields from the model
2015-10-07 09:13:10 +03:00
# exclude_fields ('field_to_exclude', ) # Exclude mapping this fields from the model
2015-09-26 09:31:53 +03:00
class Query(graphene.ObjectType):
node = relay.NodeField()
2015-09-26 09:31:53 +03:00
```
2015-09-24 12:11:50 +03:00
## Contributing
After cloning this repo, ensure dependencies are installed by running:
```sh
python setup.py install
```
After developing, the full test suite can be evaluated by running:
```sh
python setup.py test # Use --pytest-args="-v -s" for verbose mode
```