File: test_interface.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 (425 lines) | stat: -rw-r--r-- 11,197 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
from dataclasses import dataclass
from typing import Any

import pytest
from pytest_mock import MockerFixture

import strawberry
from strawberry.types.base import StrawberryObjectDefinition


def test_query_interface():
    @strawberry.interface
    class Cheese:
        name: str

    @strawberry.type
    class Swiss(Cheese):
        canton: str

    @strawberry.type
    class Italian(Cheese):
        province: str

    @strawberry.type
    class Root:
        @strawberry.field
        def assortment(self) -> list[Cheese]:
            return [
                Italian(name="Asiago", province="Friuli"),
                Swiss(name="Tomme", canton="Vaud"),
            ]

    schema = strawberry.Schema(query=Root, types=[Swiss, Italian])

    query = """{
        assortment {
            name
            ... on Italian { province }
            ... on Swiss { canton }
        }
    }"""

    result = schema.execute_sync(query)

    assert not result.errors
    assert result.data is not None
    assert result.data["assortment"] == [
        {"name": "Asiago", "province": "Friuli"},
        {"canton": "Vaud", "name": "Tomme"},
    ]


def test_interfaces_can_implement_other_interfaces():
    @strawberry.interface
    class Error:
        message: str

    @strawberry.interface
    class FieldError(Error):
        message: str
        field: str

    @strawberry.type
    class PasswordTooShort(FieldError):
        message: str
        field: str
        fix: str

    @strawberry.type
    class Query:
        @strawberry.field
        def always_error(self) -> Error:
            return PasswordTooShort(
                message="Password Too Short",
                field="Password",
                fix="Choose more characters",
            )

    schema = strawberry.Schema(Query, types=[PasswordTooShort])
    query = """{
        alwaysError {
            ... on Error {
                message
            }
            ... on FieldError {
                field
            }
            ... on PasswordTooShort {
                fix
            }
        }
    }"""

    result = schema.execute_sync(query)

    assert not result.errors
    assert result.data is not None
    assert result.data["alwaysError"] == {
        "message": "Password Too Short",
        "field": "Password",
        "fix": "Choose more characters",
    }


def test_interface_duck_typing():
    @strawberry.interface
    class Entity:
        id: int

    @strawberry.type
    class Anime(Entity):
        name: str

        @classmethod
        def is_type_of(cls, obj: Any, _) -> bool:
            return isinstance(obj, AnimeORM)

    @dataclass
    class AnimeORM:
        id: int
        name: str

    @strawberry.type
    class Query:
        @strawberry.field
        def anime(self) -> Entity:
            return AnimeORM(id=1, name="One Piece")  # type: ignore

    schema = strawberry.Schema(query=Query, types=[Anime])

    query = """{
        anime { id ... on Anime { name } }
    }"""

    result = schema.execute_sync(query)

    assert not result.errors
    assert result.data == {"anime": {"id": 1, "name": "One Piece"}}


def test_interface_explicit_type_resolution():
    @dataclass
    class AnimeORM:
        id: int
        name: str

    @strawberry.interface
    class Node:
        id: int

    @strawberry.type
    class Anime(Node):
        name: str

        @classmethod
        def is_type_of(cls, obj: Any, _) -> bool:
            return isinstance(obj, AnimeORM)

    @strawberry.type
    class Query:
        @strawberry.field
        def node(self) -> Node:
            return AnimeORM(id=1, name="One Piece")  # type: ignore

    schema = strawberry.Schema(query=Query, types=[Anime])

    query = "{ node { __typename, id ... on Anime { name }} }"
    result = schema.execute_sync(query)

    assert not result.errors
    assert result.data == {
        "node": {
            "__typename": "Anime",
            "id": 1,
            "name": "One Piece",
        }
    }


@pytest.mark.xfail(reason="We don't support returning dictionaries yet")
def test_interface_duck_typing_returning_dict():
    @strawberry.interface
    class Entity:
        id: int

    @strawberry.type
    class Anime(Entity):
        name: str

    @strawberry.type
    class Query:
        @strawberry.field
        def anime(self) -> Anime:
            return {"id": 1, "name": "One Piece"}  # type: ignore

    schema = strawberry.Schema(query=Query)

    query = """{
        anime { name }
    }"""

    result = schema.execute_sync(query)

    assert not result.errors
    assert result.data == {"anime": {"name": "One Piece"}}


def test_duplicated_interface_in_multi_inheritance():
    """Test that interfaces are gathered properly via CPython's MRO.

    Previously interfaces were duplicated within a "Diamond Problem" inheritance
    scenario which is tested here. Using the MRO instead of the `__bases__` attribute of
    a class in :py:func:`strawberry.object_type._get_interfaces` allows Python's C3
    linearization algorithm to create a consistent precedents graph without duplicates.
    """

    @strawberry.interface
    class Base:
        id: str

    @strawberry.interface
    class InterfaceA(Base):
        id: str
        field_a: str

    @strawberry.interface
    class InterfaceB(Base):
        id: str
        field_b: str

    @strawberry.type
    class MyType(InterfaceA, InterfaceB):
        id: str
        field_a: str
        field_b: str

    @strawberry.type
    class Query:
        my_type: MyType

    type_definition: StrawberryObjectDefinition = MyType.__strawberry_definition__
    origins = [i.origin for i in type_definition.interfaces]
    assert origins == [InterfaceA, InterfaceB, Base]

    strawberry.Schema(Query)  # Final sanity check to ensure schema compiles


