Skip to content

Commit

Permalink
WIP: typesafe rework
Browse files Browse the repository at this point in the history
  • Loading branch information
mskacelik committed Dec 19, 2023
1 parent 1acc749 commit 5b4ab01
Show file tree
Hide file tree
Showing 28 changed files with 2,537 additions and 19 deletions.
5 changes: 5 additions & 0 deletions client/api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<scope>provided</scope>
</dependency>

<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-graphql-client-model</artifactId>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;

import io.smallrye.graphql.client.model.ClientModels;
import io.smallrye.graphql.client.websocket.WebsocketSubprotocol;

/**
Expand Down Expand Up @@ -81,6 +82,8 @@ default TypesafeGraphQLClientBuilder headers(Map<String, String> headers) {

TypesafeGraphQLClientBuilder subprotocols(WebsocketSubprotocol... subprotocols);

TypesafeGraphQLClientBuilder clientModel(ClientModels.ClientModel clientModel);

TypesafeGraphQLClientBuilder allowUnexpectedResponseFields(boolean value);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import io.smallrye.graphql.client.impl.GraphQLClientConfiguration;
import io.smallrye.graphql.client.impl.GraphQLClientsConfiguration;
import io.smallrye.graphql.client.impl.typesafe.reflection.MethodInvocation;
import io.smallrye.graphql.client.model.ClientModels;
import io.smallrye.graphql.client.typesafe.api.GraphQLClientApi;
import io.smallrye.graphql.client.typesafe.api.TypesafeGraphQLClientBuilder;
import io.smallrye.graphql.client.vertx.VertxClientOptionsHelper;
Expand Down Expand Up @@ -48,6 +49,7 @@ public class VertxTypesafeGraphQLClientBuilder implements TypesafeGraphQLClientB
private HttpClient httpClient;
private Integer websocketInitializationTimeout;
private Boolean allowUnexpectedResponseFields;
private ClientModels.ClientModel clientModel;

public VertxTypesafeGraphQLClientBuilder() {
this.subprotocols = new ArrayList<>();
Expand Down Expand Up @@ -123,6 +125,12 @@ public VertxTypesafeGraphQLClientBuilder subprotocols(WebsocketSubprotocol... su
return this;
}

@Override
public VertxTypesafeGraphQLClientBuilder clientModel(ClientModels.ClientModel clientModel) {
this.clientModel = clientModel;
return this;
}

@Override
public TypesafeGraphQLClientBuilder allowUnexpectedResponseFields(boolean value) {
this.allowUnexpectedResponseFields = value;
Expand Down Expand Up @@ -167,14 +175,15 @@ public <T> T build(Class<T> apiClass) {
dynamicHeaders = new HashMap<>();
}

VertxTypesafeGraphQLClientProxy graphQlClient = new VertxTypesafeGraphQLClientProxy(apiClass, headers,
VertxTypesafeGraphQLClientProxy graphQLClient = new VertxTypesafeGraphQLClientProxy(apiClass, clientModel, headers,
dynamicHeaders, initPayload,
endpoint,
websocketUrl, executeSingleOperationsOverWebsocket, httpClient, webClient, subprotocols,
websocketInitializationTimeout,
allowUnexpectedResponseFields);

return apiClass.cast(Proxy.newProxyInstance(getClassLoader(apiClass), new Class<?>[] { apiClass },
(proxy, method, args) -> invoke(graphQlClient, method, args)));
(proxy, method, args) -> invoke(graphQLClient, method, args)));
}

private void applyConfigFor(Class<?> apiClass) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import io.smallrye.graphql.client.impl.typesafe.reflection.FieldInfo;
import io.smallrye.graphql.client.impl.typesafe.reflection.MethodInvocation;
import io.smallrye.graphql.client.impl.typesafe.reflection.TypeInfo;
import io.smallrye.graphql.client.model.ClientModels;
import io.smallrye.graphql.client.vertx.websocket.BuiltinWebsocketSubprotocolHandlers;
import io.smallrye.graphql.client.vertx.websocket.WebSocketSubprotocolHandler;
import io.smallrye.graphql.client.websocket.WebsocketSubprotocol;
Expand Down Expand Up @@ -77,6 +78,7 @@ class VertxTypesafeGraphQLClientProxy {
private final List<WebsocketSubprotocol> subprotocols;
private final Integer subscriptionInitializationTimeout;
private final Class<?> api;
private final ClientModels.ClientModel clientModel;
private final boolean executeSingleOperationsOverWebsocket;
private final boolean allowUnexpectedResponseFields;

Expand All @@ -91,6 +93,7 @@ class VertxTypesafeGraphQLClientProxy {

VertxTypesafeGraphQLClientProxy(
Class<?> api,
ClientModels.ClientModel clientModel,
Map<String, String> additionalHeaders,
Map<String, Uni<String>> dynamicHeaders,
Map<String, Object> initPayload,
Expand All @@ -103,6 +106,7 @@ class VertxTypesafeGraphQLClientProxy {
Integer subscriptionInitializationTimeout,
boolean allowUnexpectedResponseFields) {
this.api = api;
this.clientModel = clientModel;
this.additionalHeaders = additionalHeaders;
this.dynamicHeaders = dynamicHeaders;
this.initPayload = initPayload;
Expand Down Expand Up @@ -296,7 +300,12 @@ private Uni<WebSocketSubprotocolHandler> webSocketHandler() {

private JsonObject request(MethodInvocation method) {
JsonObjectBuilder request = jsonObjectFactory.createObjectBuilder();
String query = queryCache.computeIfAbsent(method.getKey(), key -> new QueryBuilder(method).build());
String query;
if (clientModel == null) {
query = queryCache.computeIfAbsent(method.getKey(), key -> new QueryBuilder(method).build());
} else {
query = clientModel.getOperationMap().get(method.getMethodIdentifier());
}
request.add("query", query);
request.add("variables", variables(method));
request.add("operationName", method.getName());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,18 +21,8 @@ public QueryBuilder(MethodInvocation method) {
}

public String build() {
StringBuilder request = new StringBuilder();
switch (method.getOperationType()) {
case QUERY:
request.append("query ");
break;
case MUTATION:
request.append("mutation ");
break;
case SUBSCRIPTION:
request.append("subscription ");
break;
}
StringBuilder request = new StringBuilder(method.getOperationTypeAsString());
request.append(" ");
request.append(method.getName());
if (method.hasValueParameters())
request.append(method.valueParameters().map(this::declare).collect(joining(", ", "(", ")")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -243,4 +243,19 @@ public boolean isDeclaredInCloseable() {
return method.getDeclaringClass().equals(Closeable.class) ||
method.getDeclaringClass().equals(AutoCloseable.class);
}

public String getMethodIdentifier() {
return getOperationTypeAsString() + "_" + getName();
}

public String getOperationTypeAsString() {
switch (getOperationType()) {
case MUTATION:
return "mutation";
case SUBSCRIPTION:
return "subscription ";
default:
return "query";
}
}
}
192 changes: 192 additions & 0 deletions client/model-builder/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,192 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-graphql-client-parent</artifactId>
<version>2.6.2-SNAPSHOT</version>
</parent>

<artifactId>smallrye-graphql-client-model-builder</artifactId>
<name>SmallRye: GraphQL Client :: model builder</name>
<dependencies>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>smallrye-graphql-client-model</artifactId>
</dependency>

<!-- Jandex indexer -->
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>jandex</artifactId>
</dependency>
<!-- Logging -->
<dependency>
<groupId>org.jboss.logging</groupId>
<artifactId>jboss-logging</artifactId>
<scope>provided</scope>
</dependency>

<!-- Test -->
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-graphql-client-api</artifactId>
</dependency>
<dependency>
<groupId>org.eclipse.microprofile.graphql</groupId>
<artifactId>microprofile-graphql-api</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye.reactive</groupId>
<artifactId>mutiny</artifactId>
</dependency>
<dependency>
<groupId>io.smallrye</groupId>
<artifactId>smallrye-graphql-client</artifactId>
</dependency>
</dependencies>

<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-surefire-plugin</artifactId>-->
<!-- <configuration>-->
<!-- <useFile>false</useFile>-->
<!-- <trimStackTrace>false</trimStackTrace>-->
<!-- </configuration>-->
<!-- </plugin>-->

<!-- <plugin>-->
<!-- <groupId>org.jetbrains.kotlin</groupId>-->
<!-- <artifactId>kotlin-maven-plugin</artifactId>-->
<!-- <version>${version.kotlin.compiler}</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <id>test-compile</id>-->
<!-- <goals>-->
<!-- <goal>test-compile</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <sourceDirs>-->
<!-- <dir>${project.basedir}/src/test/kotlin</dir>-->
<!-- </sourceDirs>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->
<!-- </plugin>-->


<!-- </plugins>-->
<!-- </build>-->

<!-- <profiles>-->

<!-- &lt;!&ndash; Run against the current released version &ndash;&gt;-->
<!-- <profile>-->
<!-- <id>v2.0.x</id>-->
<!-- <activation>-->
<!-- <property>-->
<!-- <name>!version.eclipse.microprofile.graphql</name>-->
<!-- </property>-->
<!-- </activation>-->

<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-dependency-plugin</artifactId>-->
<!-- <version>3.3.0</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <phase>generate-sources</phase>-->
<!-- <goals>-->
<!-- <goal>unpack</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <overWrite>true</overWrite>-->
<!-- <outputDirectory>${project.build.directory}/test-classes</outputDirectory>-->
<!-- <excludes>**/tests/,**/dynamic/,**/*Test.class,**/beans.xml</excludes>-->

