Thanks to visit codestin.com
Credit goes to github.com

Skip to content
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
21 changes: 19 additions & 2 deletions src/main/java/graphql/execution/Execution.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,15 @@
import graphql.schema.GraphQLSchema;
import graphql.schema.impl.SchemaUtil;
import org.jspecify.annotations.NonNull;
import graphql.util.FpKit;
import org.reactivestreams.Publisher;

import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;

import static graphql.Directives.EXPERIMENTAL_DISABLE_ERROR_PROPAGATION_DIRECTIVE_DEFINITION;
import static graphql.execution.ExecutionContextBuilder.newExecutionContextBuilder;
Expand Down Expand Up @@ -71,12 +73,13 @@ public Execution(ExecutionStrategy queryStrategy,
}

public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSchema graphQLSchema, ExecutionId executionId, ExecutionInput executionInput, InstrumentationState instrumentationState) {

NodeUtil.GetOperationResult getOperationResult;
CoercedVariables coercedVariables;
Supplier<NormalizedVariables> normalizedVariableValues;
try {
getOperationResult = NodeUtil.getOperation(document, executionInput.getOperationName());
coercedVariables = coerceVariableValues(graphQLSchema, executionInput, getOperationResult.operationDefinition);
normalizedVariableValues = normalizedVariableValues(graphQLSchema, executionInput, getOperationResult);
} catch (RuntimeException rte) {
if (rte instanceof GraphQLError) {
return completedFuture(new ExecutionResultImpl((GraphQLError) rte));
Expand All @@ -100,6 +103,7 @@ public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSche
.root(executionInput.getRoot())
.fragmentsByName(getOperationResult.fragmentsByName)
.coercedVariables(coercedVariables)
.normalizedVariableValues(normalizedVariableValues)
.document(document)
.operationDefinition(getOperationResult.operationDefinition)
.dataLoaderRegistry(executionInput.getDataLoaderRegistry())
Expand All @@ -124,6 +128,19 @@ public CompletableFuture<ExecutionResult> execute(Document document, GraphQLSche
return ValuesResolver.coerceVariableValues(graphQLSchema, variableDefinitions, inputVariables, executionInput.getGraphQLContext(), executionInput.getLocale());
}

private static @NonNull Supplier<NormalizedVariables> normalizedVariableValues(GraphQLSchema graphQLSchema, ExecutionInput executionInput, NodeUtil.GetOperationResult getOperationResult) {
Supplier<NormalizedVariables> normalizedVariableValues;
RawVariables inputVariables = executionInput.getRawVariables();
List<VariableDefinition> variableDefinitions = getOperationResult.operationDefinition.getVariableDefinitions();

normalizedVariableValues = FpKit.intraThreadMemoize(() ->
ValuesResolver.getNormalizedVariableValues(graphQLSchema,
variableDefinitions,
inputVariables,
executionInput.getGraphQLContext(), executionInput.getLocale()));
return normalizedVariableValues;
}


private CompletableFuture<ExecutionResult> executeOperation(ExecutionContext executionContext, Object root, OperationDefinition operationDefinition) {

Expand Down Expand Up @@ -235,7 +252,7 @@ private DataLoaderDispatchStrategy createDataLoaderDispatchStrategy(ExecutionCon
if (executionContext.getDataLoaderRegistry() == EMPTY_DATALOADER_REGISTRY || doNotAutomaticallyDispatchDataLoader) {
return DataLoaderDispatchStrategy.NO_OP;
}
if (! executionContext.isSubscriptionOperation()) {
if (!executionContext.isSubscriptionOperation()) {
boolean deferEnabled = Optional.ofNullable(executionContext.getGraphQLContext())
.map(graphqlContext -> graphqlContext.getBoolean(ExperimentalApi.ENABLE_INCREMENTAL_SUPPORT))
.orElse(false);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/graphql/execution/ExecutionContext.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class ExecutionContext {
private final OperationDefinition operationDefinition;
private final Document document;
private final CoercedVariables coercedVariables;
private final Supplier<NormalizedVariables> normalizedVariables;
private final Object root;
private final Object context;
private final GraphQLContext graphQLContext;
Expand Down Expand Up @@ -76,6 +77,7 @@ public class ExecutionContext {
this.subscriptionStrategy = builder.subscriptionStrategy;
this.fragmentsByName = builder.fragmentsByName;
this.coercedVariables = builder.coercedVariables;
this.normalizedVariables = builder.normalizedVariables;
this.document = builder.document;
this.operationDefinition = builder.operationDefinition;
this.context = builder.context;
Expand Down Expand Up @@ -130,6 +132,13 @@ public CoercedVariables getCoercedVariables() {
return coercedVariables;
}

/**
* @return a supplier that will give out the operations variables in normalized form
*/
public Supplier<NormalizedVariables> getNormalizedVariables() {
return normalizedVariables;
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just passing on NormalizedVariables

/**
* @param <T> for two
* @return the legacy context
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/graphql/execution/ExecutionContextBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;

import static graphql.Assert.assertNotNull;
import static graphql.collect.ImmutableKit.emptyList;
Expand All @@ -39,6 +40,7 @@ public class ExecutionContextBuilder {
Document document;
OperationDefinition operationDefinition;
CoercedVariables coercedVariables = CoercedVariables.emptyVariables();
Supplier<NormalizedVariables> normalizedVariables = NormalizedVariables::emptyVariables;
ImmutableMap<String, FragmentDefinition> fragmentsByName = ImmutableKit.emptyMap();
DataLoaderRegistry dataLoaderRegistry;
Locale locale;
Expand Down Expand Up @@ -173,6 +175,11 @@ public ExecutionContextBuilder coercedVariables(CoercedVariables coercedVariable
return this;
}

public ExecutionContextBuilder normalizedVariableValues(Supplier<NormalizedVariables> normalizedVariables) {
this.normalizedVariables = normalizedVariables;
return this;
}

public ExecutionContextBuilder fragmentsByName(Map<String, FragmentDefinition> fragmentsByName) {
this.fragmentsByName = ImmutableMap.copyOf(fragmentsByName);
return this;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/graphql/execution/ExecutionStrategy.java
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,8 @@ private Object fetchField(GraphQLFieldDefinition fieldDef, ExecutionContext exec
DataFetchingFieldSelectionSet fieldCollector = DataFetchingFieldSelectionSetImpl.newCollector(executionContext.getGraphQLSchema(), fieldDef.getType(), normalizedFieldSupplier);
QueryDirectives queryDirectives = new QueryDirectivesImpl(field,
executionContext.getGraphQLSchema(),
executionContext.getCoercedVariables().toMap(),
executionContext.getCoercedVariables(),
executionContext.getNormalizedVariables(),
executionContext.getGraphQLContext(),
executionContext.getLocale());

Expand Down
45 changes: 45 additions & 0 deletions src/main/java/graphql/execution/NormalizedVariables.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package graphql.execution;

import graphql.PublicApi;
import graphql.collect.ImmutableKit;
import graphql.collect.ImmutableMapWithNullValues;
import graphql.normalized.NormalizedInputValue;

import java.util.Map;

/**
* Holds coerced variables, that is their values are now in a normalized {@link graphql.normalized.NormalizedInputValue} form.
*/
@PublicApi
public class NormalizedVariables {
private final ImmutableMapWithNullValues<String, NormalizedInputValue> normalisedVariables;

public NormalizedVariables(Map<String, NormalizedInputValue> normalisedVariables) {
this.normalisedVariables = ImmutableMapWithNullValues.copyOf(normalisedVariables);
}

public Map<String, NormalizedInputValue> toMap() {
return normalisedVariables;
}

public boolean containsKey(String key) {
return normalisedVariables.containsKey(key);
}

public Object get(String key) {
return normalisedVariables.get(key);
}

public static NormalizedVariables emptyVariables() {
return new NormalizedVariables(ImmutableKit.emptyMap());
}

public static NormalizedVariables of(Map<String, NormalizedInputValue> normalisedVariables) {
return new NormalizedVariables(normalisedVariables);
}

@Override
public String toString() {
return normalisedVariables.toString();
}
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We have Raw, Coerced and NormalizedVariables now as distinct classes

6 changes: 2 additions & 4 deletions src/main/java/graphql/execution/ValuesResolver.java
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ public static CoercedVariables coerceVariableValues(GraphQLSchema schema,
*
* @return a map of the normalised values
*/
public static Map<String, NormalizedInputValue> getNormalizedVariableValues(
public static NormalizedVariables getNormalizedVariableValues(
GraphQLSchema schema,
List<VariableDefinition> variableDefinitions,
RawVariables rawVariables,
Expand Down Expand Up @@ -131,9 +131,7 @@ public static Map<String, NormalizedInputValue> getNormalizedVariableValues(
}
}
}

return result;

return NormalizedVariables.of(result);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

distinct class now used

}


Expand Down
20 changes: 10 additions & 10 deletions src/main/java/graphql/execution/directives/DirectivesResolver.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package graphql.execution.directives;

import com.google.common.collect.ImmutableMap;
import com.google.common.collect.BiMap;
import com.google.common.collect.HashBiMap;
import com.google.common.collect.ImmutableBiMap;
import graphql.GraphQLContext;
import graphql.Internal;
import graphql.execution.CoercedVariables;
Expand All @@ -11,8 +13,6 @@
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLSchema;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
Expand All @@ -26,27 +26,27 @@ public class DirectivesResolver {
public DirectivesResolver() {
}

public Map<String, List<GraphQLDirective>> resolveDirectives(List<Directive> directives, GraphQLSchema schema, Map<String, Object> variables, GraphQLContext graphQLContext, Locale locale) {
public BiMap<GraphQLDirective, Directive> resolveDirectives(List<Directive> directives, GraphQLSchema schema, CoercedVariables variables, GraphQLContext graphQLContext, Locale locale) {
GraphQLCodeRegistry codeRegistry = schema.getCodeRegistry();
Map<String, List<GraphQLDirective>> directiveMap = new LinkedHashMap<>();
BiMap<GraphQLDirective, Directive> directiveMap = HashBiMap.create();
directives.forEach(directive -> {
GraphQLDirective protoType = schema.getDirective(directive.getName());
if (protoType != null) {
GraphQLDirective newDirective = protoType.transform(builder -> buildArguments(builder, codeRegistry, protoType, directive, variables, graphQLContext, locale));
directiveMap.computeIfAbsent(newDirective.getName(), k -> new ArrayList<>()).add(newDirective);
GraphQLDirective graphQLDirective = protoType.transform(builder -> buildArguments(builder, codeRegistry, protoType, directive, variables, graphQLContext, locale));
directiveMap.put(graphQLDirective, directive);
}
});
return ImmutableMap.copyOf(directiveMap);
return ImmutableBiMap.copyOf(directiveMap);
}

private void buildArguments(GraphQLDirective.Builder directiveBuilder,
GraphQLCodeRegistry codeRegistry,
GraphQLDirective protoType,
Directive fieldDirective,
Map<String, Object> variables,
CoercedVariables variables,
GraphQLContext graphQLContext,
Locale locale) {
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(codeRegistry, protoType.getArguments(), fieldDirective.getArguments(), CoercedVariables.of(variables), graphQLContext, locale);
Map<String, Object> argumentValues = ValuesResolver.getArgumentValues(codeRegistry, protoType.getArguments(), fieldDirective.getArguments(), variables, graphQLContext, locale);
directiveBuilder.clearArguments();
protoType.getArguments().forEach(protoArg -> {
if (argumentValues.containsKey(protoArg.getName())) {
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/graphql/execution/directives/QueryDirectives.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import graphql.PublicApi;
import graphql.execution.CoercedVariables;
import graphql.execution.MergedField;
import graphql.execution.NormalizedVariables;
import graphql.language.Field;
import graphql.normalized.NormalizedInputValue;
import graphql.schema.GraphQLDirective;
import graphql.schema.GraphQLSchema;

import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.function.Supplier;

/**
* This gives you access to the immediate directives on a {@link graphql.execution.MergedField}. This does not include directives on parent
Expand Down Expand Up @@ -66,6 +69,16 @@ public interface QueryDirectives {
*/
Map<Field, List<QueryAppliedDirective>> getImmediateAppliedDirectivesByField();

/**
* This will return a map of {@link QueryAppliedDirective} to a map of their argument values in {@link NormalizedInputValue} form
* <p>
* NOTE : This will only be available when {@link graphql.normalized.ExecutableNormalizedOperationFactory} is used
* to create the {@link QueryAppliedDirective} information
*
* @return a map of applied directive to named argument values
*/
Map<QueryAppliedDirective, Map<String, NormalizedInputValue>> getNormalizedInputValueByImmediateAppliedDirectives();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for a given QueryAppliedDirective - we can get a normalised view of its argument values


/**
* This will return a list of the named directives that are immediately on this merged field.
*
Expand Down Expand Up @@ -108,6 +121,8 @@ interface Builder {

Builder coercedVariables(CoercedVariables coercedVariables);

Builder normalizedVariables(Supplier<NormalizedVariables> normalizedVariables);

Builder graphQLContext(GraphQLContext graphQLContext);

Builder locale(Locale locale);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@
import graphql.Internal;
import graphql.execution.CoercedVariables;
import graphql.execution.MergedField;
import graphql.execution.NormalizedVariables;
import graphql.language.Field;
import graphql.schema.GraphQLSchema;

import java.util.Locale;
import java.util.function.Supplier;

@Internal
public class QueryDirectivesBuilder implements QueryDirectives.Builder {

private MergedField mergedField;
private GraphQLSchema schema;
private CoercedVariables coercedVariables = CoercedVariables.emptyVariables();
private Supplier<NormalizedVariables> normalizedVariables = NormalizedVariables::emptyVariables;
private GraphQLContext graphQLContext = GraphQLContext.getDefault();
private Locale locale = Locale.getDefault();

Expand Down Expand Up @@ -42,6 +45,12 @@ public QueryDirectives.Builder coercedVariables(CoercedVariables coercedVariable
return this;
}

@Override
public QueryDirectives.Builder normalizedVariables(Supplier<NormalizedVariables> normalizedVariables) {
this.normalizedVariables = normalizedVariables;
return this;
}

@Override
public QueryDirectives.Builder graphQLContext(GraphQLContext graphQLContext) {
this.graphQLContext = graphQLContext;
Expand All @@ -57,6 +66,6 @@ public QueryDirectives.Builder locale(Locale locale) {

@Override
public QueryDirectives build() {
return new QueryDirectivesImpl(mergedField, schema, coercedVariables.toMap(), graphQLContext, locale);
return new QueryDirectivesImpl(mergedField, schema, coercedVariables, normalizedVariables, graphQLContext, locale);
}
}
Loading
Loading