Skip to content

Commit 3e8e65f

Browse files
committed
Merge pull request #16 from graphql-python/1.0
Path to 0.3
2 parents fdb1bb3 + 97a8112 commit 3e8e65f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

64 files changed

+1280
-733
lines changed

.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,5 @@ docs/_build/
5959
# PyBuilder
6060
target/
6161

62+
63+
/tests/django.sqlite

.travis.yml

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,10 @@ python:
66
- 3.4
77
- 3.5
88
- pypy
9+
cache: pip
910
install:
10-
- pip install pytest pytest-cov coveralls flake8 six blinker pytest-django
11-
- pip install -e .[django]
11+
- pip install --cache-dir $HOME/.cache/pip pytest pytest-cov coveralls flake8 six blinker pytest-django
12+
- pip install --cache-dir $HOME/.cache/pip -e .[django]
1213
- python setup.py develop
1314
script:
1415
- py.test --cov=graphene

README.md

+25-53
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
# ![Graphene Logo](http://graphene-python.org/favicon.png) [Graphene](http://graphene-python.org) [![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)
1+
# ![Graphene Logo](http://graphene-python.org/favicon.png) [Graphene](http://graphene-python.org) [![Build Status](https://travis-ci.org/graphql-python/graphene.svg?branch=master)](https://travis-ci.org/graphql-python/graphene) [![PyPI version](https://badge.fury.io/py/graphene.svg)](https://badge.fury.io/py/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)
22

33

