File: test_generic_objects.py

package info (click to toggle)
strawberry-graphql 0.306.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 18,176 kB
  • sloc: javascript: 178,052; python: 65,643; sh: 33; makefile: 25
file content (644 lines) | stat: -rw-r--r-- 17,647 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
import datetime
from typing import Annotated, Generic, TypeVar

import pytest

import strawberry
from strawberry.types.base import (
    StrawberryList,
    StrawberryOptional,
    StrawberryTypeVar,
    get_object_definition,
)
from strawberry.types.union import StrawberryUnion

T = TypeVar("T")


def test_basic_generic():
    directive = object()

    @strawberry.type
    class Edge(Generic[T]):
        node_field: T = strawberry.field(directives=[directive])

    definition = get_object_definition(Edge, strict=True)
    assert definition.is_graphql_generic
    assert definition.type_params == [T]

    [field] = definition.fields
    assert field.python_name == "node_field"
    assert isinstance(field.type, StrawberryTypeVar)
    assert field.type.type_var is T

    # let's make a copy of this generic type
    copy = get_object_definition(Edge, strict=True).copy_with({"T": str})

    definition_copy = get_object_definition(copy, strict=True)

    assert not definition_copy.is_graphql_generic
    assert definition_copy.type_params == []

    [field_copy] = definition_copy.fields
    assert field_copy.python_name == "node_field"
    assert field_copy.type is str
    assert field_copy.directives == [directive]


def test_generics_nested():
    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class Connection(Generic[T]):
        edge: Edge[T]

    definition = get_object_definition(Connection, strict=True)
    assert definition.is_graphql_generic
    assert definition.type_params == [T]

    [field] = definition.fields
    assert field.python_name == "edge"
    assert get_object_definition(field.type, strict=True).type_params == [T]

    # let's make a copy of this generic type
    definition_copy = get_object_definition(
        get_object_definition(Connection, strict=True).copy_with({"T": str}),
        strict=True,
    )

    assert not definition_copy.is_graphql_generic
    assert definition_copy.type_params == []

    [field_copy] = definition_copy.fields
    assert field_copy.python_name == "edge"


def test_generics_name():
    @strawberry.type(name="AnotherName")
    class EdgeName:
        node: str

    @strawberry.type
    class Connection(Generic[T]):
        edge: T

    definition_copy = get_object_definition(
        get_object_definition(Connection, strict=True).copy_with({"T": EdgeName}),
        strict=True,
    )

    assert not definition_copy.is_graphql_generic
    assert definition_copy.type_params == []

    [field_copy] = definition_copy.fields
    assert field_copy.python_name == "edge"


def test_generics_nested_in_list():
    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class Connection(Generic[T]):
        edges: list[Edge[T]]

    definition = get_object_definition(Connection, strict=True)
    assert definition.is_graphql_generic
    assert definition.type_params == [T]

    [field] = definition.fields
    assert field.python_name == "edges"
    assert isinstance(field.type, StrawberryList)
    assert get_object_definition(field.type.of_type, strict=True).type_params == [T]

    # let's make a copy of this generic type
    definition_copy = get_object_definition(
        definition.copy_with({"T": str}),
        strict=True,
    )

    assert not definition_copy.is_graphql_generic
    assert definition_copy.type_params == []

    [field_copy] = definition_copy.fields
    assert field_copy.python_name == "edges"
    assert isinstance(field_copy.type, StrawberryList)


def test_list_inside_generic():
    T = TypeVar("T")

    @strawberry.type
    class Value(Generic[T]):
        valuation_date: datetime.date
        value: T

    @strawberry.type
    class Foo:
        string: Value[str]
        strings: Value[list[str]]
        optional_string: Value[str | None]
        optional_strings: Value[list[str] | None]

    definition = get_object_definition(Foo, strict=True)
    assert not definition.is_graphql_generic

    [
        string_field,
        strings_field,
        optional_string_field,
        optional_strings_field,
    ] = definition.fields
    assert string_field.python_name == "string"
    assert strings_field.python_name == "strings"
    assert optional_string_field.python_name == "optional_string"
    assert optional_strings_field.python_name == "optional_strings"


