@@ -36,7 +36,7 @@ that documentation and the corresponding tests in this library together.
36
36
Install Relay Library for GraphQL Python
37
37
38
38
``` sh
39
- pip install graphql-core --pre # Last version of graphql-core
39
+ pip install " graphql-core>=2,<3 " # use version 2.x of graphql-core
40
40
pip install graphql-relay
41
41
```
42
42
@@ -54,13 +54,13 @@ returning those types.
54
54
they return a connection type.
55
55
- ` connection_definitions ` returns a ` connection_type ` and its associated
56
56
` edgeType ` , given a name and a node type.
57
- - ` connection_from_list ` is a helper method that takes an array and the
57
+ - ` connection_from_list ` is a helper method that takes a list and the
58
58
arguments from ` connection_args ` , does pagination and filtering, and returns
59
59
an object in the shape expected by a ` connection_type ` 's ` resolver ` function.
60
60
- ` connection_from_promised_list ` is similar to ` connection_from_list ` , but
61
61
it takes a promise that resolves to an array, and returns a promise that
62
62
resolves to the expected shape by ` connection_type ` .
63
- - ` cursor_for_object_in_connection ` is a helper method that takes an array and a
63
+ - ` cursor_for_object_in_connection ` is a helper method that takes a list and a
64
64
member object, and returns a cursor for use in the mutation payload.
65
65
66
66
An example usage of these methods from the [ test schema] ( tests/starwars/schema.py ) :
@@ -69,32 +69,31 @@ An example usage of these methods from the [test schema](tests/starwars/schema.p
69
69
ship_edge, ship_connection = connection_definitions(' Ship' , shipType)
70
70
71
71
factionType = GraphQLObjectType(
72
- name = ' Faction' ,
73
- description = ' A faction in the Star Wars saga' ,
72
+ name = ' Faction' ,
73
+ description = ' A faction in the Star Wars saga' ,
74
74
fields = lambda : {
75
75
' id' : global_id_field(' Faction' ),
76
76
' name' : GraphQLField(
77
77
GraphQLString,
78
78
description = ' The name of the faction.' ,
79
79
),
80
80
' ships' : GraphQLField(
81
- shipConnection,
82
- description = ' The ships used by the faction.' ,
83
- args = connection_args,
84
- resolver = lambda faction , args , * _ : connection_from_list(
85
- map (getShip, faction.ships),
86
- args
81
+ ship_connection,
82
+ description = ' The ships used by the faction.' ,
83
+ args = connection_args,
84
+ resolver = lambda faction , _info , ** args : connection_from_list(
85
+ [getShip(ship) for ship in faction.ships], args
87
86
),
88
87
)
89
88
},
90
- interfaces = [node_interface]
89
+ interfaces = [node_interface]
91
90
)
92
91
```
93
92
94
93
This shows adding a ` ships ` field to the ` Faction ` object that is a connection.
95
94
It uses ` connection_definitions({name: 'Ship', nodeType: shipType}) ` to create
96
95
the connection type, adds ` connection_args ` as arguments on this function, and
97
- then implements the resolver function by passing the array of ships and the
96
+ then implements the resolver function by passing the list of ships and the
98
97
arguments to ` connection_from_list ` .
99
98
100
99
### Object Identification
@@ -108,7 +107,7 @@ this, it takes a function to resolve an ID to an object, and to determine
108
107
the type of a given object.
109
108
- ` to_global_id ` takes a type name and an ID specific to that type name,
110
109
and returns a "global ID" that is unique among all types.
111
- - ` from_global_id ` takes the "global ID" created by ` toGlobalID ` , and retuns
110
+ - ` from_global_id ` takes the "global ID" created by ` to_global_id ` , and returns
112
111
the type name and ID used to create it.
113
112
- ` global_id_field ` creates the configuration for an ` id ` field on a node.
114
113
- ` plural_identifying_root_field ` creates a field that accepts a list of
@@ -118,17 +117,16 @@ objects.
118
117
An example usage of these methods from the [ test schema] ( tests/starwars/schema.py ) :
119
118
120
119
``` python
121
- def get_node (global_id , context , info ):
122
- resolvedGlobalId = from_global_id(global_id)
123
- _type, _id = resolvedGlobalId.type, resolvedGlobalId.id
124
- if _type == ' Faction' :
125
- return getFaction(_id)
126
- elif _type == ' Ship' :
127
- return getShip(_id)
120
+ def get_node (global_id , _info ):
121
+ type_, id_ = from_global_id(global_id)
122
+ if type_ == ' Faction' :
123
+ return getFaction(id_)
124
+ elif type_ == ' Ship' :
125
+ return getShip(id_)
128
126
else :
129
127
return None
130
128
131
- def get_node_type (obj , context , info ):
129
+ def get_node_type (obj , _info ):
132
130
if isinstance (obj, Faction):
133
131
return factionType
134
132
else :
@@ -177,11 +175,9 @@ class IntroduceShipMutation(object):
177
175
def __init__ (self , shipId , factionId , clientMutationId = None ):
178
176
self .shipId = shipId
179
177
self .factionId = factionId
180
- self .clientMutationId = None
178
+ self .clientMutationId = clientMutationId
181
179
182
- def mutate_and_get_payload (data , * _ ):
183
- shipName = data.get(' shipName' )
184
- factionId = data.get(' factionId' )
180
+ def mutate_and_get_payload (_info , shipName , factionId , ** _input ):
185
181
newShip = createShip(shipName, factionId)
186
182
return IntroduceShipMutation(
187
183
shipId = newShip.id,
@@ -201,19 +197,19 @@ shipMutation = mutation_with_client_mutation_id(
201
197
output_fields = {
202
198
' ship' : GraphQLField(
203
199
shipType,
204
- resolver = lambda payload , * _ : getShip(payload.shipId)
200
+ resolver = lambda payload , _info : getShip(payload.shipId)
205
201
),
206
202
' faction' : GraphQLField(
207
203
factionType,
208
- resolver = lambda payload , * _ : getFaction(payload.factionId)
204
+ resolver = lambda payload , _info : getFaction(payload.factionId)
209
205
)
210
206
},
211
207
mutate_and_get_payload = mutate_and_get_payload
212
208
)
213
209
214
210
mutationType = GraphQLObjectType(
215
211
' Mutation' ,
216
- fields = lambda : {
212
+ fields = lambda : {
217
213
' introduceShip' : shipMutation
218
214
}
219
215
)
0 commit comments