44
Graphene is a Python library for building GraphQL schemas/types fast and easily.
5-
* **Easy to use:** It maps the models/fields to internal GraphQL objects without effort.
5+
* **Easy to use:** Graphene helps you use GraphQL in Python without effort.
66
* **Relay:** Graphene has builtin support for Relay
7-
* **Django:** Automatic [Django models](#djangorelay-schema) conversion. *See an [example Django](https://github.com/graphql-python/swapi-graphene) implementation*
7+
* **Django:** Automatic *Django model* mapping to Graphene Types. *See an [example Django](https://github.com/graphql-python/swapi-graphene) implementation*
8+
9+
10+
*But, what is supported in this Python version?* **Everything**: Interfaces, ObjectTypes, Mutations and Relay (Nodes, Connections and Mutations).
811

912

1013
## Installation
@@ -13,29 +16,26 @@ For instaling graphene, just run this command in your shell
1316

1417
```bash
1518
pip install graphene
19+
# Or in case of need Django model support
20+
pip install graphene[django]
1621
```
1722

1823

19-
## Usage
20-
21-
Example code of a GraphQL schema using Graphene:
24+
## Examples
2225

23-
### Schema definition
26+
Here is one example for get you started:
2427

2528
```python
26-
class Character(graphene.Interface):
27-
id = graphene.IDField()
28-
name = graphene.StringField()
29-
friends = graphene.ListField('self')
30-
31-
def resolve_friends(self, args, *_):
32-
return [Human(f) for f in self.instance.friends]
29+
class Query(graphene.ObjectType):
30+
hello = graphene.StringField(description='A typical hello world')
31+
ping = graphene.StringField(description='Ping someone',
32+
to=graphene.Argument(graphene.String))
3333

34-
class Human(Character):
35-
homePlanet = graphene.StringField()
34+
def resolve_hello(self, args, info):
35+
return 'World'
3636

37-
class Query(graphene.ObjectType):
38-
human = graphene.Field(Human)
37+
def resolve_ping(self, args, info):
38+
return 'Pinging {}'.format(args.get('to'))
3939

4040
schema = graphene.Schema(query=Query)
4141
```
@@ -44,48 +44,20 @@ Then Querying `graphene.Schema` is as simple as:
4444

4545
```python
4646
query = '''
47-
query HeroNameQuery {
48-
hero {
49-
name
50-
}
47+
query SayHello {
48+
hello
49+
ping(to:'peter')
5150
}
5251
'''
5352
result = schema.execute(query)
5453
```
5554

56-
### Relay Schema
57-
58-
Graphene also supports Relay, check the [Starwars Relay example](tests/starwars_relay)!
59-
60-
```python
61-
class Ship(relay.Node):
62-
name = graphene.StringField()
63-
64-
@classmethod
65-
def get_node(cls, id):
66-
return Ship(your_ship_instance)
67-
68-
69-
class Query(graphene.ObjectType):
70-
ships = relay.ConnectionField(Ship)
71-
node = relay.NodeField()
72-
73-
```
74-
75-
### Django+Relay Schema
76-
77-
If you want to use graphene with your Django Models check the [Starwars Django example](tests/starwars_django)!
55+
If you want to learn even more, you can also check the following [examples](examples/):
7856

79-
```python
80-
class Ship(DjangoNode):
81-
class Meta:
82-
model = YourDjangoModelHere
83-
# only_fields = ('id', 'name') # Only map this fields from the model
84-
# exclude_fields ('field_to_exclude', ) # Exclude mapping this fields from the model
57+
* **Basic Schema**: [Starwars example](examples/starwars)
58+
* **Relay Schema**: [Starwars Relay example](examples/starwars_relay)
59+
* **Django model mapping**: [Starwars Django example](examples/starwars_django)
8560

86-
class Query(graphene.ObjectType):
87-
node = relay.NodeField()
88-
```
8961

9062
## Contributing
9163

README.rst

+39-73
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,16 @@
1-
Graphene |Build Status| |Coverage Status|
2-
=========================================
1+
|Graphene Logo| `Graphene <http://graphene-python.org>`__ |Build Status| |PyPI version| |Coverage Status|
2+
=========================================================================================================
33

4-
Graphene is a Python library for creating GraphQL schemas/types easly.
5-
It maps the models/fields to internal GraphQL objects without effort.
6-
Including automatic `Django models`_ conversion.
4+
Graphene is a Python library for building GraphQL schemas/types fast and
5+
easily. \* **Easy to use:** Graphene helps you use GraphQL in Python
6+
without effort. \* **Relay:** Graphene has builtin support for Relay \*
7+
**Django:** Automatic *Django model* mapping to Graphene Types. *See an
8+
`example Django <https://github.com/graphql-python/swapi-graphene>`__
9+
implementation*
10+
11+
*But, what is supported in this Python version?* **Everything**:
12+
Interfaces, ObjectTypes, Mutations and Relay (Nodes, Connections and
13+
Mutations).
714

815
Installation
916
------------
@@ -13,89 +20,49 @@ For instaling graphene, just run this command in your shell
1320
.. code:: bash
1421
1522
pip install graphene
23+
# Or in case of need Django model support
24+
pip install graphene[django]
1625
17-
Usage
18-
-----
19-
20-
Example code of a GraphQL schema using Graphene:
26+
Examples
27+
--------
2128

22-
Schema definition
23-
~~~~~~~~~~~~~~~~~
29+
Here is one example for get you started:
2430

2531
.. code:: python
2632
27-
class Character(graphene.Interface):
28-
id = graphene.IDField()
29-
name = graphene.StringField()
30-
friends = graphene.ListField('self')
31-
32-
def resolve_friends(self, args, *_):
33-
return [Human(f) for f in self.instance.friends]
33+
class Query(graphene.ObjectType):
34+
hello = graphene.StringField(description='A typical hello world')
35+
ping = graphene.StringField(description='Ping someone',
36+
to=graphene.Argument(graphene.String))
3437
35-
class Human(Character):
36-
homePlanet = graphene.StringField()
38+
def resolve_hello(self, args, info):
39+
return 'World'
3740
38-
class Query(graphene.ObjectType):
39-
human = graphene.Field(Human)
41+
def resolve_ping(self, args, info):
42+
return 'Pinging {}'.format(args.get('to'))
4043
4144
schema = graphene.Schema(query=Query)
4245
43-
Querying
44-
~~~~~~~~
45-
46-
Querying ``graphene.Schema`` is as simple as:
46+
Then Querying ``graphene.Schema`` is as simple as:
4747

4848
.. code:: python
4949
5050
query = '''
51-
query HeroNameQuery {
52-
hero {
53-
name
54-
}
51+
query SayHello {
52+
hello
53+
ping(to:'peter')
5554
}
5655
'''
5756
result = schema.execute(query)
5857
59-
Relay Schema
60-
~~~~~~~~~~~~
61-
62-
Graphene also supports Relay, check the `Starwars Relay example`_!
63-
64-
.. code:: python
65-
66-
class Ship(relay.Node):
67-
'''A ship in the Star Wars saga'''
68-
name = graphene.StringField(description='The name of the ship.')
69-
70-
@classmethod
71-
def get_node(cls, id):
72-
return Ship(getShip(id))
58+
If you want to learn even more, you can also check the following
59+
`examples <examples/>`__:
7360

74-
75-
class Query(graphene.ObjectType):
76-
ships = relay.ConnectionField(Ship, description='The ships used by the faction.')
77-
node = relay.NodeField()
78-
79-
@resolve_only_args
80-
def resolve_ships(self):
81-
return [Ship(s) for s in getShips()]
82-
83-
Django+Relay Schema
84-
~~~~~~~~~~~~~~~~~~~
85-
86-
If you want to use graphene with your Django Models check the `Starwars
87-
Django example`_!
88-
89-
.. code:: python
90-
91-
class Ship(DjangoNode):
92-
class Meta:
93-
model = YourDjangoModelHere
94-
# only_fields = ('id', 'name') # Only map this fields from the model
95-
# excluxe_fields ('field_to_excluxe', ) # Exclude mapping this fields from the model
96-
97-
class Query(graphene.ObjectType):
98-
node = relay.NodeField()
61+
- **Basic Schema**: `Starwars example <examples/starwars>`__
62+
- **Relay Schema**: `Starwars Relay
63+
example <examples/starwars_relay>`__
64+
- **Django model mapping**: `Starwars Django
65+
example <examples/starwars_django>`__
9966

10067
Contributing
10168
------------
@@ -112,11 +79,10 @@ After developing, the full test suite can be evaluated by running:
11279
11380
python setup.py test # Use --pytest-args="-v -s" for verbose mode
11481
115-
.. _Django models: #djangorelay-schema
116-
.. _Starwars Relay example: tests/starwars_relay
117-
.. _Starwars Django example: tests/starwars_django
118-
82+
.. |Graphene Logo| image:: http://graphene-python.org/favicon.png
11983
.. |Build Status| image:: https://travis-ci.org/graphql-python/graphene.svg?branch=master
12084
:target: https://travis-ci.org/graphql-python/graphene
85+
.. |PyPI version| image:: https://badge.fury.io/py/graphene.svg
86+
:target: https://badge.fury.io/py/graphene
12187
.. |Coverage Status| image:: https://coveralls.io/repos/graphql-python/graphene/badge.svg?branch=master&service=github
12288
:target: https://coveralls.io/github/graphql-python/graphene?branch=master

bin/convert_documentation

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/bash
2+
3+
pandoc README.md --from markdown --to rst -s -o README.rst
File renamed without changes.
File renamed without changes.

examples/starwars/data.py

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
humanData = {}
2+
droidData = {}
3+
4+
5+
def setup():
6+
from .schema import Human, Droid
7+
global humanData, droidData
8+
luke = Human(
9+
id='1000',
10+
name='Luke Skywalker',
11+
friends=['1002', '1003', '2000', '2001'],
12+
appears_in=[4, 5, 6],
13+
home_planet='Tatooine',
14+
)
15+
16+
vader = Human(
17+
id='1001',
18+
name='Darth Vader',
19+
friends=['1004'],
20+
appears_in=[4, 5, 6],
21+
home_planet='Tatooine',
22+
)
23+
24+
han = Human(
25+
id='1002',
26+
name='Han Solo',
27+
friends=['1000', '1003', '2001'],
28+
appears_in=[4, 5, 6],
29+
home_planet=None,
30+
)
31+
32+
leia = Human(
33+
id='1003',
34+
name='Leia Organa',
35+
friends=['1000', '1002', '2000', '2001'],
36+
appears_in=[4, 5, 6],
37+
home_planet='Alderaan',
38+
)
39+
40+
tarkin = Human(
41+
id='1004',
42+
name='Wilhuff Tarkin',
43+
friends=['1001'],
44+
appears_in=[4],
45+
home_planet=None,
46+
)
47+
48+
humanData = {
49+
'1000': luke,
50+
'1001': vader,
51+
'1002': han,
52+
'1003': leia,
53+
'1004': tarkin,
54+
}
55+
56+
threepio = Droid(
57+
id='2000',
58+
name='C-3PO',
59+
friends=['1000', '1002', '1003', '2001'],
60+
appears_in=[4, 5, 6],
61+
primary_function='Protocol',
62+
)
63+
64+
artoo = Droid(
65+
id='2001',
66+
name='R2-D2',
67+
friends=['1000', '1002', '1003'],
68+
appears_in=[4, 5, 6],
69+
primary_function='Astromech',
70+
)
71+
72+
droidData = {
73+
'2000': threepio,
74+
'2001': artoo,
75+
}
76+
77+
78+
def getCharacter(id):
79+
return humanData.get(id) or droidData.get(id)
80+
81+
82+
def getFriends(character):
83+
return map(getCharacter, character.friends)
84+
85+
86+
def getHero(episode):
87+
if episode == 5:
88+
return humanData['1000']
89+
return droidData['2001']
90+
91+
92+
def getHuman(id):
93+
return humanData.get(id)
94+
95+
96+
def getDroid(id):
97+
return droidData.get(id)

0 commit comments

Comments
 (0)