1
1
package graphql.validation.locale
2
2
3
+ import graphql.GraphQLContext
3
4
import graphql.GraphQLError
4
- import graphql.GraphqlErrorBuilder
5
5
import graphql.execution.ExecutionStepInfo
6
6
import graphql.execution.MergedField
7
7
import graphql.schema.DataFetchingEnvironment
@@ -18,6 +18,8 @@ import graphql.validation.rules.ValidationEnvironment
18
18
import graphql.validation.rules.ValidationRule
19
19
import spock.lang.Specification
20
20
21
+ import static graphql.GraphqlErrorBuilder.newError
22
+
21
23
class LocaleUtilTest extends Specification {
22
24
23
25
def directiveRules = DirectiveConstraints . newDirectiveConstraints(). build()
@@ -99,7 +101,10 @@ class LocaleUtilTest extends Specification {
99
101
100
102
@Override
101
103
List<GraphQLError > runValidation (ValidationEnvironment validationEnvironment ) {
102
- return [GraphqlErrorBuilder . newError(). message(" Locale=" + validationEnvironment. getLocale(). getCountry()). build()]
104
+ return [
105
+ newError(). message(" Locale=" + validationEnvironment. getLocale(). getCountry()). build(),
106
+ newError(). message(" Context=" + (validationEnvironment. getGraphQLContext() != null )). build()
107
+ ]
103
108
}
104
109
}
105
110
@@ -176,5 +181,31 @@ class LocaleUtilTest extends Specification {
176
181
errors = targetedValidationRules. runValidationRules(dfe, new ResourceBundleMessageInterpolator (), Locale . CHINA )
177
182
then :
178
183
errors[0 ]. message == " Locale=GB"
184
+
185
+ // use DFE direct
186
+ when :
187
+
188
+ dfe = DataFetchingEnvironmentImpl . newDataFetchingEnvironment(dfe)
189
+ .locale(Locale . UK )
190
+ .build()
191
+
192
+ errors = targetedValidationRules. runValidationRules(dfe, new ResourceBundleMessageInterpolator (), Locale . CHINA )
193
+ then :
194
+ errors[0 ]. message == " Locale=GB"
195
+ errors[1 ]. message == " Context=false"
196
+
197
+ // sneaking in a test that graphql context gets picked up here
198
+ // cheeky I know but the setup of a clean test in the exact right place is not worth it
199
+ when :
200
+
201
+ dfe = DataFetchingEnvironmentImpl . newDataFetchingEnvironment(dfe)
202
+ .locale(Locale . UK )
203
+ .graphQLContext(GraphQLContext . of([x : " present" ]))
204
+ .build()
205
+
206
+ errors = targetedValidationRules. runValidationRules(dfe, new ResourceBundleMessageInterpolator (), Locale . CHINA )
207
+ then :
208
+ errors[0 ]. message == " Locale=GB"
209
+ errors[1 ]. message == " Context=true"
179
210
}
180
211
}
0 commit comments