Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphqlContext added as well as the locale fix promised for so long #90

Merged
merged 3 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/main/java/graphql/validation/locale/LocaleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public class LocaleUtil {
*/
public static Locale determineLocale(DataFetchingEnvironment environment, Locale defaultLocale) {
//
// in a future version of graphql java the DFE will have the Locale but in the mean time
Locale locale;
locale = extractLocale(environment);
// The DFE has a locale now, but we retain the old look-ups for backwards compat reasons
//
Locale locale = environment.getLocale();
if (locale == null) {
locale = extractLocale(environment.getContext());
if (locale == null) {
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/graphql/validation/rules/ValidationEnvironment.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package graphql.validation.rules;

import graphql.GraphQLContext;
import graphql.PublicApi;
import graphql.execution.ResultPath;
import graphql.language.SourceLocation;
Expand Down Expand Up @@ -55,6 +56,8 @@ public enum ValidatedElement {
private final Object validatedValue;
private final GraphQLInputType validatedType;
private final ValidatedElement validatedElement;

private final GraphQLContext graphQLContext;
private final List<GraphQLAppliedDirective> directives;

private ValidationEnvironment(Builder builder) {
Expand All @@ -71,6 +74,7 @@ private ValidationEnvironment(Builder builder) {
this.location = builder.location;
this.validatedValue = builder.validatedValue;
this.validatedElement = builder.validatedElement;
this.graphQLContext = builder.graphQLContext;
this.directives = builder.directives;
}

Expand Down Expand Up @@ -135,6 +139,10 @@ public List<GraphQLAppliedDirective> getDirectives() {
return directives;
}

public GraphQLContext getGraphQLContext() {
return graphQLContext;
}

public ValidationEnvironment transform(Consumer<Builder> builderConsumer) {
Builder builder = newValidationEnvironment().validationEnvironment(this);
builderConsumer.accept(builder);
Expand All @@ -156,6 +164,7 @@ public static class Builder {
private GraphQLInputType validatedType;
private ValidatedElement validatedElement;
private List<GraphQLAppliedDirective> directives = Collections.emptyList();
private GraphQLContext graphQLContext = GraphQLContext.getDefault();

public Builder validationEnvironment(ValidationEnvironment validationEnvironment) {
this.argument = validationEnvironment.argument;
Expand All @@ -172,6 +181,7 @@ public Builder validationEnvironment(ValidationEnvironment validationEnvironment
this.validatedValue = validationEnvironment.validatedValue;
this.validatedElement = validationEnvironment.validatedElement;
this.directives = validationEnvironment.directives;
this.graphQLContext = validationEnvironment.graphQLContext;
return this;
}

Expand All @@ -184,6 +194,7 @@ public Builder dataFetchingEnvironment(DataFetchingEnvironment dataFetchingEnvir
location(dataFetchingEnvironment.getField().getSourceLocation());
argumentValues(dataFetchingEnvironment.getArguments());
validatedElement(ValidatedElement.FIELD);
graphQLContext(dataFetchingEnvironment.getGraphQlContext());
return this;
}

Expand Down Expand Up @@ -252,6 +263,11 @@ public Builder locale(Locale locale) {
return this;
}

public Builder graphQLContext(GraphQLContext graphQLContext) {
this.graphQLContext = graphQLContext;
return this;
}

public Builder directives(List<GraphQLAppliedDirective> directives) {
this.directives = directives;
return this;
Expand Down
35 changes: 33 additions & 2 deletions src/test/groovy/graphql/validation/locale/LocaleUtilTest.groovy
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package graphql.validation.locale

import graphql.GraphQLContext
import graphql.GraphQLError
import graphql.GraphqlErrorBuilder
import graphql.execution.ExecutionStepInfo
import graphql.execution.MergedField
import graphql.schema.DataFetchingEnvironment
Expand All @@ -18,6 +18,8 @@ import graphql.validation.rules.ValidationEnvironment
import graphql.validation.rules.ValidationRule
import spock.lang.Specification

import static graphql.GraphqlErrorBuilder.newError

class LocaleUtilTest extends Specification {

def directiveRules = DirectiveConstraints.newDirectiveConstraints().build()
Expand Down Expand Up @@ -99,7 +101,10 @@ class LocaleUtilTest extends Specification {

@Override
List<GraphQLError> runValidation(ValidationEnvironment validationEnvironment) {
return [GraphqlErrorBuilder.newError().message("Locale=" + validationEnvironment.getLocale().getCountry()).build()]
return [
newError().message("Locale=" + validationEnvironment.getLocale().getCountry()).build(),
newError().message("Context=" + (validationEnvironment.getGraphQLContext() != null)).build()
]
}
}

Expand Down Expand Up @@ -176,5 +181,31 @@ class LocaleUtilTest extends Specification {
errors = targetedValidationRules.runValidationRules(dfe, new ResourceBundleMessageInterpolator(), Locale.CHINA)
then:
errors[0].message == "Locale=GB"

// use DFE direct
when:

dfe = DataFetchingEnvironmentImpl.newDataFetchingEnvironment(dfe)
.locale(Locale.UK)
.build()

errors = targetedValidationRules.runValidationRules(dfe, new ResourceBundleMessageInterpolator(), Locale.CHINA)
then:
errors[0].message == "Locale=GB"
errors[1].message == "Context=false"

// sneaking in a test that graphql context gets picked up here
// cheeky I know but the setup of a clean test in the exact right place is not worth it
when:

dfe = DataFetchingEnvironmentImpl.newDataFetchingEnvironment(dfe)
.locale(Locale.UK)
.graphQLContext(GraphQLContext.of([x: "present"]))
.build()

errors = targetedValidationRules.runValidationRules(dfe, new ResourceBundleMessageInterpolator(), Locale.CHINA)
then:
errors[0].message == "Locale=GB"
errors[1].message == "Context=true"
}
}