|
1 | | -from typing import Any, NamedTuple, Union |
| 1 | +from typing import Any, Dict, NamedTuple, Union |
| 2 | + |
| 3 | +from pytest import fixture |
2 | 4 |
|
3 | 5 | from graphql import graphql_sync as graphql |
4 | 6 | from graphql.type import ( |
|
13 | 15 | from graphql_relay import from_global_id, global_id_field, node_definitions |
14 | 16 |
|
15 | 17 |
|
16 | | -class User(NamedTuple): |
17 | | - id: int |
18 | | - name: str |
19 | | - |
20 | | - |
21 | | -class Photo(NamedTuple): |
22 | | - photo_id: int |
23 | | - width: int |
24 | | - |
25 | | - |
26 | | -class Post(NamedTuple): |
27 | | - id: int |
28 | | - text: str |
29 | | - |
30 | | - |
31 | | -user_data = { |
32 | | - '1': User(id=1, name='John Doe'), |
33 | | - '2': User(id=2, name='Jane Smith'), |
34 | | -} |
35 | | - |
36 | | -photo_data = { |
37 | | - '1': Photo(photo_id=1, width=300), |
38 | | - '2': Photo(photo_id=2, width=400), |
39 | | -} |
40 | | - |
41 | | -post_data = { |
42 | | - '1': Post(id=1, text='lorem'), |
43 | | - '2': Post(id=2, text='ipsum'), |
44 | | -} |
45 | | - |
46 | | - |
47 | | -def get_node(global_id: str, info: GraphQLResolveInfo) -> Union[User, Photo, Post]: |
48 | | - assert info.schema is schema |
49 | | - type_, id_ = from_global_id(global_id) |
50 | | - if type_ == 'User': |
51 | | - return user_data[id_] |
52 | | - if type_ == 'Photo': |
53 | | - return photo_data[id_] |
54 | | - if type_ == 'Post': |
55 | | - return post_data[id_] |
56 | | - |
57 | | - |
58 | | -def get_node_type( |
59 | | - obj: Union[User, Photo], info: GraphQLResolveInfo, |
60 | | - _type: Any) -> GraphQLObjectType: |
61 | | - assert info.schema is schema |
62 | | - if isinstance(obj, User): |
63 | | - return user_type |
64 | | - if isinstance(obj, Photo): |
65 | | - return photo_type |
66 | | - if isinstance(obj, Post): |
67 | | - return post_type |
68 | | - |
69 | | - |
70 | | -node_interface, node_field = node_definitions(get_node, get_node_type)[:2] |
71 | | - |
72 | | -user_type = GraphQLObjectType( |
73 | | - 'User', |
74 | | - fields=lambda: { |
75 | | - 'id': global_id_field('User'), |
76 | | - 'name': GraphQLField(GraphQLString), |
77 | | - }, |
78 | | - interfaces=[node_interface] |
79 | | -) |
80 | | - |
81 | | -photo_type = GraphQLObjectType( |
82 | | - 'Photo', |
83 | | - fields=lambda: { |
84 | | - 'id': global_id_field('Photo', lambda obj, _info: obj.photo_id), |
85 | | - 'width': GraphQLField(GraphQLInt), |
86 | | - }, |
87 | | - interfaces=[node_interface] |
88 | | -) |
89 | | - |
90 | | -post_type = GraphQLObjectType( |
91 | | - 'Post', |
92 | | - fields=lambda: { |
93 | | - 'id': global_id_field(), |
94 | | - 'text': GraphQLField(GraphQLString), |
95 | | - }, |
96 | | - interfaces=[node_interface] |
97 | | -) |
98 | | - |
99 | | -query_type = GraphQLObjectType( |
100 | | - 'Query', |
101 | | - fields=lambda: { |
102 | | - 'node': node_field, |
103 | | - 'allObjects': GraphQLField( |
104 | | - GraphQLList(node_interface), |
105 | | - resolve=lambda _root, _info: [ |
106 | | - user_data['1'], user_data['2'], |
107 | | - photo_data['1'], photo_data['2'], |
108 | | - post_data['1'], post_data['2'] |
109 | | - ] |
110 | | - ) |
| 18 | +@fixture(scope='module', params=['object_access', 'dict_access']) |
| 19 | +def schema(request): |
| 20 | + """Run each test with object access and dict access.""" |
| 21 | + use_dicts = request.param == 'dict_access' |
| 22 | + |
| 23 | + if use_dicts: |
| 24 | + |
| 25 | + class User(dict): |
| 26 | + pass |
| 27 | + |
| 28 | + class Photo(dict): |
| 29 | + pass |
| 30 | + |
| 31 | + class Post(dict): |
| 32 | + pass |
| 33 | + |
| 34 | + else: |
| 35 | + |
| 36 | + class User(NamedTuple): |
| 37 | + id: int |
| 38 | + name: str |
| 39 | + |
| 40 | + class Photo(NamedTuple): |
| 41 | + photo_id: int |
| 42 | + width: int |
| 43 | + |
| 44 | + class Post(NamedTuple): |
| 45 | + id: int |
| 46 | + text: str |
| 47 | + |
| 48 | + user_data = { |
| 49 | + '1': User(id=1, name='John Doe'), |
| 50 | + '2': User(id=2, name='Jane Smith'), |
| 51 | + } |
| 52 | + |
| 53 | + photo_data = { |
| 54 | + '1': Photo(photo_id=1, width=300), |
| 55 | + '2': Photo(photo_id=2, width=400), |
| 56 | + } |
| 57 | + |
| 58 | + post_data = { |
| 59 | + '1': Post(id=1, text='lorem'), |
| 60 | + '2': Post(id=2, text='ipsum'), |
111 | 61 | } |
112 | | -) |
113 | 62 |
|
114 | | -schema = GraphQLSchema( |
115 | | - query=query_type, |
116 | | - types=[user_type, photo_type, post_type] |
117 | | -) |
| 63 | + def get_node( |
| 64 | + global_id: str, info: GraphQLResolveInfo |
| 65 | + ) -> Union[User, Photo, Post, Dict]: |
| 66 | + assert info.schema is schema |
| 67 | + type_, id_ = from_global_id(global_id) |
| 68 | + if type_ == 'User': |
| 69 | + return user_data[id_] |
| 70 | + if type_ == 'Photo': |
| 71 | + return photo_data[id_] |
| 72 | + if type_ == 'Post': |
| 73 | + return post_data[id_] |
| 74 | + |
| 75 | + def get_node_type( |
| 76 | + obj: Union[User, Photo], info: GraphQLResolveInfo, |
| 77 | + _type: Any) -> GraphQLObjectType: |
| 78 | + assert info.schema is schema |
| 79 | + if 'name' in obj if use_dicts else isinstance(obj, User): |
| 80 | + return user_type |
| 81 | + if 'photo_id' in obj if use_dicts else isinstance(obj, Photo): |
| 82 | + return photo_type |
| 83 | + if 'text' in obj if use_dicts else isinstance(obj, Post): |
| 84 | + return post_type |
| 85 | + |
| 86 | + node_interface, node_field = node_definitions(get_node, get_node_type)[:2] |
| 87 | + |
| 88 | + user_type = GraphQLObjectType( |
| 89 | + 'User', |
| 90 | + fields=lambda: { |
| 91 | + 'id': global_id_field('User'), |
| 92 | + 'name': GraphQLField(GraphQLString), |
| 93 | + }, |
| 94 | + interfaces=[node_interface] |
| 95 | + ) |
| 96 | + |
| 97 | + photo_type = GraphQLObjectType( |
| 98 | + 'Photo', |
| 99 | + fields=lambda: { |
| 100 | + 'id': global_id_field( |
| 101 | + 'Photo', |
| 102 | + lambda obj, _info: obj['photo_id'] if use_dicts else obj.photo_id), |
| 103 | + 'width': GraphQLField(GraphQLInt), |
| 104 | + }, |
| 105 | + interfaces=[node_interface] |
| 106 | + ) |
| 107 | + |
| 108 | + post_type = GraphQLObjectType( |
| 109 | + 'Post', |
| 110 | + fields=lambda: { |
| 111 | + 'id': global_id_field(), |
| 112 | + 'text': GraphQLField(GraphQLString), |
| 113 | + }, |
| 114 | + interfaces=[node_interface] |
| 115 | + ) |
| 116 | + |
| 117 | + query_type = GraphQLObjectType( |
| 118 | + 'Query', |
| 119 | + fields=lambda: { |
| 120 | + 'node': node_field, |
| 121 | + 'allObjects': GraphQLField( |
| 122 | + GraphQLList(node_interface), |
| 123 | + resolve=lambda _root, _info: [ |
| 124 | + user_data['1'], user_data['2'], |
| 125 | + photo_data['1'], photo_data['2'], |
| 126 | + post_data['1'], post_data['2'] |
| 127 | + ] |
| 128 | + ) |
| 129 | + } |
| 130 | + ) |
| 131 | + |
| 132 | + schema = GraphQLSchema( |
| 133 | + query=query_type, |
| 134 | + types=[user_type, photo_type, post_type] |
| 135 | + ) |
| 136 | + |
| 137 | + yield schema |
118 | 138 |
|
119 | 139 |
|
120 | 140 | def describe_global_id_fields(): |
121 | 141 |
|
122 | | - def gives_different_ids(): |
| 142 | + def gives_different_ids(schema): |
123 | 143 | query = ''' |
124 | 144 | { |
125 | 145 | allObjects { |
@@ -153,7 +173,7 @@ def gives_different_ids(): |
153 | 173 | None |
154 | 174 | ) |
155 | 175 |
|
156 | | - def refetches_the_ids(): |
| 176 | + def refetches_the_ids(schema): |
157 | 177 | query = ''' |
158 | 178 | { |
159 | 179 | user: node(id: "VXNlcjox") { |
|
0 commit comments