Skip to content

Commit

Permalink
added fix for non-necessary json transformation for ObjectId argument…
Browse files Browse the repository at this point in the history
… value
  • Loading branch information
mskacelik committed Nov 6, 2023
1 parent 53ed74a commit 891075f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,9 @@ private Object correctObjectClass(Object argumentValue, Field field, DataFetchin
} else if (receivedClassName.equals(String.class.getName())) {
// We got a String, but not expecting one. Lets bind to Pojo with JsonB
// This happens with @DefaultValue and Transformable (Passthrough) Scalars
if (field.getReference().getClassName().equals("org.bson.types.ObjectId")) { // edge case for ObjectId
return argumentValue;
}
return correctComplexObjectFromJsonString(argumentValue.toString(), field);
} else if (GraphQLScalarTypes.isGraphQLScalarType(field.getReference().getClassName())) {
GraphQLScalarType scalar = GraphQLScalarTypes.getScalarByClassName(field.getReference().getClassName());
Expand Down
5 changes: 5 additions & 0 deletions server/integration-tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@
<version>${version.smallrye-stork}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>bson</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jboss.shrinkwrap.resolver</groupId>
<artifactId>shrinkwrap-resolver-api-maven</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package io.smallrye.graphql.tests.objectid;

import org.bson.types.ObjectId;

import io.smallrye.graphql.api.Adapter;

public class ObjectIdAdapter implements Adapter<ObjectId, String> {
@Override
public ObjectId from(String o) {
return new ObjectId(o);
}

@Override
public String to(ObjectId a) throws Exception {
return a.toHexString();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package io.smallrye.graphql.tests.objectid;

import static org.assertj.core.api.Assertions.assertThat;

import java.net.URL;

import org.bson.types.ObjectId;
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.spec.WebArchive;
import org.junit.Test;
import org.junit.runner.RunWith;

import io.smallrye.graphql.tests.GraphQLAssured;

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

@Deployment
public static WebArchive deployment() {
return ShrinkWrap.create(WebArchive.class, "objectId-test.war")
.addClasses(SomeApi.class, ObjectIdAdapter.class);
}

@ArquillianResource
URL testingURL;

@Test
public void queryWithObjectIdArgumentTest() {
final String id = ObjectId.get().toHexString();
System.err.println(id);
GraphQLAssured graphQLAssured = new GraphQLAssured(testingURL);

String response = graphQLAssured
.post("{ returnObjectId(id: \"" + id + "\") }");
assertThat(response).contains("{\"data\":{\"returnObjectId\":\"" + id + "\"}}");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.smallrye.graphql.tests.objectid;

import org.bson.types.ObjectId;
import org.eclipse.microprofile.graphql.GraphQLApi;
import org.eclipse.microprofile.graphql.Query;

import io.smallrye.graphql.api.AdaptWith;

@GraphQLApi
public class SomeApi {
@Query
public @AdaptWith(ObjectIdAdapter.class) ObjectId returnObjectId(@AdaptWith(ObjectIdAdapter.class) ObjectId id) {
return id;
}

}

0 comments on commit 891075f

Please sign in to comment.