Skip to content

Commit 7371216

Browse files
authored
Merge pull request #10141 from phillip-kruger/smallrye-graphql-update
Update SmallRye GraphQL version to 1.0.4
2 parents 2583dcb + f83d9c8 commit 7371216

File tree

9 files changed

+345
-23
lines changed

9 files changed

+345
-23
lines changed

bom/runtime/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
<smallrye-health.version>2.2.1</smallrye-health.version>
3737
<smallrye-metrics.version>2.4.2</smallrye-metrics.version>
3838
<smallrye-open-api.version>2.0.2</smallrye-open-api.version>
39-
<smallrye-graphql.version>1.0.3</smallrye-graphql.version>
39+
<smallrye-graphql.version>1.0.4</smallrye-graphql.version>
4040
<smallrye-opentracing.version>1.3.4</smallrye-opentracing.version>
4141
<smallrye-fault-tolerance.version>4.3.0</smallrye-fault-tolerance.version>
4242
<smallrye-jwt.version>2.1.2</smallrye-jwt.version>

extensions/smallrye-graphql/deployment/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,12 @@
8080
<artifactId>quarkus-elytron-security-properties-file</artifactId>
8181
<scope>test</scope>
8282
</dependency>
83+
<dependency>
84+
<groupId>io.opentracing</groupId>
85+
<artifactId>opentracing-mock</artifactId>
86+
<scope>test</scope>
87+
</dependency>
88+
8389
</dependencies>
8490

8591
<build>

extensions/smallrye-graphql/deployment/src/main/java/io/quarkus/smallrye/graphql/deployment/SmallRyeGraphQLProcessor.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ private Class[] getGraphQLJavaClasses() {
264264
classes.add(graphql.schema.GraphQLScalarType.class);
265265
classes.add(graphql.schema.GraphQLSchema.class);
266266
classes.add(graphql.schema.GraphQLTypeReference.class);
267+
classes.add(List.class);
267268
return classes.toArray(new Class[] {});
268269
}
269270

