-
Notifications
You must be signed in to change notification settings - Fork 44
/
Copy pathtest_forward_refs.py
107 lines (81 loc) · 2.45 KB
/
test_forward_refs.py
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
import sys
import typing as T
import uuid
import graphene
import pydantic
import pytest
from graphene_pydantic import PydanticObjectType
if sys.version_info < (3, 7):
pytest.skip("ForwardRefs feature requires Python 3.7+", allow_module_level=True)
class FooModel(pydantic.BaseModel):
id: uuid.UUID
name: str
bar: "BarModel" = None
baz: T.Optional["BazModel"] = None
some_bars: T.Optional[T.List["BarModel"]] = None
class BazModel(pydantic.BaseModel):
name: str
bar: "BarModel" = None
class BarModel(pydantic.BaseModel):
id: uuid.UUID
name: str
foo: FooModel
# deliberately in this order
BazModel.model_rebuild()
class Foo(PydanticObjectType):
class Meta:
model = FooModel
class Bar(PydanticObjectType):
class Meta:
model = BarModel
class Baz(PydanticObjectType):
class Meta:
model = BazModel
# yes this is deliberately after too
FooModel.model_rebuild()
Foo.resolve_placeholders()
Bar.resolve_placeholders()
Baz.resolve_placeholders()
class Query(graphene.ObjectType):
list_foos = graphene.List(Foo)
def resolve_list_foos(self, info):
"""Dummy resolver that creates a list of Pydantic objects"""
first_foo = FooModel(id=uuid.uuid4(), name="foo")
shared_bar = BarModel(id=uuid.uuid4(), name="shared", foo=first_foo)
first_foo.bar = shared_bar
first_foo.baz = BazModel(name="baz", bar=shared_bar)
first_foo.some_bars = [shared_bar]
return [
first_foo,
FooModel(id=uuid.uuid4(), name="baz", bar=shared_bar, maybe_bar=shared_bar),
FooModel(id=uuid.uuid4(), name="quux", bar=shared_bar, some_bars=[]),
]
def test_query():
schema = graphene.Schema(query=Query)
query = """
query {
listFoos {
id
name
bar {
id
name
foo {
id
baz {
name
bar { id }
}
}
}
baz { name }
someBars { id }
}
}
"""
result = schema.execute(query)
assert result.errors is None
assert result.data is not None
data = result.data
assert data["listFoos"][0]["bar"] is not None
assert data["listFoos"][0]["bar"]["foo"]["id"] == data["listFoos"][0]["id"]