|
| 1 | +from graphql.core import graphql |
| 2 | +from graphql.core.type import GraphQLString, GraphQLBoolean, GraphQLSchema |
| 3 | +from graphql.core.type.definition import GraphQLInterfaceType, GraphQLField, GraphQLObjectType, GraphQLList, \ |
| 4 | + GraphQLUnionType |
| 5 | + |
| 6 | + |
| 7 | +class Dog(object): |
| 8 | + def __init__(self, name, woofs): |
| 9 | + self.name = name |
| 10 | + self.woofs = woofs |
| 11 | + |
| 12 | + |
| 13 | +class Cat(object): |
| 14 | + def __init__(self, name, meows): |
| 15 | + self.name = name |
| 16 | + self.meows = meows |
| 17 | + |
| 18 | + |
| 19 | +class Human(object): |
| 20 | + def __init__(self, name): |
| 21 | + self.name = name |
| 22 | + |
| 23 | + |
| 24 | +is_type_of = lambda type: lambda obj, info: isinstance(obj, type) |
| 25 | + |
| 26 | + |
| 27 | +def make_type_resolver(types): |
| 28 | + def resolve_type(obj, info): |
| 29 | + if callable(types): |
| 30 | + t = types() |
| 31 | + else: |
| 32 | + t = types |
| 33 | + |
| 34 | + for klass, type in t: |
| 35 | + if isinstance(obj, klass): |
| 36 | + return type |
| 37 | + |
| 38 | + return None |
| 39 | + |
| 40 | + return resolve_type |
| 41 | + |
| 42 | + |
| 43 | +def test_is_type_of_used_to_resolve_runtime_type_for_interface(): |
| 44 | + PetType = GraphQLInterfaceType( |
| 45 | + name='Pet', |
| 46 | + fields={ |
| 47 | + 'name': GraphQLField(GraphQLString) |
| 48 | + } |
| 49 | + ) |
| 50 | + |
| 51 | + DogType = GraphQLObjectType( |
| 52 | + name='Dog', |
| 53 | + interfaces=[PetType], |
| 54 | + is_type_of=is_type_of(Dog), |
| 55 | + fields={ |
| 56 | + 'name': GraphQLField(GraphQLString), |
| 57 | + 'woofs': GraphQLField(GraphQLBoolean) |
| 58 | + } |
| 59 | + ) |
| 60 | + |
| 61 | + CatType = GraphQLObjectType( |
| 62 | + name='Cat', |
| 63 | + interfaces=[PetType], |
| 64 | + is_type_of=is_type_of(Cat), |
| 65 | + fields={ |
| 66 | + 'name': GraphQLField(GraphQLString), |
| 67 | + 'meows': GraphQLField(GraphQLBoolean) |
| 68 | + } |
| 69 | + ) |
| 70 | + |
| 71 | + schema = GraphQLSchema( |
| 72 | + query=GraphQLObjectType( |
| 73 | + name='Query', |
| 74 | + fields={ |
| 75 | + 'pets': GraphQLField( |
| 76 | + GraphQLList(PetType), |
| 77 | + resolver=lambda *_: [Dog('Odie', True), Cat('Garfield', False)] |
| 78 | + ) |
| 79 | + } |
| 80 | + ) |
| 81 | + ) |
| 82 | + |
| 83 | + query = ''' |
| 84 | + { |
| 85 | + pets { |
| 86 | + name |
| 87 | + ... on Dog { |
| 88 | + woofs |
| 89 | + } |
| 90 | + ... on Cat { |
| 91 | + meows |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + ''' |
| 96 | + |
| 97 | + result = graphql(schema, query) |
| 98 | + assert not result.errors |
| 99 | + assert result.data == {'pets': [{'woofs': True, 'name': 'Odie'}, {'name': 'Garfield', 'meows': False}]} |
| 100 | + |
| 101 | + |
| 102 | +def test_is_type_of_used_to_resolve_runtime_type_for_union(): |
| 103 | + DogType = GraphQLObjectType( |
| 104 | + name='Dog', |
| 105 | + is_type_of=is_type_of(Dog), |
| 106 | + fields={ |
| 107 | + 'name': GraphQLField(GraphQLString), |
| 108 | + 'woofs': GraphQLField(GraphQLBoolean) |
| 109 | + } |
| 110 | + ) |
| 111 | + |
| 112 | + CatType = GraphQLObjectType( |
| 113 | + name='Cat', |
| 114 | + is_type_of=is_type_of(Cat), |
| 115 | + fields={ |
| 116 | + 'name': GraphQLField(GraphQLString), |
| 117 | + 'meows': GraphQLField(GraphQLBoolean) |
| 118 | + } |
| 119 | + ) |
| 120 | + |
| 121 | + PetType = GraphQLUnionType( |
| 122 | + name='Pet', |
| 123 | + types=[CatType, DogType] |
| 124 | + ) |
| 125 | + |
| 126 | + schema = GraphQLSchema( |
| 127 | + query=GraphQLObjectType( |
| 128 | + name='Query', |
| 129 | + fields={ |
| 130 | + 'pets': GraphQLField( |
| 131 | + GraphQLList(PetType), |
| 132 | + resolver=lambda *_: [Dog('Odie', True), Cat('Garfield', False)] |
| 133 | + ) |
| 134 | + } |
| 135 | + ) |
| 136 | + ) |
| 137 | + |
| 138 | + query = ''' |
| 139 | + { |
| 140 | + pets { |
| 141 | + ... on Dog { |
| 142 | + name |
| 143 | + woofs |
| 144 | + } |
| 145 | + ... on Cat { |
| 146 | + name |
| 147 | + meows |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + ''' |
| 152 | + |
| 153 | + result = graphql(schema, query) |
| 154 | + assert not result.errors |
| 155 | + assert result.data == {'pets': [{'woofs': True, 'name': 'Odie'}, {'name': 'Garfield', 'meows': False}]} |
| 156 | + |
| 157 | + |
| 158 | +def test_resolve_type_on_interface_yields_useful_error(): |
| 159 | + PetType = GraphQLInterfaceType( |
| 160 | + name='Pet', |
| 161 | + fields={ |
| 162 | + 'name': GraphQLField(GraphQLString) |
| 163 | + }, |
| 164 | + resolve_type=make_type_resolver(lambda: [ |
| 165 | + (Dog, DogType), |
| 166 | + (Cat, CatType), |
| 167 | + (Human, HumanType) |
| 168 | + ]) |
| 169 | + ) |
| 170 | + |
| 171 | + DogType = GraphQLObjectType( |
| 172 | + name='Dog', |
| 173 | + interfaces=[PetType], |
| 174 | + fields={ |
| 175 | + 'name': GraphQLField(GraphQLString), |
| 176 | + 'woofs': GraphQLField(GraphQLBoolean) |
| 177 | + } |
| 178 | + ) |
| 179 | + |
| 180 | + HumanType = GraphQLObjectType( |
| 181 | + name='Human', |
| 182 | + fields={ |
| 183 | + 'name': GraphQLField(GraphQLString), |
| 184 | + } |
| 185 | + ) |
| 186 | + |
| 187 | + CatType = GraphQLObjectType( |
| 188 | + name='Cat', |
| 189 | + interfaces=[PetType], |
| 190 | + fields={ |
| 191 | + 'name': GraphQLField(GraphQLString), |
| 192 | + 'meows': GraphQLField(GraphQLBoolean) |
| 193 | + } |
| 194 | + ) |
| 195 | + |
| 196 | + schema = GraphQLSchema( |
| 197 | + query=GraphQLObjectType( |
| 198 | + name='Query', |
| 199 | + fields={ |
| 200 | + 'pets': GraphQLField( |
| 201 | + GraphQLList(PetType), |
| 202 | + resolver=lambda *_: [Dog('Odie', True), Cat('Garfield', False), Human('Jon')] |
| 203 | + ) |
| 204 | + } |
| 205 | + ) |
| 206 | + ) |
| 207 | + |
| 208 | + query = ''' |
| 209 | + { |
| 210 | + pets { |
| 211 | + name |
| 212 | + ... on Dog { |
| 213 | + woofs |
| 214 | + } |
| 215 | + ... on Cat { |
| 216 | + meows |
| 217 | + } |
| 218 | + } |
| 219 | + } |
| 220 | + ''' |
| 221 | + |
| 222 | + result = graphql(schema, query) |
| 223 | + assert result.errors[0].message == 'Runtime Object type "Human" is not a possible type for "Pet".' |
| 224 | + assert result.data == {'pets': [{'woofs': True, 'name': 'Odie'}, {'name': 'Garfield', 'meows': False}, None]} |
| 225 | + |
| 226 | + |
| 227 | +def test_resolve_type_on_union_yields_useful_error(): |
| 228 | + DogType = GraphQLObjectType( |
| 229 | + name='Dog', |
| 230 | + fields={ |
| 231 | + 'name': GraphQLField(GraphQLString), |
| 232 | + 'woofs': GraphQLField(GraphQLBoolean) |
| 233 | + } |
| 234 | + ) |
| 235 | + |
| 236 | + HumanType = GraphQLObjectType( |
| 237 | + name='Human', |
| 238 | + fields={ |
| 239 | + 'name': GraphQLField(GraphQLString), |
| 240 | + } |
| 241 | + ) |
| 242 | + |
| 243 | + CatType = GraphQLObjectType( |
| 244 | + name='Cat', |
| 245 | + fields={ |
| 246 | + 'name': GraphQLField(GraphQLString), |
| 247 | + 'meows': GraphQLField(GraphQLBoolean) |
| 248 | + } |
| 249 | + ) |
| 250 | + |
| 251 | + PetType = GraphQLUnionType( |
| 252 | + name='Pet', |
| 253 | + types=[DogType, CatType], |
| 254 | + resolve_type=make_type_resolver(lambda: [ |
| 255 | + (Dog, DogType), |
| 256 | + (Cat, CatType), |
| 257 | + (Human, HumanType) |
| 258 | + ]) |
| 259 | + ) |
| 260 | + |
| 261 | + schema = GraphQLSchema( |
| 262 | + query=GraphQLObjectType( |
| 263 | + name='Query', |
| 264 | + fields={ |
| 265 | + 'pets': GraphQLField( |
| 266 | + GraphQLList(PetType), |
| 267 | + resolver=lambda *_: [Dog('Odie', True), Cat('Garfield', False), Human('Jon')] |
| 268 | + ) |
| 269 | + } |
| 270 | + ) |
| 271 | + ) |
| 272 | + |
| 273 | + query = ''' |
| 274 | + { |
| 275 | + pets { |
| 276 | + ... on Dog { |
| 277 | + name |
| 278 | + woofs |
| 279 | + } |
| 280 | + ... on Cat { |
| 281 | + name |
| 282 | + meows |
| 283 | + } |
| 284 | + } |
| 285 | + } |
| 286 | + ''' |
| 287 | + |
| 288 | + result = graphql(schema, query) |
| 289 | + assert result.errors[0].message == 'Runtime Object type "Human" is not a possible type for "Pet".' |
| 290 | + assert result.data == {'pets': [{'woofs': True, 'name': 'Odie'}, {'name': 'Garfield', 'meows': False}, None]} |
0 commit comments