def test_interface_resolve_type(mocker: MockerFixture):
    """Check that the default implemenetation of `resolve_type` functions as expected.

    In this test-case the default implementation of `resolve_type` defined in
    `GraphQLCoreConverter.from_interface`, should immediately resolve the type of the
    returned concrete object. A concrete object is defined as one that is an instance of
    the interface it implements.

    Before the default implementation of `resolve_type`, the `is_type_of` methods of all
    specializations of an interface (in this case Anime & Movie) would be called. As
    this needlessly reduces performance, this test checks if only `Anime.is_type_of` is
    called when `Query.node` returns an `Anime` object.
    """

    class IsTypeOfTester:
        @classmethod
        def is_type_of(cls, obj: Any, _) -> bool:
            return isinstance(obj, cls)

    spy_is_type_of = mocker.spy(IsTypeOfTester, "is_type_of")

    @strawberry.interface
    class Node:
        id: int

    @strawberry.type
    class Anime(Node, IsTypeOfTester):
        name: str

    @strawberry.type
    class Movie(Node):
        title: str

        @classmethod
        def is_type_of(cls, *args: Any, **kwargs: Any) -> bool:
            del args, kwargs
            raise RuntimeError("Movie.is_type_of shouldn't have been called")

    @strawberry.type
    class Query:
        @strawberry.field
        def node(self) -> Node:
            return Anime(id=1, name="One Pierce")

    schema = strawberry.Schema(query=Query, types=[Anime, Movie])

    query = "{ node {  __typename, id } }"
    result = schema.execute_sync(query)

    assert not result.errors
    assert result.data == {"node": {"__typename": "Anime", "id": 1}}
    spy_is_type_of.assert_called_once()


def test_interface_specialized_resolve_type(mocker: MockerFixture):
    """Test that a specialized ``resolve_type`` is called."""

    class InterfaceTester:
        @classmethod
        def resolve_type(cls, obj: Any, *args: Any, **kwargs: Any) -> str:
            del args, kwargs
            return obj.__strawberry_definition__.name

    spy_resolve_type = mocker.spy(InterfaceTester, "resolve_type")

    @strawberry.interface
    class Food(InterfaceTester):
        id: int

    @strawberry.type
    class Fruit(Food):
        name: str

    @strawberry.type
    class Query:
        @strawberry.field
        def food(self) -> Food:
            return Fruit(id=1, name="strawberry")

    schema = strawberry.Schema(query=Query, types=[Fruit])
    result = schema.execute_sync("query { food { ... on Fruit { name } } }")

    assert not result.errors
    assert result.data == {"food": {"name": "strawberry"}}
    spy_resolve_type.assert_called_once()


@pytest.mark.asyncio
async def test_derived_interface(mocker: MockerFixture):
    """Test if correct resolve_type is called on a derived interface."""

    class NodeInterfaceTester:
        @classmethod
        def resolve_type(cls, obj: Any, *args: Any, **kwargs: Any) -> str:
            del args, kwargs
            return obj.__strawberry_definition__.name

    class NamedNodeInterfaceTester:
        @classmethod
        def resolve_type(cls, obj: Any, *args: Any, **kwargs: Any) -> str:
            del args, kwargs
            return obj.__strawberry_definition__.name

    spy_node_resolve_type = mocker.spy(NodeInterfaceTester, "resolve_type")
    spy_named_node_resolve_type = mocker.spy(NamedNodeInterfaceTester, "resolve_type")

    @strawberry.interface
    class Node(NodeInterfaceTester):
        id: int

    @strawberry.interface
    class NamedNode(NamedNodeInterfaceTester, Node):
        name: str

    @strawberry.type
    class Person(NamedNode):
        pass

    @strawberry.type
    class Query:
        @strawberry.field
        def friends(self) -> list[NamedNode]:
            return [Person(id=1, name="foo"), Person(id=2, name="bar")]

    schema = strawberry.Schema(Query, types=[Person])
    result = await schema.execute("query { friends { name } }")

    assert not result.errors
    assert result.data == {"friends": [{"name": "foo"}, {"name": "bar"}]}

    assert result.data is not None
    assert spy_named_node_resolve_type.call_count == len(result.data["friends"])
    spy_node_resolve_type.assert_not_called()


def test_resolve_type_on_interface_returning_interface():
    @strawberry.interface
    class Node:
        id: strawberry.ID

        @classmethod
        def resolve_type(cls, obj: Any, *args: Any, **kwargs: Any) -> str:
            return "Video" if obj.id == "1" else "Image"

    @strawberry.type
    class Video(Node): ...

    @strawberry.type
    class Image(Node): ...

    @strawberry.type
    class Query:
        @strawberry.field
        def node(self, id: strawberry.ID) -> Node:
            return Node(id=id)

    schema = strawberry.Schema(query=Query, types=[Video, Image])

    query = """
        query {
            one: node(id: "1") {
                __typename
                id
            }
            two: node(id: "2") {
                __typename
                id
            }
        }
    """

    result = schema.execute_sync(query)

    assert not result.errors

    assert result.data
    assert result.data["one"] == {"id": "1", "__typename": "Video"}
    assert result.data["two"] == {"id": "2", "__typename": "Image"}