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