@@ -216,3 +216,110 @@ def test_get_by_id(self):
216
216
assert not result .errors
217
217
assert result .data ["user" ]["id" ] == self .user_list [1 ]["id" ]
218
218
assert result .data ["user" ]["name" ] == self .user_list [1 ]["name" ]
219
+
220
+
221
+ class TestIncompleteCustomGlobalID :
222
+ def setup (self ):
223
+ self .user_list = [
224
+ {"id" : 1 , "name" : "First" },
225
+ {"id" : 2 , "name" : "Second" },
226
+ {"id" : 3 , "name" : "Third" },
227
+ {"id" : 4 , "name" : "Fourth" },
228
+ ]
229
+ self .users = {user ["id" ]: user for user in self .user_list }
230
+
231
+ def test_must_define_to_global_id (self ):
232
+ """
233
+ Test that if the `to_global_id` method is not defined, we can query the object, but we can't request its ID.
234
+ """
235
+
236
+ class CustomGlobalIDType (BaseGlobalIDType ):
237
+ graphene_type = Int
238
+
239
+ @classmethod
240
+ def resolve_global_id (cls , info , global_id ):
241
+ _type = info .return_type .graphene_type ._meta .name
242
+ return _type , global_id
243
+
244
+ class CustomNode (Node ):
245
+ class Meta :
246
+ global_id_type = CustomGlobalIDType
247
+
248
+ class User (ObjectType ):
249
+ class Meta :
250
+ interfaces = [CustomNode ]
251
+
252
+ name = String ()
253
+
254
+ @classmethod
255
+ def get_node (cls , _type , _id ):
256
+ return self .users [_id ]
257
+
258
+ class RootQuery (ObjectType ):
259
+ user = CustomNode .Field (User )
260
+
261
+ self .schema = Schema (query = RootQuery , types = [User ])
262
+ self .graphql_schema = self .schema .graphql_schema
263
+
264
+ query = """query {
265
+ user(id: 2) {
266
+ name
267
+ }
268
+ }"""
269
+ result = graphql_sync (self .graphql_schema , query )
270
+ assert not result .errors
271
+ assert result .data ["user" ]["name" ] == self .user_list [1 ]["name" ]
272
+
273
+ query = """query {
274
+ user(id: 2) {
275
+ id
276
+ name
277
+ }
278
+ }"""
279
+ result = graphql_sync (self .graphql_schema , query )
280
+ assert result .errors is not None
281
+ assert len (result .errors ) == 1
282
+ assert result .errors [0 ].path == ["user" , "id" ]
283
+
284
+ def test_must_define_resolve_global_id (self ):
285
+ """
286
+ Test that if the `resolve_global_id` method is not defined, we can't query the object by ID.
287
+ """
288
+
289
+ class CustomGlobalIDType (BaseGlobalIDType ):
290
+ graphene_type = Int
291
+
292
+ @classmethod
293
+ def to_global_id (cls , _type , _id ):
294
+ return _id
295
+
296
+ class CustomNode (Node ):
297
+ class Meta :
298
+ global_id_type = CustomGlobalIDType
299
+
300
+ class User (ObjectType ):
301
+ class Meta :
302
+ interfaces = [CustomNode ]
303
+
304
+ name = String ()
305
+
306
+ @classmethod
307
+ def get_node (cls , _type , _id ):
308
+ return self .users [_id ]
309
+
310
+ class RootQuery (ObjectType ):
311
+ user = CustomNode .Field (User )
312
+
313
+ self .schema = Schema (query = RootQuery , types = [User ])
314
+ self .graphql_schema = self .schema .graphql_schema
315
+
316
+ query = """query {
317
+ user(id: 2) {
318
+ id
319
+ name
320
+ }
321
+ }"""
322
+ result = graphql_sync (self .graphql_schema , query )
323
+ assert result .errors is not None
324
+ assert len (result .errors ) == 1
325
+ assert result .errors [0 ].path == ["user" ]
0 commit comments