-
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 4 commits
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 |
---|---|---|
|
@@ -12,8 +12,10 @@ | |
import java.util.List; | ||
import java.util.Map; | ||
|
||
import org.eclipse.microprofile.graphql.Name; | ||
import org.jboss.logging.Logger; | ||
|
||
import io.smallrye.graphql.api.Namespace; | ||
import io.smallrye.graphql.client.impl.ErrorMessageProvider; | ||
import io.smallrye.graphql.client.impl.GraphQLClientConfiguration; | ||
import io.smallrye.graphql.client.impl.GraphQLClientsConfiguration; | ||
|
@@ -145,6 +147,14 @@ public VertxTypesafeGraphQLClientBuilder websocketInitializationTimeout(Integer | |
|
||
@Override | ||
public <T> T build(Class<T> apiClass) { | ||
Name nameAnnotation = apiClass.getAnnotation(Name.class); | ||
Namespace namespaceAnnotation = apiClass.getAnnotation(Namespace.class); | ||
|
||
if (nameAnnotation != null && namespaceAnnotation != null) { | ||
throw new RuntimeException("You can only use one of the annotations - @Name or @Namespace " + | ||
"over the GraphQLClientApi interface. Please, fix next interface: " + apiClass.getName()); | ||
} | ||
|
||
if (this.options == null) { | ||
this.options = new WebClientOptions(); | ||
} | ||
|
@@ -185,7 +195,8 @@ public <T> T build(Class<T> apiClass) { | |
endpoint, | ||
websocketUrl, executeSingleOperationsOverWebsocket, httpClient, webClient, subprotocols, | ||
websocketInitializationTimeout, | ||
allowUnexpectedResponseFields); | ||
allowUnexpectedResponseFields, | ||
namespaceAnnotation != null); | ||
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. Why would you pass a |
||
|
||
return apiClass.cast(Proxy.newProxyInstance(getClassLoader(apiClass), new Class<?>[] { apiClass }, | ||
(proxy, method, args) -> invoke(graphQLClient, method, args))); | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,13 +1,16 @@ | ||||||
package io.smallrye.graphql.client.model; | ||||||
|
||||||
import static io.smallrye.graphql.client.model.Annotations.GRAPHQL_CLIENT_API; | ||||||
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.ScanningContext.getIndex; | ||||||
|
||||||
import java.util.ArrayList; | ||||||
import java.util.Collection; | ||||||
import java.util.HashMap; | ||||||
import java.util.List; | ||||||
import java.util.Map; | ||||||
import java.util.stream.Collectors; | ||||||
|
||||||
import org.jboss.jandex.AnnotationInstance; | ||||||
import org.jboss.jandex.ClassInfo; | ||||||
|
@@ -56,12 +59,15 @@ private ClientModels generateClientModels() { | |||||
Collection<AnnotationInstance> graphQLApiAnnotations = getIndex() | ||||||
.getAnnotations(GRAPHQL_CLIENT_API); | ||||||
|
||||||
validateAnnotations(graphQLApiAnnotations); | ||||||
|
||||||
graphQLApiAnnotations.forEach(graphQLApiAnnotation -> { | ||||||
ClientModel operationMap = new ClientModel(); | ||||||
ClassInfo apiClass = graphQLApiAnnotation.target().asClass(); | ||||||
boolean useNamespace = apiClass.hasDeclaredAnnotation(NAMESPACE); | ||||||
List<MethodInfo> methods = getAllMethodsIncludingFromSuperClasses(apiClass); | ||||||
methods.forEach(method -> { | ||||||
String query = new QueryBuilder(method).build(); | ||||||
String query = new QueryBuilder(method).build(useNamespace); | ||||||
LOG.debugf("[%s] – Query created: %s", apiClass.name().toString(), query); | ||||||
operationMap.getOperationMap() | ||||||
.putIfAbsent(OperationModel.of(method).getMethodKey(), query); | ||||||
|
@@ -75,6 +81,19 @@ private ClientModels generateClientModels() { | |||||
return clientModels; | ||||||
} | ||||||
|
||||||
private void validateAnnotations(Collection<AnnotationInstance> graphQLApiAnnotations) { | ||||||
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. Could we name this |
||||||
List<String> errorInterfaces = graphQLApiAnnotations.stream() | ||||||
.map(annotation -> annotation.target().asClass()) | ||||||
.filter(classInfo -> classInfo.hasDeclaredAnnotation(NAMESPACE) && classInfo.hasDeclaredAnnotation(NAME)) | ||||||
.map(classInfo -> classInfo.name().toString()) | ||||||
.collect(Collectors.toList()); | ||||||
if (!errorInterfaces.isEmpty()) { | ||||||
throw new RuntimeException("You can only use one of the annotations - @Name or @Namespace " + | ||||||
"over the GraphQLClientApi interface. Please, fix next interfaces: " + | ||||||
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
|
||||||
String.join(", ", errorInterfaces)); | ||||||
} | ||||||
} | ||||||
|
||||||
/** | ||||||
* Retrieves all methods, including those from superclasses (interfaces). | ||||||
* | ||||||
|
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.