Skip to content

Commit

Permalink
Added feature to add extensions to SmallRyeContext object, added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mskacelik committed Feb 21, 2023
1 parent d5c6af1 commit c5bfc57
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -256,4 +256,19 @@ public boolean hasRequest() {
public String toString() {
return SmallRyeContextManager.getCurrentSmallRyeContext().toString();
}

@Override
public Map<String, Object> getAddedExtensions() {
return SmallRyeContextManager.getCurrentSmallRyeContext().getAddedExtensions();
}

@Override
public void setAddedExtensions(Map<String, Object> addedExtensions) {
SmallRyeContextManager.getCurrentSmallRyeContext().setAddedExtensions(addedExtensions);
}

@Override
public void addExtension(String key, Object value) {
SmallRyeContextManager.getCurrentSmallRyeContext().addExtension(key, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -42,8 +43,15 @@ public class ExecutionResponse {

private final ExecutionResult executionResult;

private Map<String, Object> addedExtensions;

public ExecutionResponse(ExecutionResult executionResult) {
this(executionResult, null);
}

public ExecutionResponse(ExecutionResult executionResult, Map<String, Object> addedExtensions) {
this.executionResult = executionResult;
this.addedExtensions = addedExtensions;
}

public String toString() {
Expand Down Expand Up @@ -97,13 +105,19 @@ private JsonObjectBuilder addDataToResponse(JsonObjectBuilder returnObjectBuilde

private JsonObjectBuilder addExtensionsToResponse(JsonObjectBuilder returnObjectBuilder, ExecutionResult executionResult) {
final Map<Object, Object> extensions = executionResult.getExtensions();
if (extensions != null) {
JsonObject extensionsObject = buildExtensions(extensions);
returnObjectBuilder = returnObjectBuilder.add(EXTENSIONS, extensionsObject);
if (extensions != null) { // ERRORS
returnObjectBuilder = addExtensionToBuilder(extensions, returnObjectBuilder);
} else if (addedExtensions != null && !addedExtensions.isEmpty()) { // ADDED EXTENSIONS
returnObjectBuilder = addExtensionToBuilder(new HashMap(addedExtensions), returnObjectBuilder);
}
return returnObjectBuilder;
}

private JsonObjectBuilder addExtensionToBuilder(Map<Object, Object> extensions, JsonObjectBuilder returnObjectBuilder) {
JsonObject extensionsObject = buildExtensions(extensions);
return returnObjectBuilder.add(EXTENSIONS, extensionsObject);
}

private JsonObject buildExtensions(final Map<Object, Object> extensions) {
JsonObjectBuilder extensionsBuilder = jsonObjectFactory.createObjectBuilder();
for (final Map.Entry<Object, Object> entry : extensions.entrySet()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,11 @@ private void notifyAndWrite(SmallRyeContext smallRyeContext,
ExecutionResult executionResult,
ExecutionResponseWriter writer) {
smallRyeContext.setExecutionResult(executionResult);

// Notify after
eventEmitter.fireAfterExecute(smallRyeContext);

ExecutionResponse executionResponse = new ExecutionResponse(executionResult);
ExecutionResponse executionResponse = new ExecutionResponse(executionResult,
smallRyeContext.getAddedExtensions());
if (!payloadOption.equals(LogPayloadOption.off)) {
log.payloadOut(executionResponse.toString());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import static io.smallrye.graphql.SmallRyeGraphQLServerMessages.msg;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;
Expand Down Expand Up @@ -42,6 +43,19 @@ public class SmallRyeContext implements Context {
private QueryCache queryCache;
private DocumentSupplier documentSupplier;
private ExecutionResult executionResult;
private Map<String, Object> addedExtensions = new HashMap<>();

public Map<String, Object> getAddedExtensions() {
return addedExtensions;
}

public void setAddedExtensions(Map<String, Object> addedExtensions) {
this.addedExtensions = addedExtensions;
}

public void addExtension(String key, Object value) {
addedExtensions.put(key, value);
}

public SmallRyeContext(String createdBy) {
this.createdBy = createdBy;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.smallrye.graphql.tests.extensions;

public class Shirt {
private long size;

public Shirt() {
}

public long getSize() {
return size;
}

public void setSize(long size) {
this.size = size;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.smallrye.graphql.tests.extensions;

import java.util.List;

import jakarta.inject.Inject;

import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;

import io.smallrye.graphql.execution.context.SmallRyeContext;

@GraphQLApi
public class ShirtResource {

@Inject
SmallRyeContext smallRyeContext;

@Query
public List<Shirt> getShirts() {
smallRyeContext.addExtension("bar", "This is test for extensions");
smallRyeContext.addExtension("foo", 3.14159);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.smallrye.graphql.tests.extensions;

import java.net.URL;

import org.hamcrest.MatcherAssert;
import org.hamcrest.Matchers;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.container.test.api.RunAsClient;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.arquillian.test.api.ArquillianResource;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import io.smallrye.graphql.tests.GraphQLAssured;

@RunWith(Arquillian.class)
@RunAsClient
public class UserSupportedExtensionsTest {

@Deployment
public static WebArchive deployment() {
return ShrinkWrap.create(WebArchive.class, "user-extensions.war")
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml")
.addClasses(Shirt.class, ShirtResource.class);
}

@ArquillianResource
URL testingURL;

@Test
public void userAddedExtension() {
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL);
String request = "query { shirts { size } }";
String response = graphQLAssured.post(request);
MatcherAssert.assertThat(response, Matchers.containsString("\"extensions\":"));
MatcherAssert.assertThat(response, Matchers.containsString("\"bar\":\"This is test for extensions\""));
MatcherAssert.assertThat(response, Matchers.containsString("\"foo\":3.14159"));
}
}

0 comments on commit c5bfc57

Please sign in to comment.