Skip to content

Commit

Permalink
fix: ExecutionInput locale is null
Browse files Browse the repository at this point in the history
  • Loading branch information
pipinet authored and jmartisk committed Dec 18, 2023
1 parent f4e5147 commit 92529ab
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 28 deletions.
Original file line number Diff line number Diff line change
@@ -1,24 +1,12 @@
package io.smallrye.graphql.execution;

import static io.smallrye.graphql.SmallRyeGraphQLServerLogging.log;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;

import jakarta.json.JsonObject;

import org.dataloader.BatchLoaderWithContext;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderFactory;
import org.dataloader.DataLoaderRegistry;

import graphql.*;
import graphql.ExecutionInput;
import graphql.ExecutionInput.Builder;
import graphql.ExecutionResult;
import graphql.ExecutionResultImpl;
import graphql.GraphQL;
import graphql.GraphQLError;
import graphql.GraphqlErrorBuilder;
import graphql.analysis.MaxQueryComplexityInstrumentation;
import graphql.analysis.MaxQueryDepthInstrumentation;
import graphql.execution.ExecutionId;
Expand All @@ -42,6 +30,22 @@
import io.smallrye.graphql.spi.config.Config;
import io.smallrye.graphql.spi.config.LogPayloadOption;
import io.smallrye.mutiny.Uni;
import jakarta.json.JsonObject;
import org.dataloader.BatchLoaderWithContext;
import org.dataloader.DataLoader;
import org.dataloader.DataLoaderFactory;
import org.dataloader.DataLoaderRegistry;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;

import static io.smallrye.graphql.SmallRyeGraphQLServerLogging.log;

/**
* Executing the GraphQL request
Expand Down Expand Up @@ -75,7 +79,7 @@ public ExecutionService(GraphQLSchema graphQLSchema, Schema schema) {
}

public ExecutionService(GraphQLSchema graphQLSchema, Schema schema, ExecutionStrategy queryExecutionStrategy,
ExecutionStrategy mutationExecutionStrategy) {
ExecutionStrategy mutationExecutionStrategy) {

this.graphQLSchema = graphQLSchema;
this.schema = schema;
Expand Down Expand Up @@ -151,6 +155,7 @@ public void execute(JsonObject jsonInput, Map<String, Object> context, Execution
// Query
Builder executionBuilder = ExecutionInput.newExecutionInput()
.query(query)
.locale(resolveLocale(context))
.executionId(finalExecutionId);

// Variables
Expand Down Expand Up @@ -195,6 +200,14 @@ public void execute(JsonObject jsonInput, Map<String, Object> context, Execution
}
}

private Locale resolveLocale(Map<String, Object> context) {
Object mayLocale = context.get("locale");
if (mayLocale instanceof Locale) {
return (Locale) mayLocale;
}
return Locale.getDefault();
}

private static void sendError(String errorMessage, ExecutionResponseWriter writer) {
GraphQLError error = GraphqlErrorBuilder
.newError()
Expand All @@ -209,9 +222,9 @@ private static void sendError(String errorMessage, ExecutionResponseWriter write
}

private void writeAsync(GraphQL graphQL,
ExecutionInput executionInput,
SmallRyeContext smallRyeContext,
ExecutionResponseWriter writer) {
ExecutionInput executionInput,
SmallRyeContext smallRyeContext,
ExecutionResponseWriter writer) {

Uni.createFrom().completionStage(() -> graphQL.executeAsync(executionInput))

Expand All @@ -229,9 +242,9 @@ private void writeAsync(GraphQL graphQL,
}

private void writeSync(GraphQL g,
ExecutionInput executionInput,
SmallRyeContext smallRyeContext,
ExecutionResponseWriter writer) {
ExecutionInput executionInput,
SmallRyeContext smallRyeContext,
ExecutionResponseWriter writer) {
try {
ExecutionResult executionResult = g.execute(executionInput);
notifyAndWrite(smallRyeContext, executionResult, writer);
Expand All @@ -241,8 +254,8 @@ private void writeSync(GraphQL g,
}

private void notifyAndWrite(SmallRyeContext smallRyeContext,
ExecutionResult executionResult,
ExecutionResponseWriter writer) {
ExecutionResult executionResult,
ExecutionResponseWriter writer) {
smallRyeContext.setExecutionResult(executionResult);
// Notify after
eventEmitter.fireAfterExecute(smallRyeContext);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public class GraphQLExceptionWhileDataFetching extends ExceptionWhileDataFetchin

public GraphQLExceptionWhileDataFetching(ResultPath path, Throwable exception, SourceLocation sourceLocation) {
super(path, exception, sourceLocation);
this.message = super.getException().getMessage();
this.message = super.getException().getLocalizedMessage();
}

public GraphQLExceptionWhileDataFetching(String message, ResultPath path, Throwable exception,
Expand Down

0 comments on commit 92529ab

Please sign in to comment.