11package graphql.validation.locale
22
3+ import graphql.GraphQLContext
34import graphql.GraphQLError
4- import graphql.GraphqlErrorBuilder
55import graphql.execution.ExecutionStepInfo
66import graphql.execution.MergedField
77import graphql.schema.DataFetchingEnvironment
@@ -18,6 +18,8 @@ import graphql.validation.rules.ValidationEnvironment
1818import graphql.validation.rules.ValidationRule
1919import spock.lang.Specification
2020
21+ import static graphql.GraphqlErrorBuilder.newError
22+
2123class LocaleUtilTest extends Specification {
2224
2325 def directiveRules = DirectiveConstraints . newDirectiveConstraints(). build()
@@ -99,7 +101,10 @@ class LocaleUtilTest extends Specification {
99101
100102 @Override
101103 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+ ]
103108 }
104109 }
105110
@@ -176,5 +181,31 @@ class LocaleUtilTest extends Specification {
176181 errors = targetedValidationRules. runValidationRules(dfe, new ResourceBundleMessageInterpolator (), Locale . CHINA )
177182 then :
178183 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"
179210 }
180211}
0 commit comments