@@ -164,9 +164,100 @@ class RealHumanBean(models.Model):
164
164
R.Human.CanBe(Human)
165
165
```
166
166
167
+ # # Relay Support (WIP)
168
+ - [x] Node
169
+ - [x] Connection & Edges
170
+ - [ ] Mutations
171
+
172
+ At this point, Epoxy has rudimentary `relay` support. Enable support for `Relay` by mixing in the `RelayMixin` using
173
+ `TypeResolver.Mixin` .
174
+
175
+ ```python
176
+ from epoxy.contrib.relay import RelayMixin
177
+ from epoxy.contrib.relay.data_source.memory import InMemoryDataSource
178
+
179
+ # Epoxy provides an "in memory" data source, that implements `epoxy.contrib.relay.data_source.BaseDataSource`,
180
+ # which can be used to easily create a mock data source. In practice, you'd implement your own data source.
181
+ data_source = InMemoryDataSource()
182
+
183
+ R = TypeRegistry()
184
+ Relay = R.Mixin(RelayMixin, data_source)
185
+ ```
186
+
187
+ # ## Node Definition
188
+ Once `RelayMixin` has been mixed into the Registry, things can subclass `Node` automatically!
189
+
190
+ ```python
191
+
192
+ class Pet(R.Implements[Relay.Node]):
193
+ name = R.String
194
+
195
+ ```
196
+
197
+ # ## Connection Definition & `NodeField`
198
+ Connections can be defined upon any object type . Here we' ll make a `Query` root node that provides a connection
199
+ to a list of pets & a node field to resolve an indivudal node.
200
+
201
+ ```python
202
+
203
+ class Query(R.ObjectType):
204
+ pets = Relay.Connection(' Pet' , R.Pet) # The duplicate 'Pet' definition is just temporary and will be removed.
205
+ node = Relay.NodeField
206
+
207
+ ```
208
+
209
+ # ## Adding some data!
210
+ Let' s add some pets to the `data_source` and query them!
211
+
212
+ ```python
213
+
214
+ # Schema has to be defined so that all thunks are resolved before we can use `Pet` as a container.
215
+ Schema = R.Schema(R.Query)
216
+
217
+ pet_names = [" Max" , " Buddy" , " Charlie" , " Jack" , " Cooper" , " Rocky" ]
218
+
219
+ for i, pet_name in enumerate (pet_names, 1 ):
220
+ data_source.add(Pet(id = i, name = pet_name))
221
+
222
+ ```
223
+
224
+
225
+ # ## Running Relay Connection Query:
226
+
227
+ ```python
228
+
229
+ result = graphql(Schema, '''
230
+ {
231
+ pets(first: 5) {
232
+ edges {
233
+ node {
234
+ id
235
+ name
236
+ }
237
+ cursor
238
+ }
239
+ pageInfo {
240
+ hasPreviousPage
241
+ hasNextPage
242
+ startCursor
243
+ endCursor
244
+ }
245
+ }
246
+ node(id: "UGV0OjU=") {
247
+ id
248
+ ... on Pet {
249
+ name
250
+ }
251
+ }
252
+ }
253
+ ''' )
254
+ ```
255
+
256
+
167
257
# # Mutations (Coming Soon)
168
258
169
- Epoxy also supports defining mutations:
259
+ Epoxy also supports defining mutations. Making a Mutation a Relay mutation is as simple as changing `R.Mutation` to
260
+ `Relay.Mutation` .
170
261
171
262
172
263
```python
0 commit comments