File: test_forward_references.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 (271 lines) | stat: -rw-r--r-- 6,053 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
# type: ignore
from __future__ import annotations

import textwrap
from typing import Annotated

import strawberry
from strawberry.printer import print_schema
from strawberry.scalars import JSON
from strawberry.types.base import StrawberryList, StrawberryOptional
from tests.a import A
from tests.d import D


def test_forward_reference():
    global MyType

    @strawberry.type
    class Query:
        me: MyType = strawberry.field(name="myself")

    @strawberry.type
    class MyType:
        id: strawberry.ID
        scalar: JSON
        optional_scalar: JSON | None

    expected_representation = '''
    """
    The `JSON` scalar type represents JSON values as specified by [ECMA-404](https://ecma-international.org/wp-content/uploads/ECMA-404_2nd_edition_december_2017.pdf).
    """
    scalar JSON @specifiedBy(url: "https://ecma-international.org/wp-content/uploads/ECMA-404_2nd_edition_december_2017.pdf")

    type MyType {
      id: ID!
      scalar: JSON!
      optionalScalar: JSON
    }

    type Query {
      myself: MyType!
    }
    '''

    schema = strawberry.Schema(Query)

    assert print_schema(schema) == textwrap.dedent(expected_representation).strip()

    del MyType


def test_lazy_forward_reference():
    @strawberry.type
    class Query:
        @strawberry.field
        async def a(self) -> A:  # pragma: no cover
            return A(id=strawberry.ID("1"))

    expected_representation = """
    type A {
      id: ID!
      b: B!
      optionalB: B
      optionalB2: B
    }

    type B {
      id: ID!
      a: A!
      aList: [A!]!
      optionalA: A
      optionalA2: A
    }

    type Query {
      a: A!
    }
    """

    schema = strawberry.Schema(query=Query)
    assert print_schema(schema) == textwrap.dedent(expected_representation).strip()


def test_lazy_forward_reference_schema_with_a_list_only():
    @strawberry.type
    class Query:
        @strawberry.field
        async def d(self) -> D:  # pragma: no cover
            return D(id=strawberry.ID("1"))

    expected_representation = """
    type C {
      id: ID!
    }

    type D {
      id: ID!
      cList: [C!]!
    }

    type Query {
      d: D!
    }
    """

    schema = strawberry.Schema(query=Query)
    assert print_schema(schema) == textwrap.dedent(expected_representation).strip()


def test_with_resolver():
    global User

    @strawberry.type
    class User:
        name: str

    def get_users() -> list[User]:  # pragma: no cover
        return []

    @strawberry.type
    class Query:
        users: list[User] = strawberry.field(resolver=get_users)

    definition = Query.__strawberry_definition__
    assert definition.name == "Query"

    [field] = definition.fields

    assert field.python_name == "users"
    assert isinstance(field.type, StrawberryList)
    assert field.type.of_type is User

    del User


def test_union_or_notation():
    global User

    @strawberry.type
    class User:
        name: str

    def get_users() -> list[User] | None:  # pragma: no cover
        return []

    @strawberry.type
    class Query:
        users: list[User] | None = strawberry.field(resolver=get_users)

    definition = Query.__strawberry_definition__
    assert definition.name == "Query"

    [field] = definition.fields

    assert field.python_name == "users"
    assert isinstance(field.type, StrawberryOptional)
    assert isinstance(field.type.of_type, StrawberryList)
    assert field.type.of_type.of_type is User

    del User


def test_union_or_notation_generic_type_alias():
    global User

    @strawberry.type
    class User:
        name: str

    def get_users() -> list[User] | None:  # pragma: no cover
        return []

    @strawberry.type
    class Query:
        users: list[User] | None = strawberry.field(resolver=get_users)

    definition = Query.__strawberry_definition__
    assert definition.name == "Query"

    [field] = definition.fields

    assert field.python_name == "users"
    assert isinstance(field.type, StrawberryOptional)
    assert isinstance(field.type.of_type, StrawberryList)
    assert field.type.of_type.of_type is User

    del User


def test_annotated():
    global User

    @strawberry.type
    class User:
        name: str

    def get_users() -> list[User]:  # pragma: no cover
        return []

    @strawberry.type
    class Query:
        users: Annotated[list[User], object()] = strawberry.field(resolver=get_users)

    definition = Query.__strawberry_definition__
    assert definition.name == "Query"

    [field] = definition.fields

    assert field.python_name == "users"
    assert isinstance(field.type, StrawberryList)
    assert field.type.of_type is User

    del User


def test_annotated_or_notation():
    global User

    @strawberry.type
    class User:
        name: str

    def get_users() -> list[User] | None:  # pragma: no cover
        return []

    @strawberry.type
    class Query:
        users: Annotated[list[User] | None, object()] = strawberry.field(
            resolver=get_users
        )

    definition = Query.__strawberry_definition__
    assert definition.name == "Query"

    [field] = definition.fields

    assert field.python_name == "users"
    assert isinstance(field.type, StrawberryOptional)
    assert isinstance(field.type.of_type, StrawberryList)
    assert field.type.of_type.of_type is User

    del User


def test_annotated_or_notation_generic_type_alias():
    global User

    @strawberry.type
    class User:
        name: str

    def get_users() -> list[User]:  # pragma: no cover
        return []

    @strawberry.type
    class Query:
        users: Annotated[list[User] | None, object()] = strawberry.field(
            resolver=get_users
        )

    definition = Query.__strawberry_definition__
    assert definition.name == "Query"

    [field] = definition.fields

    assert field.python_name == "users"
    assert isinstance(field.type, StrawberryOptional)
    assert isinstance(field.type.of_type, StrawberryList)
    assert field.type.of_type.of_type is User

    del User