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

fix #624: typesafe client: @Name annotation on methods, too #682

Merged
merged 4 commits into from
Mar 19, 2021
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 @@ -18,10 +18,10 @@
import javax.enterprise.inject.Stereotype;

import org.eclipse.microprofile.graphql.Mutation;
import org.eclipse.microprofile.graphql.Name;
import org.eclipse.microprofile.graphql.Query;

import io.smallrye.graphql.client.typesafe.api.GraphQlClientException;
import io.smallrye.graphql.client.typesafe.impl.CollectionUtils;

public class MethodInvocation {
public static MethodInvocation of(Method method, Object... args) {
Expand All @@ -48,7 +48,7 @@ public String getKey() {
}

public boolean isQuery() {
return !ifAnnotated(Mutation.class).isPresent();
return !method.isAnnotationPresent(Mutation.class);
}

public String getName() {
Expand All @@ -58,15 +58,20 @@ public String getName() {
}

private Optional<String> queryName() {
return ifAnnotated(Query.class)
.map(Query::value)
.filter(CollectionUtils::nonEmpty);
Query query = method.getAnnotation(Query.class);
if (query != null && !query.value().isEmpty())
return Optional.of(query.value());
Name name = method.getAnnotation(Name.class);
if (name != null)
return Optional.of(name.value());
return Optional.empty();
}

private Optional<String> mutationName() {
return ifAnnotated(Mutation.class)
.map(Mutation::value)
.filter(CollectionUtils::nonEmpty);
Mutation mutation = method.getAnnotation(Mutation.class);
if (mutation != null && !mutation.value().isEmpty())
return Optional.of(mutation.value());
return Optional.empty();
}

private String methodName() {
Expand All @@ -76,10 +81,6 @@ private String methodName() {
return name;
}

private <T extends Annotation> Optional<T> ifAnnotated(Class<T> type) {
return Optional.ofNullable(method.getAnnotation(type));
}

public TypeInfo getReturnType() {
return new TypeInfo(type, method.getGenericReturnType(), method.getAnnotatedReturnType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ void shouldCallParamQuery() {
then(greeting).isEqualTo("hi, foo");
}

@GraphQlClientApi
interface RenamedMethodApi {
@Name("greeting")
String someOtherMethodName();
}

@Test
void shouldCallRenamedQuery() {
fixture.returnsData("'greeting':'hi, foo'");
RenamedMethodApi api = fixture.build(RenamedMethodApi.class);

String greeting = api.someOtherMethodName();

then(fixture.query()).isEqualTo("query greeting { greeting }");
then(fixture.variables()).isEqualTo("{}");
then(greeting).isEqualTo("hi, foo");
}

@GraphQlClientApi
interface ObjectApi {
Greeting greeting();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ static class Team {

@GraphQlClientApi
interface SuperHeroApi {
@Name("findTeams")
ErrorOr<List<Team>> teams();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,24 +57,6 @@ void shouldCallNonNullStringParamQuery() {
then(greeting).isEqualTo("hi, foo");
}

@GraphQlClientApi
interface RenamedParamApi {
String greeting(@Name("who") String foo);
}

@Test
void shouldCallRenamedParamQuery() {
fixture.returnsData("'greeting':'hi, foo'");
RenamedParamApi api = fixture.build(RenamedParamApi.class);

String greeting = api.greeting("foo");

then(fixture.query()).isEqualTo("query greeting($who: String) { greeting(who: $who) }");
then(fixture.variables()).isEqualTo("{'who':'foo'}");
then(fixture.operationName()).isEqualTo("greeting");
then(greeting).isEqualTo("hi, foo");
}

@Test
void shouldEscapeParamScalarQuery() {
fixture.returnsData("'greeting':'hi, foo'");
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@
<pushChanges>false</pushChanges>
<localCheckout>true</localCheckout>
<remoteTagging>false</remoteTagging>
<!--suppress MavenModelInspection -->
<arguments>-DskipTests ${release.arguments}</arguments>
</configuration>
</plugin>
Expand Down