<!-- <artifactItems>-->
<!-- <artifactItem>-->
<!-- <groupId>org.eclipse.microprofile.graphql</groupId>-->
<!-- <artifactId>microprofile-graphql-tck</artifactId>-->
<!-- <type>jar</type>-->
<!-- </artifactItem>-->
<!-- </artifactItems>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->

<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<!-- </profile>-->

<!-- &lt;!&ndash; Run against the snapshot version &ndash;&gt;-->
<!-- <profile>-->
<!-- <id>v2.1-SNAPSHOT</id>-->
<!-- <activation>-->
<!-- <property>-->
<!-- <name>version.eclipse.microprofile.graphql</name>-->
<!-- <value>2.1-SNAPSHOT</value>-->
<!-- </property>-->
<!-- </activation>-->
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.apache.maven.plugins</groupId>-->
<!-- <artifactId>maven-dependency-plugin</artifactId>-->
<!-- <version>3.3.0</version>-->
<!-- <executions>-->
<!-- <execution>-->
<!-- <phase>generate-sources</phase>-->
<!-- <goals>-->
<!-- <goal>unpack</goal>-->
<!-- </goals>-->
<!-- <configuration>-->
<!-- <overWrite>true</overWrite>-->
<!-- <outputDirectory>${project.build.directory}/test-classes</outputDirectory>-->
<!-- <excludes>**/tests/,**/dynamic/,**/*Test.class,**/beans.xml</excludes>-->

<!-- <artifactItems>-->
<!-- <artifactItem>-->
<!-- <groupId>org.eclipse.microprofile.graphql</groupId>-->
<!-- <artifactId>microprofile-graphql-server-tck</artifactId>-->
<!-- <type>jar</type>-->
<!-- </artifactItem>-->
<!-- </artifactItems>-->
<!-- </configuration>-->
<!-- </execution>-->
<!-- </executions>-->

<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<!-- </profile>-->


<!-- <profile>-->
<!-- <id>coverage</id>-->
<!-- <properties>-->
<!-- <argLine>@{jacocoArgLine}</argLine>-->
<!-- </properties>-->
<!-- <build>-->
<!-- <plugins>-->
<!-- <plugin>-->
<!-- <groupId>org.jacoco</groupId>-->
<!-- <artifactId>jacoco-maven-plugin</artifactId>-->
<!-- </plugin>-->
<!-- </plugins>-->
<!-- </build>-->
<!-- </profile>-->
<!-- </profiles>-->

</project>
Loading

0 comments on commit 5b4ab01

Please sign in to comment.