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

#1570: add executionResult field to SmallRyeContext #1575

Merged
merged 4 commits into from
Sep 27, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Original file line number Diff line number Diff line change
Expand Up @@ -200,14 +200,7 @@ private void writeAsync(GraphQL graphQL,

SmallRyeContextManager.restore(smallRyeContext);

// Notify after
eventEmitter.fireAfterExecute(smallRyeContext);

ExecutionResponse executionResponse = new ExecutionResponse(executionResult);
if (!payloadOption.equals(LogPayloadOption.off)) {
log.payloadOut(executionResponse.toString());
}
writer.write(executionResponse);
notifyAndWrite(smallRyeContext, executionResult, writer);

}, failure -> {
if (failure != null) {
Expand All @@ -222,20 +215,29 @@ private void writeSync(GraphQL g,
ExecutionResponseWriter writer) {
try {
ExecutionResult executionResult = g.execute(executionInput);
// Notify after
eventEmitter.fireAfterExecute(smallRyeContext);

ExecutionResponse executionResponse = new ExecutionResponse(executionResult);
if (!payloadOption.equals(LogPayloadOption.off)) {
log.payloadOut(executionResponse.toString());
}

writer.write(executionResponse);
notifyAndWrite(smallRyeContext, executionResult, writer);
} catch (Throwable t) {
writer.fail(t);
}
}

private void notifyAndWrite(SmallRyeContext smallRyeContext,
ExecutionResult executionResult,
ExecutionResponseWriter writer
){
smallRyeContext.setExecutionResult(executionResult);

// Notify after
eventEmitter.fireAfterExecute(smallRyeContext);

ExecutionResponse executionResponse = new ExecutionResponse(executionResult);
if (!payloadOption.equals(LogPayloadOption.off)) {
log.payloadOut(executionResponse.toString());
}

writer.write(executionResponse);
}

private <K, T> DataLoaderRegistry getDataLoaderRegistry(List<Operation> operations) {
DataLoaderRegistry dataLoaderRegistry = new DataLoaderRegistry();
for (Operation operation : operations) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.Map;
import java.util.Optional;

import graphql.ExecutionResult;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;

Expand Down Expand Up @@ -40,6 +41,7 @@ public class SmallRyeContext implements Context {
private ExecutionInput executionInput;
private QueryCache queryCache;
private DocumentSupplier documentSupplier;
private ExecutionResult executionResult;

public SmallRyeContext(String createdBy) {
this.createdBy = createdBy;
Expand Down Expand Up @@ -199,6 +201,17 @@ public void setDocumentSupplier(DocumentSupplier documentSupplier) {
this.documentSupplier = documentSupplier;
}

public void setExecutionResult(ExecutionResult executionResult) {
this.executionResult = executionResult;
}

public Optional<ExecutionResult> getExecutionResult() {
if (this.executionResult != null) {
return Optional.of(this.executionResult);
}
return Optional.empty();
}
phillip-kruger marked this conversation as resolved.
Show resolved Hide resolved

@Override
public <T> T unwrap(Class<T> wrappedType) {
// We only support DataFetchingEnvironment, ExecutionInput and Document at this point
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package io.smallrye.graphql.execution.context;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;

import graphql.ExecutionResult;

import java.util.Optional;

import org.junit.jupiter.api.Test;

class SmallRyeContextTest {

@Test
void getAndSetExecutionResult() {
SmallRyeContext context = new SmallRyeContext(SmallRyeContextManager.class.getName());
assertEquals(context.getExecutionResult(), Optional.empty());

ExecutionResult executionResult = mock(ExecutionResult.class);
context.setExecutionResult(executionResult);
assertEquals(context.getExecutionResult(), Optional.of(executionResult));
}
}