-
Notifications
You must be signed in to change notification settings - Fork 93
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support for records as input on the server side
- Loading branch information
Showing
3 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
120 changes: 120 additions & 0 deletions
120
...r/integration-tests-jdk16/src/test/java/io/smallrye/graphql/tests/records/RecordTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package io.smallrye.graphql.tests.records; | ||
|
||
import io.smallrye.graphql.client.Response; | ||
import io.smallrye.graphql.client.core.Document; | ||
import io.smallrye.graphql.client.core.InputObject; | ||
import io.smallrye.graphql.client.core.InputObjectField; | ||
import io.smallrye.graphql.client.dynamic.api.DynamicGraphQLClient; | ||
import io.smallrye.graphql.client.dynamic.vertx.VertxDynamicGraphQLClientBuilder; | ||
import org.eclipse.microprofile.graphql.GraphQLApi; | ||
import org.eclipse.microprofile.graphql.Query; | ||
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 javax.json.bind.annotation.JsonbCreator; | ||
import java.net.URL; | ||
|
||
import static io.smallrye.graphql.client.core.Argument.arg; | ||
import static io.smallrye.graphql.client.core.Argument.args; | ||
import static io.smallrye.graphql.client.core.Document.document; | ||
import static io.smallrye.graphql.client.core.Field.field; | ||
import static io.smallrye.graphql.client.core.InputObject.inputObject; | ||
import static io.smallrye.graphql.client.core.InputObjectField.prop; | ||
import static io.smallrye.graphql.client.core.Operation.operation; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
/** | ||
* This test verifies that the server side can handle Java records in GraphQL apis, both as input and as output types. | ||
*/ | ||
@RunWith(Arquillian.class) | ||
@RunAsClient | ||
public class RecordTest { | ||
|
||
@Deployment | ||
public static WebArchive deployment() { | ||
return ShrinkWrap.create(WebArchive.class) | ||
.addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml") | ||
.addClasses(SimpleRecord.class); | ||
} | ||
|
||
@ArquillianResource | ||
URL testingURL; | ||
|
||
@Test | ||
public void testSimpleRecord() throws Exception { | ||
try (DynamicGraphQLClient client = new VertxDynamicGraphQLClientBuilder() | ||
.url(testingURL.toString() + "graphql").build()) { | ||
Document query = document(operation( | ||
field("simple", | ||
args(arg("input", | ||
inputObject(prop("a", "a"), prop("b", "b")))), | ||
field("a"), | ||
field("b")))); | ||
Response response = client.executeSync(query); | ||
assertEquals("a", response.getData().getJsonObject("simple").getString("a")); | ||
assertEquals("b", response.getData().getJsonObject("simple").getString("b")); | ||
} | ||
} | ||
|
||
@Test | ||
public void testSimpleRecordWithFactory() throws Exception { | ||
try (DynamicGraphQLClient client = new VertxDynamicGraphQLClientBuilder() | ||
.url(testingURL.toString() + "graphql").build()) { | ||
Document query = document(operation( | ||
field("simpleWithFactory", | ||
args(arg("input", | ||
inputObject(prop("a", "a"), prop("b", "b")))), | ||
field("a"), | ||
field("b")))); | ||
Response response = client.executeSync(query); | ||
System.out.println(response); | ||
System.out.println("query.build() = " + query.build()); | ||
assertEquals("a", response.getData().getJsonObject("simpleWithFactory").getString("a")); | ||
assertEquals("b", response.getData().getJsonObject("simpleWithFactory").getString("b")); | ||
} | ||
} | ||
|
||
@GraphQLApi | ||
public static class Api { | ||
|
||
@Query | ||
public SimpleRecord simple(SimpleRecord input) { | ||
return input; | ||
} | ||
|
||
@Query | ||
public SimpleRecordWithFactory simpleWithFactory(SimpleRecordWithFactory input) { | ||
return input; | ||
} | ||
|
||
} | ||
|
||
public record SimpleRecord(String a, String b) { | ||
|
||
// FIXME: until Yasson receives proper support for records, we have | ||
// to use this hack to tell Yasson to use the canonical constructor when | ||
// deserializing from JSON. Otherwise it will try to find and use a no-arg constructor, and | ||
// that does not exist. | ||
@JsonbCreator | ||
public SimpleRecord { | ||
} | ||
|
||
} | ||
|
||
public record SimpleRecordWithFactory(String a, String b) { | ||
|
||
@JsonbCreator | ||
public static SimpleRecordWithFactory build(String a, String b) { | ||
return new SimpleRecordWithFactory(a, b); | ||
} | ||
|
||
} | ||
|
||
} |