def test_generic_with_optional():
    @strawberry.type
    class Edge(Generic[T]):
        node: T | None

    definition = get_object_definition(Edge, strict=True)
    assert definition.is_graphql_generic
    assert definition.type_params == [T]

    [field] = definition.fields
    assert field.python_name == "node"
    assert isinstance(field.type, StrawberryOptional)
    assert isinstance(field.type.of_type, StrawberryTypeVar)
    assert field.type.of_type.type_var is T

    # let's make a copy of this generic type
    definition_copy = get_object_definition(
        definition.copy_with({"T": str}), strict=True
    )

    assert not definition_copy.is_graphql_generic
    assert definition_copy.type_params == []

    [field_copy] = definition_copy.fields
    assert field_copy.python_name == "node"
    assert isinstance(field_copy.type, StrawberryOptional)
    assert field_copy.type.of_type is str


def test_generic_with_list():
    @strawberry.type
    class Connection(Generic[T]):
        edges: list[T]

    definition = get_object_definition(Connection, strict=True)
    assert definition.is_graphql_generic
    assert definition.type_params == [T]

    [field] = definition.fields
    assert field.python_name == "edges"
    assert isinstance(field.type, StrawberryList)
    assert isinstance(field.type.of_type, StrawberryTypeVar)
    assert field.type.of_type.type_var is T

    # let's make a copy of this generic type
    definition_copy = get_object_definition(
        definition.copy_with({"T": str}), strict=True
    )

    assert not definition_copy.is_graphql_generic
    assert definition_copy.type_params == []

    [field_copy] = definition_copy.fields
    assert field_copy.python_name == "edges"
    assert isinstance(field_copy.type, StrawberryList)
    assert field_copy.type.of_type is str


def test_generic_with_list_of_optionals():
    @strawberry.type
    class Connection(Generic[T]):
        edges: list[T | None]

    definition = get_object_definition(Connection, strict=True)
    assert definition.is_graphql_generic
    assert definition.type_params == [T]

    [field] = definition.fields
    assert field.python_name == "edges"
    assert isinstance(field.type, StrawberryList)
    assert isinstance(field.type.of_type, StrawberryOptional)
    assert isinstance(field.type.of_type.of_type, StrawberryTypeVar)
    assert field.type.of_type.of_type.type_var is T

    # let's make a copy of this generic type
    definition_copy = get_object_definition(
        definition.copy_with({"T": str}), strict=True
    )

    assert not definition_copy.is_graphql_generic
    assert definition_copy.type_params == []

    [field_copy] = definition_copy.fields
    assert field_copy.python_name == "edges"
    assert isinstance(field_copy.type, StrawberryList)
    assert isinstance(field_copy.type.of_type, StrawberryOptional)
    assert field_copy.type.of_type.of_type is str


def test_generics_with_unions():
    @strawberry.type
    class Error:
        message: str

    @strawberry.type
    class Edge(Generic[T]):
        node: Error | T

    definition = get_object_definition(Edge, strict=True)
    assert definition.type_params == [T]

    [field] = definition.fields
    assert field.python_name == "node"
    assert isinstance(field.type, StrawberryUnion)
    assert field.type.types == (Error, T)

    # let's make a copy of this generic type
    @strawberry.type
    class Node:
        name: str

    definition_copy = get_object_definition(
        definition.copy_with({"T": Node}), strict=True
    )

    assert not definition_copy.is_graphql_generic
    assert definition_copy.type_params == []

    [field_copy] = definition_copy.fields
    assert field_copy.python_name == "node"
    assert isinstance(field_copy.type, StrawberryUnion)
    assert field_copy.type.types == (Error, Node)


