Skip to content

Commit 10a3f60

Browse files
authored
Merge pull request #90 from graphql-java/graphql-context-added
GraphqlContext added as well as the locale fix promised for so long
2 parents fde06da + d04517c commit 10a3f60

File tree

3 files changed

+52
-5
lines changed

3 files changed

+52
-5
lines changed

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ 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) {
3030
locale = extractLocale(environment.getContext());
3131
if (locale == null) {

src/main/java/graphql/validation/rules/ValidationEnvironment.java

+16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package graphql.validation.rules;
22

3+
import graphql.GraphQLContext;
34
import graphql.PublicApi;
45
import graphql.execution.ResultPath;
56
import graphql.language.SourceLocation;
@@ -55,6 +56,8 @@ public enum ValidatedElement {
5556
private final Object validatedValue;
5657
private final GraphQLInputType validatedType;
5758
private final ValidatedElement validatedElement;
59+
60+
private final GraphQLContext graphQLContext;
5861
private final List<GraphQLAppliedDirective> directives;
5962

6063
private ValidationEnvironment(Builder builder) {
@@ -71,6 +74,7 @@ private ValidationEnvironment(Builder builder) {
7174
this.location = builder.location;
7275
this.validatedValue = builder.validatedValue;
7376
this.validatedElement = builder.validatedElement;
77+
this.graphQLContext = builder.graphQLContext;
7478
this.directives = builder.directives;
7579
}
7680

@@ -135,6 +139,10 @@ public List<GraphQLAppliedDirective> getDirectives() {
135139
return directives;
136140
}
137141

142+
public GraphQLContext getGraphQLContext() {
143+
return graphQLContext;
144+
}
145+
138146
public ValidationEnvironment transform(Consumer<Builder> builderConsumer) {
139147
Builder builder = newValidationEnvironment().validationEnvironment(this);
140148
builderConsumer.accept(builder);
@@ -156,6 +164,7 @@ public static class Builder {
156164
private GraphQLInputType validatedType;
157165
private ValidatedElement validatedElement;
158166
private List<GraphQLAppliedDirective> directives = Collections.emptyList();
167+
private GraphQLContext graphQLContext = GraphQLContext.getDefault();
159168

160169
public Builder validationEnvironment(ValidationEnvironment validationEnvironment) {
161170
this.argument = validationEnvironment.argument;
@@ -172,6 +181,7 @@ public Builder validationEnvironment(ValidationEnvironment validationEnvironment
172181
this.validatedValue = validationEnvironment.validatedValue;
173182
this.validatedElement = validationEnvironment.validatedElement;
174183
this.directives = validationEnvironment.directives;
184+
this.graphQLContext = validationEnvironment.graphQLContext;
175185
return this;
176186
}
177187

@@ -184,6 +194,7 @@ public Builder dataFetchingEnvironment(DataFetchingEnvironment dataFetchingEnvir
184194
location(dataFetchingEnvironment.getField().getSourceLocation());
185195
argumentValues(dataFetchingEnvironment.getArguments());
186196
validatedElement(ValidatedElement.FIELD);
197+
graphQLContext(dataFetchingEnvironment.getGraphQlContext());
187198
return this;
188199
}
189200

@@ -252,6 +263,11 @@ public Builder locale(Locale locale) {
252263
return this;
253264
}
254265

266+
public Builder graphQLContext(GraphQLContext graphQLContext) {
267+
this.graphQLContext = graphQLContext;
268+
return this;
269+
}
270+
255271
public Builder directives(List<GraphQLAppliedDirective> directives) {
256272
this.directives = directives;
257273
return this;

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)