Skip to content

Commit

Permalink
Merge pull request quarkusio#36348 from jmartisk/smallrye-graphql-2.5.0
Browse files Browse the repository at this point in the history
Smallrye GraphQL 2.5.0
  • Loading branch information
jmartisk authored Oct 11, 2023
2 parents 798dba7 + bba3785 commit 61d3986
Show file tree
Hide file tree
Showing 13 changed files with 412 additions and 6 deletions.
4 changes: 2 additions & 2 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<micrometer.version>1.11.1</micrometer.version><!-- keep in sync with hdrhistogram -->
<hdrhistogram.version>2.1.12</hdrhistogram.version><!-- keep in sync with micrometer -->
<google-auth.version>0.22.0</google-auth.version>
<graphql-java.version>20.2</graphql-java.version> <!-- keep in sync with smallrye-graphql -->
<graphql-java.version>21.1</graphql-java.version> <!-- keep in sync with smallrye-graphql -->
<microprofile-config-api.version>3.0.3</microprofile-config-api.version>
<microprofile-health-api.version>4.0.1</microprofile-health-api.version>
<microprofile-metrics-api.version>4.0.1</microprofile-metrics-api.version>
Expand All @@ -55,7 +55,7 @@
<smallrye-health.version>4.0.4</smallrye-health.version>
<smallrye-metrics.version>4.0.0</smallrye-metrics.version>
<smallrye-open-api.version>3.6.2</smallrye-open-api.version>
<smallrye-graphql.version>2.4.0</smallrye-graphql.version>
<smallrye-graphql.version>2.5.0</smallrye-graphql.version>
<smallrye-opentracing.version>3.0.3</smallrye-opentracing.version>
<smallrye-fault-tolerance.version>6.2.6</smallrye-fault-tolerance.version>
<smallrye-jwt.version>4.3.1</smallrye-jwt.version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,16 @@ void activateEventing(SmallRyeGraphQLConfig graphQLConfig, BuildProducer<SystemP
}
}

@BuildStep
void activateFederationBatchResolving(SmallRyeGraphQLConfig graphQLConfig,
BuildProducer<SystemPropertyBuildItem> systemProperties) {
if (graphQLConfig.federationBatchResolvingEnabled.isPresent()) {
String value = graphQLConfig.federationBatchResolvingEnabled.get().toString();
systemProperties.produce(new SystemPropertyBuildItem(ConfigKey.ENABLE_FEDERATION_BATCH_RESOLVING, value));
System.setProperty(ConfigKey.ENABLE_FEDERATION_BATCH_RESOLVING, value);
}
}

