|
3 | 3 |
|
4 | 4 | from ....language import ast
|
5 | 5 | from ....type import (GraphQLEnumType, GraphQLInterfaceType, GraphQLList,
|
6 |
| - GraphQLNonNull, GraphQLObjectType, GraphQLScalarType, |
| 6 | + GraphQLNonNull, GraphQLObjectType, GraphQLScalarType, GraphQLBoolean, |
7 | 7 | GraphQLSchema, GraphQLUnionType, GraphQLString, GraphQLInt, GraphQLField)
|
8 | 8 | from ..resolver import type_resolver
|
9 | 9 | from ..fragment import Fragment
|
@@ -45,3 +45,72 @@ def test_fragment_resolver_abstract(benchmark):
|
45 | 45 | 'name': 'name:'+str(x)
|
46 | 46 | } for x in all_slots]
|
47 | 47 | }
|
| 48 | + |
| 49 | + |
| 50 | +def test_fragment_resolver_context(): |
| 51 | + Query = GraphQLObjectType('Query', fields={ |
| 52 | + 'context': GraphQLField(GraphQLString, resolver=lambda root, args, context, info: context), |
| 53 | + 'same_schema': GraphQLField(GraphQLBoolean, resolver=lambda root, args, context, info: info.schema == schema) |
| 54 | + }) |
| 55 | + |
| 56 | + document_ast = parse('''query { |
| 57 | + context |
| 58 | + same_schema |
| 59 | + }''') |
| 60 | + # node_fragment = Fragment(type=Node, field_asts=node_field_asts) |
| 61 | + schema = GraphQLSchema(query=Query) |
| 62 | + # partial_execute = partial(execute, schema, document_ast, context_value="1") |
| 63 | + # resolved = benchmark(partial_execute) |
| 64 | + resolved = execute(schema, document_ast, context_value="1") |
| 65 | + assert not resolved.errors |
| 66 | + assert resolved.data == { |
| 67 | + 'context': '1', |
| 68 | + 'same_schema': True, |
| 69 | + } |
| 70 | + |
| 71 | + |
| 72 | +def test_fragment_resolver_fails(): |
| 73 | + def raise_resolver(*args, **kwargs): |
| 74 | + raise Exception("My exception") |
| 75 | + |
| 76 | + def succeeds_resolver(*args, **kwargs): |
| 77 | + return True |
| 78 | + |
| 79 | + Query = GraphQLObjectType('Query', fields={ |
| 80 | + 'fails': GraphQLField(GraphQLString, resolver=raise_resolver), |
| 81 | + 'succeeds': GraphQLField(GraphQLBoolean, resolver=succeeds_resolver) |
| 82 | + }) |
| 83 | + |
| 84 | + document_ast = parse('''query { |
| 85 | + fails |
| 86 | + succeeds |
| 87 | + }''') |
| 88 | + # node_fragment = Fragment(type=Node, field_asts=node_field_asts) |
| 89 | + schema = GraphQLSchema(query=Query) |
| 90 | + # partial_execute = partial(execute, schema, document_ast, context_value="1") |
| 91 | + # resolved = benchmark(partial_execute) |
| 92 | + resolved = execute(schema, document_ast, context_value="1") |
| 93 | + assert len(resolved.errors) == 1 |
| 94 | + assert resolved.data == { |
| 95 | + 'fails': None, |
| 96 | + 'succeeds': True, |
| 97 | + } |
| 98 | + |
| 99 | + |
| 100 | +def test_fragment_resolver_resolves_all_list(): |
| 101 | + Query = GraphQLObjectType('Query', fields={ |
| 102 | + 'ints': GraphQLField(GraphQLList(GraphQLNonNull(GraphQLInt)), resolver=lambda *args: [1, "2", "NaN"]), |
| 103 | + }) |
| 104 | + |
| 105 | + document_ast = parse('''query { |
| 106 | + ints |
| 107 | + }''') |
| 108 | + # node_fragment = Fragment(type=Node, field_asts=node_field_asts) |
| 109 | + schema = GraphQLSchema(query=Query) |
| 110 | + # partial_execute = partial(execute, schema, document_ast, context_value="1") |
| 111 | + # resolved = benchmark(partial_execute) |
| 112 | + resolved = execute(schema, document_ast, context_value="1") |
| 113 | + assert len(resolved.errors) == 1 |
| 114 | + assert resolved.data == { |
| 115 | + 'ints': [1, 2, None] |
| 116 | + } |
0 commit comments