extensions/smallrye-graphql/deployment/src/test/java/io/quarkus/smallrye/graphql/deployment/GraphQLTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,11 @@ public void testBasicPost() {
5151

5252
@Test
5353
public void testBasicGet() {
54-
String pingRequest = getPayload("{\n" +
54+
String pingRequest = "{\n" +
5555
" ping {\n" +
5656
" message\n" +
5757
" }\n" +
58-
"}");
58+
"}";
5959

6060
RestAssured.given().when()
6161
.accept(MEDIATYPE_JSON)

extensions/smallrye-graphql/runtime/src/main/java/io/quarkus/smallrye/graphql/runtime/SmallRyeGraphQLExecutionHandler.java

Lines changed: 67 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,20 @@
22

33
import java.io.ByteArrayInputStream;
44
import java.io.IOException;
5+
import java.io.StringReader;
56
import java.io.StringWriter;
6-
import java.nio.charset.StandardCharsets;
7+
import java.io.UnsupportedEncodingException;
8+
import java.net.URLDecoder;
79
import java.util.List;
810

911
import javax.json.Json;
12+
import javax.json.JsonBuilderFactory;
1013
import javax.json.JsonObject;
14+
import javax.json.JsonObjectBuilder;
1115
import javax.json.JsonReader;
16+
import javax.json.JsonReaderFactory;
1217
import javax.json.JsonWriter;
18+
import javax.json.JsonWriterFactory;
1319

1420
import io.quarkus.arc.Arc;
1521
import io.quarkus.arc.ManagedContext;
@@ -29,12 +35,16 @@
2935
public class SmallRyeGraphQLExecutionHandler implements Handler<RoutingContext> {
3036
private static boolean allowGet = false;
3137
private static final String QUERY = "query";
38+
private static final String VARIABLES = "variables";
3239
private static final String OK = "OK";
3340
private volatile ExecutionService executionService;
3441
private final CurrentIdentityAssociation currentIdentityAssociation;
42+
private static final JsonBuilderFactory jsonObjectFactory = Json.createBuilderFactory(null);
43+
private static final JsonReaderFactory jsonReaderFactory = Json.createReaderFactory(null);
44+
private static final JsonWriterFactory jsonWriterFactory = Json.createWriterFactory(null);
3545

3646
public SmallRyeGraphQLExecutionHandler(boolean allowGet, CurrentIdentityAssociation currentIdentityAssociation) {
37-
this.allowGet = allowGet;
47+
SmallRyeGraphQLExecutionHandler.allowGet = allowGet;
3848
this.currentIdentityAssociation = currentIdentityAssociation;
3949
}
4050

@@ -96,13 +106,26 @@ private void handlePost(HttpServerResponse response, RoutingContext ctx) {
96106

97107
private void handleGet(HttpServerResponse response, RoutingContext ctx) {
98108
if (allowGet) {
99-
List<String> queries = ctx.queryParam(QUERY);
100-
if (queries != null && !queries.isEmpty()) {
101-
String graphqlGetRequest = queries.get(0);
102-
String getResponse = doRequest(graphqlGetRequest.getBytes(StandardCharsets.UTF_8));
103-
response.setStatusCode(200)
104-
.setStatusMessage(OK)
105-
.end(Buffer.buffer(getResponse));
109+
String query = getQueryParameter(ctx, QUERY);
110+
if (query != null && !query.isEmpty()) {
111+
try {
112+
String variables = getQueryParameter(ctx, VARIABLES);
113+
114+
JsonObjectBuilder input = jsonObjectFactory.createObjectBuilder();
115+
input.add(QUERY, URLDecoder.decode(query, "UTF8"));
116+
if (variables != null && !variables.isEmpty()) {
117+
JsonObject jsonObject = toJsonObject(URLDecoder.decode(variables, "UTF8"));
118+
input.add(VARIABLES, jsonObject);
119+
}
120+
121+
String getResponse = doRequest(input.build());
122+
123+
response.setStatusCode(200)
124+
.setStatusMessage(OK)
125+
.end(Buffer.buffer(getResponse));
126+
} catch (UnsupportedEncodingException ex) {
127+
throw new RuntimeException(ex);
128+
}
106129
} else {
107130
response.setStatusCode(204).end();
108131
}
@@ -111,6 +134,14 @@ private void handleGet(HttpServerResponse response, RoutingContext ctx) {
111134
}
112135
}
113136

137+
private String getQueryParameter(RoutingContext ctx, String parameterName) {
138+
List<String> all = ctx.queryParam(parameterName);
139+
if (all != null && !all.isEmpty()) {
140+
return all.get(0);
141+
}
142+
return null;
143+
}
144+
114145
private String getAllowedMethods() {
115146
if (allowGet) {
116147
return "GET, POST, OPTIONS";
@@ -121,23 +152,39 @@ private String getAllowedMethods() {
121152

122153
private String doRequest(final byte[] body) {
123154
try (ByteArrayInputStream input = new ByteArrayInputStream(body);
124-
final JsonReader jsonReader = Json.createReader(input)) {
155+
final JsonReader jsonReader = jsonReaderFactory.createReader(input)) {
125156
JsonObject jsonInput = jsonReader.readObject();
126-
JsonObject outputJson = getExecutionService().execute(jsonInput);
127-
if (outputJson != null) {
128-
try (StringWriter output = new StringWriter();
129-
final JsonWriter jsonWriter = Json.createWriter(output)) {
130-
jsonWriter.writeObject(outputJson);
131-
output.flush();
132-
return output.toString();
133-
}
134-
}
135-
return null;
157+
return doRequest(jsonInput);
136158
} catch (IOException ex) {
137159
throw new RuntimeException(ex);
138160
}
139161
}
140162

163+
private String doRequest(JsonObject jsonInput) {
164+
JsonObject outputJson = getExecutionService().execute(jsonInput);
165+
if (outputJson != null) {
166+
try (StringWriter output = new StringWriter();
167+
final JsonWriter jsonWriter = jsonWriterFactory.createWriter(output)) {
168+
jsonWriter.writeObject(outputJson);
169+
output.flush();
170+
return output.toString();
171+
} catch (IOException ex) {
172+
throw new RuntimeException(ex);
173+
}
174+
}
175+
return null;
176+
}
177+
178+
private static JsonObject toJsonObject(String jsonString) {
179+
if (jsonString == null || jsonString.isEmpty()) {
180+
return null;
181+
}
182+
183+
try (JsonReader jsonReader = jsonReaderFactory.createReader(new StringReader(jsonString))) {
184+
return jsonReader.readObject();
185+
}
186+
}
187+
141188
private ExecutionService getExecutionService() {
142189
if (this.executionService == null) {
143190
this.executionService = Arc.container().instance(ExecutionService.class).get();

tcks/microprofile-graphql/pom.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
<dependenciesToScan>
2323
<dependency>org.eclipse.microprofile.graphql:microprofile-graphql-tck</dependency>
2424
</dependenciesToScan>
25+
<suiteXmlFiles>
26+
<suiteXmlFile>testng.xml</suiteXmlFile>
27+
</suiteXmlFiles>
2528
</configuration>
2629
</plugin>
2730
</plugins>

0 commit comments

Comments
 (0)