def test_using_generics():
    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class User:
        name: str

    @strawberry.type
    class Query:
        user: Edge[User]

    definition = get_object_definition(Query, strict=True)

    [field] = definition.fields
    assert field.python_name == "user"

    user_edge_definition = get_object_definition(field.type, strict=True)
    assert not user_edge_definition.is_graphql_generic

    [node_field] = user_edge_definition.fields
    assert node_field.python_name == "node"
    assert node_field.type is User


def test_using_generics_nested():
    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class Connection(Generic[T]):
        edges: Edge[T]

    @strawberry.type
    class User:
        name: str

    @strawberry.type
    class Query:
        users: Connection[User]

    connection_definition = get_object_definition(Connection, strict=True)
    assert connection_definition.is_graphql_generic
    assert connection_definition.type_params == [T]

    query_definition = get_object_definition(Query, strict=True)

    [user_field] = query_definition.fields
    assert user_field.python_name == "users"

    user_connection_definition = get_object_definition(user_field.type, strict=True)
    assert not user_connection_definition.is_graphql_generic

    [edges_field] = user_connection_definition.fields
    assert edges_field.python_name == "edges"


def test_using_generics_raises_when_missing_annotation():
    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class User:
        name: str

    error_message = (
        f'Query fields cannot be resolved. The type "{Edge!r}" '
        "is generic, but no type has been passed"
    )

    @strawberry.type
    class Query:
        user: Edge

    with pytest.raises(TypeError, match=error_message):
        strawberry.Schema(Query)


def test_using_generics_raises_when_missing_annotation_nested():
    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class Connection(Generic[T]):
        edges: list[Edge[T]]

    @strawberry.type
    class User:
        name: str

    error_message = (
        f'Query fields cannot be resolved. The type "{Connection!r}" '
        "is generic, but no type has been passed"
    )

    @strawberry.type
    class Query:
        users: Connection

    with pytest.raises(TypeError, match=error_message):
        strawberry.Schema(Query)


def test_generics_inside_optional():
    @strawberry.type
    class Error:
        message: str

    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class Query:
        user: Edge[str] | None

    query_definition = get_object_definition(Query, strict=True)
    assert query_definition.type_params == []

    [field] = query_definition.fields
    assert field.python_name == "user"
    assert isinstance(field.type, StrawberryOptional)

    str_edge_definition = get_object_definition(field.type.of_type, strict=True)
    assert not str_edge_definition.is_graphql_generic


def test_generics_inside_list():
    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class Query:
        user: list[Edge[str]]

    query_definition = get_object_definition(Query, strict=True)
    assert query_definition.type_params == []

    [field] = query_definition.fields
    assert field.python_name == "user"
    assert isinstance(field.type, StrawberryList)

    str_edge_definition = get_object_definition(field.type.of_type, strict=True)
    assert not str_edge_definition.is_graphql_generic


def test_generics_inside_unions():
    @strawberry.type
    class Error:
        message: str

    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class Query:
        user: Edge[str] | Error

    query_definition = get_object_definition(Query, strict=True)
    assert query_definition.type_params == []

    [field] = query_definition.fields
    assert field.python_name == "user"
    assert not isinstance(field.type, StrawberryOptional)

    union = field.type
    assert isinstance(union, StrawberryUnion)
    assert not get_object_definition(union.types[0], strict=True).is_graphql_generic


def test_multiple_generics_inside_unions():
    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.type
    class Query:
        user: Edge[int] | Edge[str]

    query_definition = get_object_definition(Query, strict=True)
    assert query_definition.type_params == []

    [user_field] = query_definition.fields
    assert user_field.python_name == "user"
    assert not isinstance(user_field.type, StrawberryOptional)

    union = user_field.type
    assert isinstance(union, StrawberryUnion)

    int_edge_definition = get_object_definition(union.types[0], strict=True)
    assert not int_edge_definition.is_graphql_generic
    assert int_edge_definition.fields[0].type is int

    str_edge_definition = get_object_definition(union.types[1], strict=True)
    assert not str_edge_definition.is_graphql_generic
    assert str_edge_definition.fields[0].type is str


