|
1 | 1 | from graphql.type import GraphQLObjectType, GraphQLSchema
|
2 |
| -from pytest import raises |
| 2 | +from graphql import GraphQLError |
| 3 | +from pytest import mark, raises, fixture |
3 | 4 |
|
4 | 5 | from graphene.tests.utils import dedent
|
5 | 6 |
|
6 | 7 | from ..field import Field
|
7 | 8 | from ..objecttype import ObjectType
|
8 | 9 | from ..scalars import String
|
9 |
| -from ..schema import Schema |
| 10 | +from ..schema import Schema, UnforgivingExecutionContext |
10 | 11 |
|
11 | 12 |
|
12 | 13 | class MyOtherType(ObjectType):
|
@@ -68,3 +69,115 @@ def test_schema_requires_query_type():
|
68 | 69 | assert len(result.errors) == 1
|
69 | 70 | error = result.errors[0]
|
70 | 71 | assert error.message == "Query root type must be provided."
|
| 72 | + |
| 73 | + |
| 74 | +class TestUnforgivingExecutionContext: |
| 75 | + @fixture |
| 76 | + def schema(self): |
| 77 | + class ErrorFieldsMixin: |
| 78 | + sanity_field = String() |
| 79 | + expected_error_field = String() |
| 80 | + unexpected_value_error_field = String() |
| 81 | + unexpected_type_error_field = String() |
| 82 | + unexpected_attribute_error_field = String() |
| 83 | + unexpected_key_error_field = String() |
| 84 | + |
| 85 | + @staticmethod |
| 86 | + def resolve_sanity_field(obj, info): |
| 87 | + return "not an error" |
| 88 | + |
| 89 | + @staticmethod |
| 90 | + def resolve_expected_error_field(obj, info): |
| 91 | + raise GraphQLError("expected error") |
| 92 | + |
| 93 | + @staticmethod |
| 94 | + def resolve_unexpected_value_error_field(obj, info): |
| 95 | + raise ValueError("unexpected error") |
| 96 | + |
| 97 | + @staticmethod |
| 98 | + def resolve_unexpected_type_error_field(obj, info): |
| 99 | + raise TypeError("unexpected error") |
| 100 | + |
| 101 | + @staticmethod |
| 102 | + def resolve_unexpected_attribute_error_field(obj, info): |
| 103 | + raise AttributeError("unexpected error") |
| 104 | + |
| 105 | + @staticmethod |
| 106 | + def resolve_unexpected_key_error_field(obj, info): |
| 107 | + return {}["fails"] |
| 108 | + |
| 109 | + class NestedObject(ErrorFieldsMixin, ObjectType): |
| 110 | + pass |
| 111 | + |
| 112 | + class MyQuery(ErrorFieldsMixin, ObjectType): |
| 113 | + nested_object = Field(NestedObject) |
| 114 | + nested_object_error = Field(NestedObject) |
| 115 | + |
| 116 | + @staticmethod |
| 117 | + def resolve_nested_object(obj, info): |
| 118 | + return object() |
| 119 | + |
| 120 | + @staticmethod |
| 121 | + def resolve_nested_object_error(obj, info): |
| 122 | + raise TypeError() |
| 123 | + |
| 124 | + schema = Schema(query=MyQuery) |
| 125 | + return schema |
| 126 | + |
| 127 | + def test_sanity_check(self, schema): |
| 128 | + # this should pass with no errors (sanity check) |
| 129 | + result = schema.execute( |
| 130 | + "query { sanityField }", |
| 131 | + execution_context_class=UnforgivingExecutionContext, |
| 132 | + ) |
| 133 | + assert not result.errors |
| 134 | + assert result.data == {"sanityField": "not an error"} |
| 135 | + |
| 136 | + def test_nested_sanity_check(self, schema): |
| 137 | + # this should pass with no errors (sanity check) |
| 138 | + result = schema.execute( |
| 139 | + r"query { nestedObject { sanityField } }", |
| 140 | + execution_context_class=UnforgivingExecutionContext, |
| 141 | + ) |
| 142 | + assert not result.errors |
| 143 | + assert result.data == {"nestedObject": {"sanityField": "not an error"}} |
| 144 | + |
| 145 | + def test_graphql_error(self, schema): |
| 146 | + result = schema.execute( |
| 147 | + "query { expectedErrorField }", |
| 148 | + execution_context_class=UnforgivingExecutionContext, |
| 149 | + ) |
| 150 | + assert len(result.errors) == 1 |
| 151 | + assert result.errors[0].message == "expected error" |
| 152 | + assert result.data == {"expectedErrorField": None} |
| 153 | + |
| 154 | + def test_nested_graphql_error(self, schema): |
| 155 | + result = schema.execute( |
| 156 | + r"query { nestedObject { expectedErrorField } }", |
| 157 | + execution_context_class=UnforgivingExecutionContext, |
| 158 | + ) |
| 159 | + assert len(result.errors) == 1 |
| 160 | + assert result.errors[0].message == "expected error" |
| 161 | + assert result.data == {"nestedObject": {"expectedErrorField": None}} |
| 162 | + |
| 163 | + @mark.parametrize( |
| 164 | + "field,exception", |
| 165 | + [ |
| 166 | + ("unexpectedValueErrorField", ValueError), |
| 167 | + ("unexpectedTypeErrorField", TypeError), |
| 168 | + ("unexpectedAttributeErrorField", AttributeError), |
| 169 | + ("unexpectedKeyErrorField", KeyError), |
| 170 | + ("nestedObject { unexpectedValueErrorField }", ValueError), |
| 171 | + ("nestedObject { unexpectedTypeErrorField }", TypeError), |
| 172 | + ("nestedObject { unexpectedAttributeErrorField }", AttributeError), |
| 173 | + ("nestedObject { unexpectedKeyErrorField }", KeyError), |
| 174 | + ("nestedObjectError { __typename }", TypeError), |
| 175 | + ], |
| 176 | + ) |
| 177 | + def test_unexpected_error(self, field, exception, schema): |
| 178 | + with raises(exception): |
| 179 | + # no result, but the exception should be propagated |
| 180 | + schema.execute( |
| 181 | + f"query {{ {field} }}", |
| 182 | + execution_context_class=UnforgivingExecutionContext, |
| 183 | + ) |
0 commit comments