-
Notifications
You must be signed in to change notification settings - Fork 91
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 @Namespace for GraphQLApi and GraphQLClientApi #2184
base: main
Are you sure you want to change the base?
Changes from 1 commit
d02c67f
10c16d2
eb9df1a
536955a
cc93826
1d7ad70
698fd8d
80c9496
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
@@ -24,14 +24,19 @@ public QueryBuilder(MethodInvocation method) { | |||||||||||||||
public String build() { | ||||||||||||||||
StringBuilder request = new StringBuilder(method.getOperationTypeAsString()); | ||||||||||||||||
request.append(" "); | ||||||||||||||||
request.append(method.getName()); | ||||||||||||||||
if (method.hasValueParameters()) | ||||||||||||||||
|
||||||||||||||||
request.append(method.getOperationName()); // operation name | ||||||||||||||||
|
||||||||||||||||
if (method.hasValueParameters()) { | ||||||||||||||||
request.append(method.valueParameters().map(this::declare).collect(joining(", ", "(", ")"))); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
String groupName = method.getGroupName(); | ||||||||||||||||
if (groupName != null) { | ||||||||||||||||
request.append(" { "); | ||||||||||||||||
request.append(groupName); | ||||||||||||||||
List<String> namespaces = method.getNamespaces(); | ||||||||||||||||
if (!namespaces.isEmpty()) { | ||||||||||||||||
namespaces.forEach(value -> { | ||||||||||||||||
request.append(" { "); | ||||||||||||||||
request.append(value); | ||||||||||||||||
}); | ||||||||||||||||
} | ||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||||
|
||||||||||||||||
if (method.isSingle()) { | ||||||||||||||||
|
@@ -45,11 +50,13 @@ public String build() { | |||||||||||||||
|
||||||||||||||||
request.append(fields(method.getReturnType())); | ||||||||||||||||
|
||||||||||||||||
if (method.isSingle()) | ||||||||||||||||
if (method.isSingle()) { | ||||||||||||||||
request.append(" }"); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
if (groupName != null) | ||||||||||||||||
request.append(" } "); | ||||||||||||||||
if (!namespaces.isEmpty()) { | ||||||||||||||||
request.append(" } ".repeat(namespaces.size())); | ||||||||||||||||
} | ||||||||||||||||
|
||||||||||||||||
return request.toString(); | ||||||||||||||||
} | ||||||||||||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package io.smallrye.graphql.client.impl.typesafe.reflection; | ||
|
||
import static java.util.stream.Collectors.joining; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.io.Closeable; | ||
|
@@ -10,10 +11,12 @@ | |
import java.lang.reflect.Modifier; | ||
import java.security.AccessController; | ||
import java.security.PrivilegedAction; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Objects; | ||
import java.util.Optional; | ||
import java.util.function.Function; | ||
import java.util.stream.Collectors; | ||
import java.util.stream.IntStream; | ||
import java.util.stream.Stream; | ||
|
||
|
@@ -23,6 +26,7 @@ | |
import org.eclipse.microprofile.graphql.Name; | ||
import org.eclipse.microprofile.graphql.Query; | ||
|
||
import io.smallrye.graphql.api.Namespace; | ||
import io.smallrye.graphql.api.Subscription; | ||
import io.smallrye.graphql.client.core.OperationType; | ||
import io.smallrye.graphql.client.model.MethodKey; | ||
|
@@ -36,14 +40,14 @@ public static MethodInvocation of(Method method, Object... args) { | |
private final TypeInfo type; | ||
private final Method method; | ||
private final Object[] parameterValues; | ||
private final String groupName; | ||
private final List<String> namespaces; | ||
private List<ParameterInfo> parameters; | ||
|
||
private MethodInvocation(TypeInfo type, Method method, Object[] parameterValues) { | ||
this.type = type; | ||
this.method = method; | ||
this.parameterValues = parameterValues; | ||
this.groupName = readGroupName(method); | ||
this.namespaces = readNamespace(method); | ||
} | ||
|
||
@Override | ||
|
@@ -262,18 +266,25 @@ public String getOperationTypeAsString() { | |
} | ||
} | ||
|
||
public String getGroupName() { | ||
return groupName; | ||
public List<String> getNamespaces() { | ||
return namespaces; | ||
} | ||
|
||
private String readGroupName(Method method) { | ||
Name annotation = method.getDeclaringClass().getAnnotation(Name.class); | ||
private List<String> readNamespace(Method method) { | ||
Namespace annotation = method.getDeclaringClass().getAnnotation(Namespace.class); | ||
if (annotation != null) { | ||
String groupName = annotation.value().trim(); | ||
if (!groupName.isEmpty()) { | ||
return groupName; | ||
} | ||
return Arrays.stream(annotation.value().split("/")) | ||
.map(String::trim) | ||
.collect(Collectors.toList()); | ||
} | ||
return null; | ||
return List.of(); | ||
} | ||
|
||
public String getOperationName() { | ||
return namespaces.stream().map(this::prettyString).collect(joining()) + prettyString(getName()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this. Why do we do this? And if we wouldn't uppercase the very first character, most changes to the tests would become unnecessary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For example
If don create first letter to upper case, operation will firstsecondthirdsomeMynotherNamefindAll. it is unreadable. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I think I get it: if the user wants to send multiple operations in one request, their names have to be distinct. But with the typesafe client, you can send multiple request only with a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When the Quarkus tests failed, I saw that Quarkus also checks the operation name.
As a result, the quarkus build will not crash, and there will be a minimum changes in the tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm obviously biased by the Java naming convention that methods start with a lowercase character. That's why I'd like to stick with that, also for operation names. I.e., also if there is a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I just referred to examples from the graphql documentation https://graphql.org/learn/queries/#operation-name |
||
} | ||
|
||
private String prettyString(String value) { | ||
return value.substring(0, 1).toUpperCase() + value.substring(1); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -3,6 +3,8 @@ | |||||
import static io.smallrye.graphql.client.model.helper.OperationModel.of; | ||||||
import static java.util.stream.Collectors.joining; | ||||||
|
||||||
import java.util.List; | ||||||
|
||||||
import org.jboss.jandex.MethodInfo; | ||||||
|
||||||
import io.smallrye.graphql.client.model.helper.DirectiveInstance; | ||||||
|
@@ -34,15 +36,19 @@ public QueryBuilder(MethodInfo method) { | |||||
public String build() { | ||||||
StringBuilder request = new StringBuilder(method.getOperationTypeAsString()); | ||||||
request.append(" "); | ||||||
request.append(method.getName()); // operationName | ||||||
|
||||||
request.append(method.getOperationName()); // operation name | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
if (method.hasValueParameters()) { | ||||||
request.append(method.valueParameters().stream().map(method::declare).collect(joining(", ", "(", ")"))); | ||||||
} | ||||||
|
||||||
String groupName = method.getGroupName(); | ||||||
if (groupName != null) { | ||||||
request.append(" { "); | ||||||
request.append(groupName); | ||||||
List<String> namespaces = method.getNamespaces(); | ||||||
if (!namespaces.isEmpty()) { | ||||||
namespaces.forEach(value -> { | ||||||
request.append(" { "); | ||||||
request.append(value); | ||||||
}); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as in the QueryBuilder. I really look forward to get rid of this kind of code duplication when @mskacelik finishes his work. |
||||||
} | ||||||
|
||||||
if (method.isSingle()) { | ||||||
|
@@ -61,11 +67,13 @@ public String build() { | |||||
|
||||||
request.append(method.fields(method.getReturnType())); | ||||||
|
||||||
if (method.isSingle()) | ||||||
if (method.isSingle()) { | ||||||
request.append(" }"); | ||||||
} | ||||||
|
||||||
if (groupName != null) | ||||||
request.append(" } "); | ||||||
if (!namespaces.isEmpty()) { | ||||||
request.append(" } ".repeat(namespaces.size())); | ||||||
} | ||||||
|
||||||
return request.toString(); | ||||||
} | ||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,12 +3,14 @@ | |
import static io.smallrye.graphql.client.model.Annotations.MULTIPLE; | ||
import static io.smallrye.graphql.client.model.Annotations.MUTATION; | ||
import static io.smallrye.graphql.client.model.Annotations.NAME; | ||
import static io.smallrye.graphql.client.model.Annotations.NAMESPACE; | ||
import static io.smallrye.graphql.client.model.Annotations.QUERY; | ||
import static io.smallrye.graphql.client.model.Annotations.SUBCRIPTION; | ||
import static io.smallrye.graphql.client.model.ScanningContext.getIndex; | ||
import static java.util.stream.Collectors.joining; | ||
import static java.util.stream.Collectors.toList; | ||
|
||
import java.util.Arrays; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import java.util.Stack; | ||
|
@@ -36,7 +38,7 @@ public class OperationModel implements NamedElement { | |
private final Stack<String> expressionStack = new Stack<>(); | ||
private Stack<TypeModel> rawParametrizedTypes = new Stack<>(); | ||
private final List<DirectiveInstance> directives; | ||
private final String groupName; | ||
private final List<String> namespaces; | ||
|
||
/** | ||
* Creates a new {@code OperationModel} instance based on the provided Jandex {@link MethodInfo}. | ||
|
@@ -50,7 +52,7 @@ public class OperationModel implements NamedElement { | |
getDirectiveLocation(), AnnotationTarget.Kind.METHOD) | ||
.map(DirectiveInstance::of) | ||
.collect(toList()); | ||
this.groupName = readGroupName(method); | ||
this.namespaces = readNamespace(method); | ||
} | ||
|
||
/** | ||
|
@@ -391,22 +393,25 @@ private boolean isRawParametrizedType(TypeModel type) { | |
return type.isCustomParametrizedType() && !type.getFirstRawType().isTypeVariable(); | ||
} | ||
|
||
public String getGroupName() { | ||
return groupName; | ||
public List<String> getNamespaces() { | ||
return namespaces; | ||
} | ||
|
||
private String readGroupName(MethodInfo method) { | ||
List<AnnotationInstance> annotationInstances = method.declaringClass().annotations(NAME); | ||
for (AnnotationInstance annotationInstance : annotationInstances) { | ||
if (annotationInstance.target().kind() == AnnotationTarget.Kind.CLASS) { | ||
if (annotationInstance.target().asClass().name().equals(method.declaringClass().name())) { | ||
String groupName = annotationInstance.value().asString().trim(); | ||
if (!groupName.isEmpty()) { | ||
return groupName; | ||
} | ||
} | ||
} | ||
private List<String> readNamespace(MethodInfo method) { | ||
AnnotationInstance annotationInstance = method.declaringClass().declaredAnnotation(NAMESPACE); | ||
if (annotationInstance != null) { | ||
return Arrays.stream(annotationInstance.value().asString().split("/")) | ||
.map(String::trim) | ||
.collect(Collectors.toList()); | ||
} | ||
return null; | ||
return List.of(); | ||
} | ||
|
||
public String getOperationName() { | ||
return namespaces.stream().map(this::prettyString).collect(joining()) + prettyString(getName()); | ||
} | ||
|
||
private String prettyString(String value) { | ||
return value.substring(0, 1).toUpperCase() + value.substring(1); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see above |
||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.