1
- Graphene | Build Status | |Coverage Status |
2
- =========================================
1
+ | Graphene Logo | ` Graphene < http://graphene-python.org >`__ | Build Status | | PyPI version | |Coverage Status |
2
+ =========================================================================================================
3
3
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: ** It maps the models/fields to internal
6
+ GraphQL objects without effort. \* **Relay: ** Graphene has builtin
7
+ support for Relay \* **Django: ** Automatic *Django model * mapping to
8
+ Graphene Types. *See an `example
9
+ Django <http://github.com/graphql-python/swapi-graphene>`__
10
+ implementation *
7
11
8
12
Installation
9
13
------------
@@ -13,89 +17,46 @@ For instaling graphene, just run this command in your shell
13
17
.. code :: bash
14
18
15
19
pip install graphene
20
+ # Or in case of need Django model support
21
+ pip install graphene[django]
16
22
17
- Usage
18
- -----
23
+ Examples
24
+ --------
19
25
20
- Example code of a GraphQL schema using Graphene:
21
-
22
- Schema definition
23
- ~~~~~~~~~~~~~~~~~
26
+ Here is one example for get you started:
24
27
25
28
.. code :: python
26
29
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]
30
+ class Query (graphene .ObjectType ):
31
+ hello = graphene.StringField(description = ' A typical hello world' )
32
+ ping = graphene.StringField(description = ' Ping someone' ,
33
+ to = graphene.Argument(graphene.String))
34
34
35
- class Human ( Character ):
36
- homePlanet = graphene.StringField()
35
+ def resolve_hello ( self , args , info ):
36
+ return ' World '
37
37
38
- class Query ( graphene . ObjectType ):
39
- human = graphene.Field(Human )
38
+ def resolve_ping ( self , args , info ):
39
+ return ' Pinging {} ' .format(args.get( ' to ' ) )
40
40
41
41
schema = graphene.Schema(query = Query)
42
42
43
- Querying
44
- ~~~~~~~~
45
-
46
- Querying ``graphene.Schema `` is as simple as:
43
+ Then Querying ``graphene.Schema `` is as simple as:
47
44
48
45
.. code :: python
49
46
50
47
query = '''
51
- query HeroNameQuery {
52
- hero {
53
- name
54
- }
48
+ query SayHello {
49
+ hello
50
+ ping(to:'peter')
55
51
}
56
52
'''
57
53
result = schema.execute(query)
58
54
59
- Relay Schema
60
- ~~~~~~~~~~~~
55
+ If you want to learn even more, you can also check the following
56
+ examples:
61
57
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 ))
73
-
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()
58
+ - Relay Schema: `Starwars Relay example <examples/starwars_relay >`__
59
+ - Django: `Starwars Django example <examples/starwars_django >`__
99
60
100
61
Contributing
101
62
------------
@@ -112,11 +73,10 @@ After developing, the full test suite can be evaluated by running:
112
73
113
74
python setup.py test # Use --pytest-args="-v -s" for verbose mode
114
75
115
- .. _Django models : #djangorelay-schema
116
- .. _Starwars Relay example : tests/starwars_relay
117
- .. _Starwars Django example : tests/starwars_django
118
-
76
+ .. |Graphene Logo | image :: http://graphene-python.org/favicon.png
119
77
.. |Build Status | image :: https://travis-ci.org/graphql-python/graphene.svg?branch=master
120
78
:target: https://travis-ci.org/graphql-python/graphene
79
+ .. |PyPI version | image :: https://badge.fury.io/py/graphene.svg
80
+ :target: https://badge.fury.io/py/graphene
121
81
.. |Coverage Status | image :: https://coveralls.io/repos/graphql-python/graphene/badge.svg?branch=master&service=github
122
82
:target: https://coveralls.io/github/graphql-python/graphene?branch=master
0 commit comments