From 6f77b8082a7930eb2ea92b75254c6c36a3e57b44 Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Thu, 24 Aug 2023 16:25:57 +1000 Subject: [PATCH 1/3] Added graphql context to ValidationEnvironment --- .../validation/rules/ValidationEnvironment.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/graphql/validation/rules/ValidationEnvironment.java b/src/main/java/graphql/validation/rules/ValidationEnvironment.java index 903b5bb..ae3c4b1 100644 --- a/src/main/java/graphql/validation/rules/ValidationEnvironment.java +++ b/src/main/java/graphql/validation/rules/ValidationEnvironment.java @@ -1,5 +1,6 @@ package graphql.validation.rules; +import graphql.GraphQLContext; import graphql.PublicApi; import graphql.execution.ResultPath; import graphql.language.SourceLocation; @@ -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 directives; private ValidationEnvironment(Builder builder) { @@ -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; } @@ -135,6 +139,10 @@ public List getDirectives() { return directives; } + public GraphQLContext getGraphQLContext() { + return graphQLContext; + } + public ValidationEnvironment transform(Consumer builderConsumer) { Builder builder = newValidationEnvironment().validationEnvironment(this); builderConsumer.accept(builder); @@ -156,6 +164,7 @@ public static class Builder { private GraphQLInputType validatedType; private ValidatedElement validatedElement; private List directives = Collections.emptyList(); + private GraphQLContext graphQLContext = GraphQLContext.getDefault(); public Builder validationEnvironment(ValidationEnvironment validationEnvironment) { this.argument = validationEnvironment.argument; @@ -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; } @@ -184,6 +194,7 @@ public Builder dataFetchingEnvironment(DataFetchingEnvironment dataFetchingEnvir location(dataFetchingEnvironment.getField().getSourceLocation()); argumentValues(dataFetchingEnvironment.getArguments()); validatedElement(ValidatedElement.FIELD); + graphQLContext(dataFetchingEnvironment.getGraphQlContext()); return this; } @@ -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 directives) { this.directives = directives; return this; From e1bcdaa48da42e7e88a5250d1d687e127966ad8c Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Thu, 24 Aug 2023 17:27:24 +1000 Subject: [PATCH 2/3] Locale is now in DFE --- .../graphql/validation/locale/LocaleUtil.java | 17 +++++---- .../validation/locale/LocaleUtilTest.groovy | 35 +++++++++++++++++-- 2 files changed, 43 insertions(+), 9 deletions(-) diff --git a/src/main/java/graphql/validation/locale/LocaleUtil.java b/src/main/java/graphql/validation/locale/LocaleUtil.java index 13fffdb..718da5b 100644 --- a/src/main/java/graphql/validation/locale/LocaleUtil.java +++ b/src/main/java/graphql/validation/locale/LocaleUtil.java @@ -23,17 +23,20 @@ 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()); + locale = extractLocale(environment); if (locale == null) { - locale = extractLocale(environment.getSource()); + locale = extractLocale(environment.getContext()); if (locale == null) { - locale = extractLocale(environment.getRoot()); + locale = extractLocale(environment.getSource()); if (locale == null) { - locale = defaultLocale; + locale = extractLocale(environment.getRoot()); + if (locale == null) { + locale = defaultLocale; + } } } } diff --git a/src/test/groovy/graphql/validation/locale/LocaleUtilTest.groovy b/src/test/groovy/graphql/validation/locale/LocaleUtilTest.groovy index b01b5eb..cda044b 100644 --- a/src/test/groovy/graphql/validation/locale/LocaleUtilTest.groovy +++ b/src/test/groovy/graphql/validation/locale/LocaleUtilTest.groovy @@ -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 @@ -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() @@ -99,7 +101,10 @@ class LocaleUtilTest extends Specification { @Override List 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() + ] } } @@ -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" } } From d04517c6a0ef034b0698557eb1dd6213ad7def86 Mon Sep 17 00:00:00 2001 From: Brad Baker Date: Thu, 24 Aug 2023 17:46:05 +1000 Subject: [PATCH 3/3] Locale is now in DFE - no need for reflection --- .../java/graphql/validation/locale/LocaleUtil.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/src/main/java/graphql/validation/locale/LocaleUtil.java b/src/main/java/graphql/validation/locale/LocaleUtil.java index 718da5b..11eabcd 100644 --- a/src/main/java/graphql/validation/locale/LocaleUtil.java +++ b/src/main/java/graphql/validation/locale/LocaleUtil.java @@ -27,16 +27,13 @@ public static Locale determineLocale(DataFetchingEnvironment environment, Locale // Locale locale = environment.getLocale(); if (locale == null) { - locale = extractLocale(environment); + locale = extractLocale(environment.getContext()); if (locale == null) { - locale = extractLocale(environment.getContext()); + locale = extractLocale(environment.getSource()); if (locale == null) { - locale = extractLocale(environment.getSource()); + locale = extractLocale(environment.getRoot()); if (locale == null) { - locale = extractLocale(environment.getRoot()); - if (locale == null) { - locale = defaultLocale; - } + locale = defaultLocale; } } }