Skip to content

Commit e1bcdaa

Browse files
committed
Locale is now in DFE
1 parent 6f77b80 commit e1bcdaa

File tree

2 files changed

+43
-9
lines changed

2 files changed

+43
-9
lines changed

src/main/java/graphql/validation/locale/LocaleUtil.java

+10-7
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,20 @@ public class LocaleUtil {
2323
*/
2424
public static Locale determineLocale(DataFetchingEnvironment environment, Locale defaultLocale) {
2525
//
26-
// in a future version of graphql java the DFE will have the Locale but in the mean time
27-
Locale locale;
28-
locale = extractLocale(environment);
26+
// The DFE has a locale now, but we retain the old look-ups for backwards compat reasons
27+
//
28+
Locale locale = environment.getLocale();
2929
if (locale == null) {
30-
locale = extractLocale(environment.getContext());
30+
locale = extractLocale(environment);
3131
if (locale == null) {
32-
locale = extractLocale(environment.getSource());
32+
locale = extractLocale(environment.getContext());
3333
if (locale == null) {
34-
locale = extractLocale(environment.getRoot());
34+
locale = extractLocale(environment.getSource());
3535
if (locale == null) {
36-
locale = defaultLocale;
36+
locale = extractLocale(environment.getRoot());
37+
if (locale == null) {
38+
locale = defaultLocale;
39+
}
3740
}
3841
}
3942
}

src/test/groovy/graphql/validation/locale/LocaleUtilTest.groovy

+33-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package graphql.validation.locale
22

3+
import graphql.GraphQLContext
34
import graphql.GraphQLError
4-
import graphql.GraphqlErrorBuilder
55
import graphql.execution.ExecutionStepInfo
66
import graphql.execution.MergedField
77
import graphql.schema.DataFetchingEnvironment
@@ -18,6 +18,8 @@ import graphql.validation.rules.ValidationEnvironment
1818
import graphql.validation.rules.ValidationRule
1919
import spock.lang.Specification
2020

21+
import static graphql.GraphqlErrorBuilder.newError
22+
2123
class 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

Comments
 (0)