def test_union_inside_generics():
    @strawberry.type
    class Dog:
        name: str

    @strawberry.type
    class Cat:
        name: str

    @strawberry.type
    class Connection(Generic[T]):
        nodes: list[T]

    DogCat = Annotated[Dog | Cat, strawberry.union("DogCat")]

    @strawberry.type
    class Query:
        connection: Connection[DogCat]

    query_definition = get_object_definition(Query, strict=True)
    assert query_definition.type_params == []

    [connection_field] = query_definition.fields
    assert connection_field.python_name == "connection"
    assert not isinstance(connection_field, StrawberryOptional)

    dog_cat_connection_definition = get_object_definition(
        connection_field.type, strict=True
    )

    [node_field] = dog_cat_connection_definition.fields
    assert isinstance(node_field.type, StrawberryList)

    union = dog_cat_connection_definition.fields[0].type.of_type
    assert isinstance(union, StrawberryUnion)


def test_anonymous_union_inside_generics():
    @strawberry.type
    class Dog:
        name: str

    @strawberry.type
    class Cat:
        name: str

    @strawberry.type
    class Connection(Generic[T]):
        nodes: list[T]

    @strawberry.type
    class Query:
        connection: Connection[Dog | Cat]

    definition = get_object_definition(Query, strict=True)
    assert definition.type_params == []

    [connection_field] = definition.fields
    assert connection_field.python_name == "connection"

    dog_cat_connection_definition = get_object_definition(
        connection_field.type, strict=True
    )

    [node_field] = dog_cat_connection_definition.fields
    assert isinstance(node_field.type, StrawberryList)

    union = node_field.type.of_type
    assert isinstance(union, StrawberryUnion)


def test_using_generics_with_interfaces():
    @strawberry.type
    class Edge(Generic[T]):
        node: T

    @strawberry.interface
    class WithName:
        name: str

    @strawberry.type
    class Query:
        user: Edge[WithName]

    query_definition = get_object_definition(Query, strict=True)

    [user_field] = query_definition.fields
    assert user_field.python_name == "user"

    with_name_definition = get_object_definition(user_field.type, strict=True)
    assert not with_name_definition.is_graphql_generic

    [node_field] = with_name_definition.fields
    assert node_field.python_name == "node"
    assert node_field.type is WithName


def test_generic_with_arguments():
    T = TypeVar("T")

    @strawberry.type
    class Collection(Generic[T]):
        @strawberry.field
        def by_id(self, ids: list[int]) -> list[T]:
            return []

    @strawberry.type
    class Post:
        name: str

    @strawberry.type
    class Query:
        user: Collection[Post]

    query_definition = get_object_definition(Query, strict=True)

    [user_field] = query_definition.fields
    assert user_field.python_name == "user"

    post_collection_definition = get_object_definition(user_field.type, strict=True)
    assert not post_collection_definition.is_graphql_generic

    [by_id_field] = post_collection_definition.fields
    assert by_id_field.python_name == "by_id"
    assert isinstance(by_id_field.type, StrawberryList)
    assert by_id_field.type.of_type is Post

    [ids_argument] = by_id_field.arguments
    assert ids_argument.python_name == "ids"
    assert isinstance(ids_argument.type, StrawberryList)
    assert ids_argument.type.of_type is int


def test_federation():
    @strawberry.federation.type(keys=["id"])
    class Edge(Generic[T]):
        id: strawberry.ID
        node_field: T

    definition = get_object_definition(Edge, strict=True)

    definition_copy = get_object_definition(
        definition.copy_with({"T": str}), strict=True
    )

    assert not definition_copy.is_graphql_generic
    assert definition_copy.type_params == []
    assert definition_copy.directives == definition.directives

    [field1_copy, field2_copy] = definition_copy.fields

    assert field1_copy.python_name == "id"
    assert field1_copy.type is strawberry.ID

    assert field2_copy.python_name == "node_field"
    assert field2_copy.type is str