/*
* Decides whether we want to activate GraphQL federation and updates system properties accordingly.
* If quarkus.smallrye-graphql.federation.enabled is unspecified, enable federation automatically if we see
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.smallrye.graphql.deployment.federation.batch;

import io.smallrye.graphql.api.federation.Extends;
import io.smallrye.graphql.api.federation.External;
import io.smallrye.graphql.api.federation.Key;

@Key(fields = "id")
@Extends
public class Foo {

@External
public int id;

public String name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package io.quarkus.smallrye.graphql.deployment.federation.batch;

import java.util.List;
import java.util.stream.Collectors;

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

@GraphQLApi
public class FooApi {
@Query
public List<Foo> foos(List<Integer> id) {
return id.stream().map(this::foo).collect(Collectors.toList());
}

private Foo foo(int id) {
var foo = new Foo();
foo.id = id;
foo.name = "Name of " + id;
return foo;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package io.quarkus.smallrye.graphql.deployment.federation.batch;

import static org.hamcrest.Matchers.containsString;

import org.hamcrest.CoreMatchers;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.smallrye.graphql.deployment.AbstractGraphQLTest;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class GraphQLFederationBatchTest extends AbstractGraphQLTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(FooApi.class, Foo.class)
.addAsResource(new StringAsset("quarkus.smallrye-graphql.schema-include-directives=true\n" +
"quarkus.smallrye-graphql.federation.batch-resolving-enabled=true"),
"application.properties")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

@Test
public void checkServiceDeclarationInSchema() {
RestAssured.given()
.get("/graphql/schema.graphql")
.then()
.body(containsString("type _Service {"));
}

@Test
public void checkFederationDirectivesInSchema() {
RestAssured.given()
.get("/graphql/schema.graphql")
.then()
.body(containsString("id: Int! @external"))
.body(containsString("type Foo @extends @key(fields : \"id\")"));
;
}

@Test
public void resolvePerFederation() {
String query = "query federation($representations: [_Any!]!) {\n" +
" _entities(representations: $representations) {\n" +
" ... on Foo {\n" +
" id\n" +
" name\n" +
" }\n" +
" }\n" +
"}";
String variables = "{\n" +
" \"representations\": [\n" +
" {\n" +
" \"__typename\": \"Foo\",\n" +
" \"id\": 1\n" +
" },\n" +
" {\n" +
" \"__typename\": \"Foo\",\n" +
" \"id\": 2\n" +
" }\n" +
" ]\n" +
"}";

String request = getPayload(query, variables);
RestAssured.given().when()
.accept(MEDIATYPE_JSON)
.contentType(MEDIATYPE_JSON)
.body(request)
.post("/graphql")
.then()
.assertThat()
.statusCode(200)
.and()
.body(CoreMatchers.is(
"{\"data\":{\"_entities\":[{\"id\":1,\"name\":\"Name of 1\"},{\"id\":2,\"name\":\"Name of 2\"}]}}"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.quarkus.smallrye.graphql.deployment.federation.batch.uni;

import java.util.List;
import java.util.stream.Collectors;

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

import io.quarkus.smallrye.graphql.deployment.federation.batch.Foo;
import io.smallrye.mutiny.Uni;

@GraphQLApi
public class FooApiUni {

@Query
public Uni<List<Foo>> foos(List<Integer> id) {
return Uni.join().all(id.stream().map(this::foo).collect(Collectors.toList())).andFailFast();
}

private Uni<Foo> foo(int id) {
var foo = new Foo();
foo.id = id;
foo.name = "Name of " + id;
return Uni.createFrom().item(foo);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package io.quarkus.smallrye.graphql.deployment.federation.batch.uni;

import static org.hamcrest.Matchers.containsString;

import org.hamcrest.CoreMatchers;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.asset.StringAsset;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import io.quarkus.smallrye.graphql.deployment.AbstractGraphQLTest;
import io.quarkus.smallrye.graphql.deployment.federation.batch.Foo;
import io.quarkus.test.QuarkusUnitTest;
import io.restassured.RestAssured;

public class GraphQLFederationBatchUniTest extends AbstractGraphQLTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(FooApiUni.class, Foo.class)
.addAsResource(new StringAsset("quarkus.smallrye-graphql.schema-include-directives=true\n" +
"quarkus.smallrye-graphql.federation.batch-resolving-enabled=true"),
"application.properties")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

@Test
public void checkServiceDeclarationInSchema() {
RestAssured.given()
.get("/graphql/schema.graphql")
.then()
.body(containsString("type _Service {"));
}

@Test
public void checkFederationDirectivesInSchema() {
RestAssured.given()
.get("/graphql/schema.graphql")
.then()
.body(containsString("id: Int! @external"))
.body(containsString("type Foo @extends @key(fields : \"id\")"));
;
}

@Test
public void resolvePerFederation() {
String query = "query federation($representations: [_Any!]!) {\n" +
" _entities(representations: $representations) {\n" +
" ... on Foo {\n" +
" id\n" +
" name\n" +
" }\n" +
" }\n" +
"}";
String variables = "{\n" +
" \"representations\": [\n" +
" {\n" +
" \"__typename\": \"Foo\",\n" +
" \"id\": 1\n" +
" },\n" +
" {\n" +
" \"__typename\": \"Foo\",\n" +
" \"id\": 2\n" +
" }\n" +
" ]\n" +
"}";

String request = getPayload(query, variables);
RestAssured.given().when()
.accept(MEDIATYPE_JSON)
.contentType(MEDIATYPE_JSON)
.body(request)
.post("/graphql")
.then()
.assertThat()
.statusCode(200)
.and()
.body(CoreMatchers.is(
"{\"data\":{\"_entities\":[{\"id\":1,\"name\":\"Name of 1\"},{\"id\":2,\"name\":\"Name of 2\"}]}}"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package io.quarkus.smallrye.graphql.deployment.federation.complex;

import io.smallrye.graphql.api.federation.Extends;
import io.smallrye.graphql.api.federation.External;
import io.smallrye.graphql.api.federation.Key;

@Key(fields = "id")
@Key(fields = "otherId")
@Extends
public class Bar {

@External
public int id;
@External
public String otherId;

public String name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package io.quarkus.smallrye.graphql.deployment.federation.complex;

import io.smallrye.graphql.api.federation.Extends;
import io.smallrye.graphql.api.federation.External;
import io.smallrye.graphql.api.federation.Key;

@Key(fields = "id")
@Extends
public class Foo {

@External
public int id;

public String name;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.quarkus.smallrye.graphql.deployment.federation.complex;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

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

import io.smallrye.mutiny.Uni;

@GraphQLApi
public class FooApi {
@Query
public Foo foo(int id) {
var foo = new Foo();
foo.id = id;
foo.name = "Name of " + id;
return foo;
}

@Query
public Uni<List<Bar>> bars(List<Integer> id, List<String> otherId) {
return Uni.join().all(
IntStream.range(0, id.size()).boxed().map(i -> bar(id.get(i), otherId.get(i))).collect(Collectors.toList()))
.andFailFast();
}

private Uni<Bar> bar(int id, String otherId) {
var bar = new Bar();
bar.id = id;
bar.otherId = otherId;
bar.name = id + otherId;
return Uni.createFrom().item(bar);
}
}
Loading

0 comments on commit 61d3986

Please sign in to comment.