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

Add federation Resolver annotation to index #42

Merged
merged 2 commits into from
Sep 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -94,6 +94,7 @@
import io.smallrye.graphql.api.federation.Key;
import io.smallrye.graphql.api.federation.Provides;
import io.smallrye.graphql.api.federation.Requires;
import io.smallrye.graphql.api.federation.Resolver;
import io.smallrye.graphql.api.federation.Shareable;
import io.smallrye.graphql.api.federation.Tag;
import io.smallrye.graphql.api.federation.link.Import;
Expand Down Expand Up @@ -321,6 +322,7 @@ void buildFinalIndex(
indexer.indexClass(ScopeGroup.class);
indexer.indexClass(ScopeItem.class);
indexer.indexClass(Namespace.class);
indexer.indexClass(Resolver.class);
} catch (IOException ex) {
LOG.warn("Failure while creating index", ex);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package io.quarkus.smallrye.graphql.deployment.federation.resolver;

import org.eclipse.microprofile.graphql.GraphQLApi;

import io.smallrye.graphql.api.federation.Resolver;

@GraphQLApi
public class ExtendedApi {
@Resolver
public ExtendedType extendedTypeById(String id) {
ExtendedType extendedType = new ExtendedType();
extendedType.setId(id);
extendedType.setDescription("extendedTypeById");
return extendedType;
}

@Resolver
public ExtendedType extendedTypeByIdNameKey(String id, String name, String key) {
ExtendedType extendedType = new ExtendedType();
extendedType.setId(id);
extendedType.setValue(id + name + key);
extendedType.setDescription("extendedTypeByIdNameKey");
return extendedType;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package io.quarkus.smallrye.graphql.deployment.federation.resolver;

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

@Extends
@Key(fields = @FieldSet("id"))
public class ExtendedType {
@External
private String id;

@External
private String name;

@External
private String key;

@Requires(fields = @FieldSet("name key"))
private String value;

private String description;

public String getId() {
return id;
}

public void setId(String id) {
this.id = id;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getKey() {
return key;
}

public void setKey(String key) {
this.key = key;
}

public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package io.quarkus.smallrye.graphql.deployment.federation.resolver;

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 FederationResolverTest extends AbstractGraphQLTest {

@RegisterExtension
static QuarkusUnitTest test = new QuarkusUnitTest()
.withApplicationRoot((jar) -> jar
.addClasses(ExtendedApi.class, ExtendedType.class)
.addAsResource(new StringAsset("quarkus.smallrye-graphql.schema-include-directives=true"),
"application.properties")
.addAsManifestResource(EmptyAsset.INSTANCE, "beans.xml"));

@Test
public void resolverById() {
String request = getPayload(TEST_ID_QUERY);
RestAssured.given().when()
.accept(MEDIATYPE_JSON)
.contentType(MEDIATYPE_JSON)
.body(request)
.post("/graphql")
.then()
.assertThat()
.statusCode(200)
.and()
.body(CoreMatchers.is(
"{\"data\":{\"_entities\":[{\"id\":\"id\",\"description\":\"extendedTypeById\"}]}}"));
}

@Test
public void resolverByIdNameKey() {
String request = getPayload(TEST_ID_NAME_KEY_QUERY);
RestAssured.given().when()
.accept(MEDIATYPE_JSON)
.contentType(MEDIATYPE_JSON)
.body(request)
.post("/graphql")
.then()
.assertThat()
.statusCode(200)
.and()
.body(CoreMatchers.is(
"{\"data\":{\"_entities\":[{\"id\":\"id\",\"value\":\"idnamekey\",\"description\":\"extendedTypeByIdNameKey\"}]}}"));
}

private static final String TEST_ID_QUERY = "query {\n" +
"_entities(\n" +
" representations: { id: \"id\", __typename: \"ExtendedType\" }\n" +
") {\n" +
" ... on ExtendedType {\n" +
" id\n" +
" description\n" +
" }\n" +
" }\n" +
"}";

private static final String TEST_ID_NAME_KEY_QUERY = "query {\n" +
"_entities(\n" +
" representations: { id: \"id\", name: \"name\", key: \"key\", __typename: \"ExtendedType\" }\n" +
") {\n" +
" ... on ExtendedType {\n" +
" id\n" +
" value\n" +
" description\n" +
" }\n" +
" }\n" +
"}";
}
Loading