|
1 | 1 | from functools import partial |
| 2 | +from typing import Callable, List, Tuple |
2 | 3 |
|
3 | 4 | from graphql.utilities import build_schema |
4 | 5 | from graphql.validation import NoDeprecatedCustomRule |
5 | 6 |
|
6 | 7 | from .harness import assert_validation_errors |
7 | 8 |
|
8 | | -schema = build_schema( |
9 | | - """ |
10 | | - enum EnumType { |
11 | | - NORMAL_VALUE |
12 | | - DEPRECATED_VALUE @deprecated(reason: "Some enum reason.") |
13 | | - DEPRECATED_VALUE_WITH_NO_REASON @deprecated |
14 | | - } |
15 | 9 |
|
16 | | - type Query { |
17 | | - normalField(enumArg: [EnumType]): String |
18 | | - deprecatedField: String @deprecated(reason: "Some field reason.") |
19 | | - deprecatedFieldWithNoReason: String @deprecated |
20 | | - } |
21 | | - """ |
22 | | -) |
23 | | - |
24 | | -assert_errors = partial(assert_validation_errors, NoDeprecatedCustomRule, schema=schema) |
25 | | - |
26 | | -assert_valid = partial(assert_errors, errors=[]) |
| 10 | +def build_assertions( |
| 11 | + sdl_str: str, |
| 12 | +) -> Tuple[Callable[[str], None], Callable[[str, List], None]]: |
| 13 | + schema = build_schema(sdl_str) |
| 14 | + assert_errors = partial( |
| 15 | + assert_validation_errors, NoDeprecatedCustomRule, schema=schema |
| 16 | + ) |
| 17 | + assert_valid = partial(assert_errors, errors=[]) |
| 18 | + return ( |
| 19 | + assert_valid, |
| 20 | + assert_errors, |
| 21 | + ) # type: ignore |
27 | 22 |
|
28 | 23 |
|
29 | 24 | def describe_validate_no_deprecated(): |
30 | | - def ignores_fields_and_enum_values_that_are_not_deprecated(): |
31 | | - assert_valid( |
| 25 | + def describe_no_deprecated_fields(): |
| 26 | + _assert_valid, _assert_errors = build_assertions( |
32 | 27 | """ |
33 | | - { |
34 | | - normalField(enumArg: [NORMAL_VALUE]) |
| 28 | + type Query { |
| 29 | + normalField: String |
| 30 | + deprecatedField: String @deprecated(reason: "Some field reason.") |
35 | 31 | } |
36 | 32 | """ |
37 | 33 | ) |
38 | 34 |
|
39 | | - def ignores_unknown_fields_and_enum_values(): |
40 | | - assert_valid( |
| 35 | + def ignores_fields_and_enum_values_that_are_not_deprecated(): |
| 36 | + _assert_valid( |
| 37 | + """ |
| 38 | + { |
| 39 | + normalField |
| 40 | + } |
| 41 | + """ |
| 42 | + ) |
| 43 | + |
| 44 | + def ignores_unknown_fields(): |
| 45 | + _assert_valid( |
| 46 | + """ |
| 47 | + { |
| 48 | + unknownField |
| 49 | + } |
| 50 | +
|
| 51 | + fragment UnknownFragment on UnknownType { |
| 52 | + unknownField |
| 53 | + } |
| 54 | + """ |
| 55 | + ) |
| 56 | + |
| 57 | + def reports_error_when_a_deprecated_field_is_selected(): |
| 58 | + message = ( |
| 59 | + "The field Query.deprecatedField is deprecated. Some field reason." |
| 60 | + ) |
| 61 | + _assert_errors( |
| 62 | + """ |
| 63 | + { |
| 64 | + deprecatedField |
| 65 | + } |
| 66 | +
|
| 67 | + fragment QueryFragment on Query { |
| 68 | + deprecatedField |
| 69 | + } |
| 70 | + """, |
| 71 | + [ |
| 72 | + { |
| 73 | + "message": message, |
| 74 | + "locations": [(3, 19)], |
| 75 | + }, |
| 76 | + { |
| 77 | + "message": message, |
| 78 | + "locations": [(7, 19)], |
| 79 | + }, |
| 80 | + ], |
| 81 | + ) |
| 82 | + |
| 83 | + def describe_no_deprecated_enum_values(): |
| 84 | + _assert_valid, _assert_errors = build_assertions( |
41 | 85 | """ |
42 | | - fragment UnknownFragment on UnknownType { |
43 | | - unknownField(unknownArg: UNKNOWN_VALUE) |
| 86 | + enum EnumType { |
| 87 | + NORMAL_VALUE |
| 88 | + DEPRECATED_VALUE @deprecated(reason: "Some enum reason.") |
44 | 89 | } |
45 | 90 |
|
46 | | - fragment QueryFragment on Query { |
47 | | - unknownField(unknownArg: UNKNOWN_VALUE) |
48 | | - normalField(enumArg: UNKNOWN_VALUE) |
| 91 | + type Query { |
| 92 | + someField(enumArg: EnumType): String |
49 | 93 | } |
50 | 94 | """ |
51 | 95 | ) |
52 | 96 |
|
53 | | - def reports_error_when_a_deprecated_field_is_selected(): |
54 | | - assert_errors( |
55 | | - """ |
56 | | - { |
57 | | - normalField |
58 | | - deprecatedField |
59 | | - deprecatedFieldWithNoReason |
60 | | - } |
61 | | - """, |
62 | | - [ |
63 | | - { |
64 | | - "message": "The field Query.deprecatedField is deprecated." |
65 | | - " Some field reason.", |
66 | | - "locations": [(4, 15)], |
67 | | - }, |
| 97 | + def ignores_enum_values_that_are_not_deprecated(): |
| 98 | + _assert_valid( |
| 99 | + """ |
68 | 100 | { |
69 | | - "message": "The field Query.deprecatedFieldWithNoReason" |
70 | | - " is deprecated. No longer supported", |
71 | | - "locations": [(5, 15)], |
72 | | - }, |
73 | | - ], |
74 | | - ) |
| 101 | + normalField(enumArg: NORMAL_VALUE) |
| 102 | + } |
| 103 | + """ |
| 104 | + ) |
75 | 105 |
|
76 | | - def reports_error_when_a_deprecated_enum_value_is_used(): |
77 | | - assert_errors( |
78 | | - """ |
79 | | - { |
80 | | - normalField(enumArg: [NORMAL_VALUE, DEPRECATED_VALUE]) |
81 | | - normalField(enumArg: [DEPRECATED_VALUE_WITH_NO_REASON]) |
82 | | - } |
83 | | - """, |
84 | | - [ |
85 | | - { |
86 | | - "message": "The enum value 'EnumType.DEPRECATED_VALUE'" |
87 | | - " is deprecated. Some enum reason.", |
88 | | - "locations": [(3, 51)], |
89 | | - }, |
90 | | - { |
91 | | - "message": "The enum value" |
92 | | - " 'EnumType.DEPRECATED_VALUE_WITH_NO_REASON'" |
93 | | - " is deprecated. No longer supported", |
94 | | - "locations": [(4, 37)], |
95 | | - }, |
96 | | - ], |
97 | | - ) |
| 106 | + def ignores_unknown_enum_values(): |
| 107 | + _assert_valid( |
| 108 | + """ |
| 109 | + query ( |
| 110 | + $unknownValue: EnumType = UNKNOWN_VALUE |
| 111 | + $unknownType: UnknownType = UNKNOWN_VALUE |
| 112 | + ) { |
| 113 | + someField(enumArg: UNKNOWN_VALUE) |
| 114 | + someField(unknownArg: UNKNOWN_VALUE) |
| 115 | + unknownField(unknownArg: UNKNOWN_VALUE) |
| 116 | + } |
98 | 117 |
|
99 | | - def reports_error_when_deprecated_field_or_enum_value_is_used_inside_a_fragment(): |
100 | | - assert_errors( |
101 | | - """ |
102 | | - fragment QueryFragment on Query { |
103 | | - deprecatedField |
104 | | - normalField(enumArg: [NORMAL_VALUE, DEPRECATED_VALUE]) |
105 | | - } |
106 | | - """, |
107 | | - [ |
108 | | - { |
109 | | - "message": "The field Query.deprecatedField is deprecated." |
110 | | - " Some field reason.", |
111 | | - "locations": [(3, 15)], |
112 | | - }, |
113 | | - { |
114 | | - "message": "The enum value 'EnumType.DEPRECATED_VALUE'" |
115 | | - " is deprecated. Some enum reason.", |
116 | | - "locations": [(4, 51)], |
117 | | - }, |
118 | | - ], |
119 | | - ) |
| 118 | + fragment SomeFragment on Query { |
| 119 | + someField(enumArg: UNKNOWN_VALUE) |
| 120 | + } |
| 121 | + """ |
| 122 | + ) |
| 123 | + |
| 124 | + def reports_error_when_a_deprecated_enum_value_is_used(): |
| 125 | + message = ( |
| 126 | + "The enum value 'EnumType.DEPRECATED_VALUE' is deprecated." |
| 127 | + " Some enum reason." |
| 128 | + ) |
| 129 | + _assert_errors( |
| 130 | + """ |
| 131 | + query ( |
| 132 | + $variable: EnumType = DEPRECATED_VALUE |
| 133 | + ) { |
| 134 | + someField(enumArg: DEPRECATED_VALUE) |
| 135 | + } |
| 136 | +
|
| 137 | + fragment QueryFragment on Query { |
| 138 | + someField(enumArg: DEPRECATED_VALUE) |
| 139 | + } |
| 140 | + """, |
| 141 | + [ |
| 142 | + { |
| 143 | + "message": message, |
| 144 | + "locations": [(3, 41)], |
| 145 | + }, |
| 146 | + { |
| 147 | + "message": message, |
| 148 | + "locations": [(5, 38)], |
| 149 | + }, |
| 150 | + { |
| 151 | + "message": message, |
| 152 | + "locations": [(9, 38)], |
| 153 | + }, |
| 154 | + ], |
| 155 | + ) |
0 commit comments