Skip to content

Commit 62c0d66

Browse files
authored
Upgrade graphiql (#2346)
* Upgrade to graphiql 2.2 Signed-off-by: Thomas Segismont <[email protected]> * Use graphiql toolkit as data fetcher Provides both HTTP and GraphQLWS transports Signed-off-by: Thomas Segismont <[email protected]> --------- Signed-off-by: Thomas Segismont <[email protected]>
1 parent 79faa1b commit 62c0d66

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+354
-144
lines changed

vertx-web-graphql/src/main/asciidoc/index.adoc

Lines changed: 32 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -59,35 +59,6 @@ To enable it, create the {@link io.vertx.ext.web.handler.graphql.GraphQLHandler}
5959
{@link examples.GraphQLExamples#handlerSetupBatching}
6060
----
6161

62-
=== GraphiQL IDE
63-
64-
As you are building your application, testing your GraphQL queries in https://github.com/graphql/graphiql[GraphiQL] can be handy.
65-
66-
To do so, create a route for GraphiQL resources and a {@link io.vertx.ext.web.handler.graphql.GraphiQLHandler} for them:
67-
68-
[source,$lang]
69-
----
70-
{@link examples.GraphQLExamples#handlerSetupGraphiQL}
71-
----
72-
73-
Then browse to http://localhost:8080/graphiql/.
74-
75-
NOTE: The GraphiQL user interface is disabled by default for security reasons.
76-
This is why you must configure the {@link io.vertx.ext.web.handler.graphql.GraphiQLHandlerOptions} to enable it.
77-
78-
TIP: GraphiQL is enabled automatically when Vert.x Web runs in development mode.
79-
To switch the development mode on, use the `VERTXWEB_ENVIRONMENT` environment variable or `vertxweb.environment` system property and set it to `dev`.
80-
In this case, create the {@link io.vertx.ext.web.handler.graphql.GraphiQLHandler} without changing the `enabled` property.
81-
82-
If your application is protected by authentication, you can customize the headers sent by GraphiQL dynamically:
83-
84-
[source,$lang]
85-
----
86-
{@link examples.GraphQLExamples#handlerSetupGraphiQLAuthn}
87-
----
88-
89-
Please refer to the {@link io.vertx.ext.web.handler.graphql.GraphiQLHandlerOptions} documentation for further details.
90-
9162
=== GraphQL over WebSocket
9263

9364
Vert.x Web GraphQL is compatible with the https://github.com/enisdenjo/graphql-ws[GraphQL over Websocket protocol].
@@ -124,6 +95,38 @@ To support both HTTP and Websockets on the same URI, the {@link io.vertx.ext.web
12495

12596
IMPORTANT: A _subscription_ `DataFetcher` has to return a `org.reactivestreams.Publisher` instance.
12697

98+
=== GraphiQL IDE
99+
100+
As you are building your application, testing your GraphQL queries in https://github.com/graphql/graphiql[GraphiQL] can be handy.
101+
102+
To do so, create a route for GraphiQL resources and a {@link io.vertx.ext.web.handler.graphql.GraphiQLHandler} for them:
103+
104+
[source,$lang]
105+
----
106+
{@link examples.GraphQLExamples#handlerSetupGraphiQL}
107+
----
108+
109+
Then browse to http://localhost:8080/graphiql/.
110+
111+
NOTE: The GraphiQL user interface is disabled by default for security reasons.
112+
This is why you must configure the {@link io.vertx.ext.web.handler.graphql.GraphiQLHandlerOptions} to enable it.
113+
114+
[TIP]
115+
====
116+
GraphiQL is enabled automatically when Vert.x Web runs in development mode.
117+
To switch the development mode on, use the `VERTXWEB_ENVIRONMENT` environment variable or `vertxweb.environment` system property and set it to `dev`.
118+
In this case, create the {@link io.vertx.ext.web.handler.graphql.GraphiQLHandler} without changing the `enabled` property.
119+
====
120+
121+
If your application is protected by authentication, you can customize the headers sent by GraphiQL dynamically:
122+
123+
[source,$lang]
124+
----
125+
{@link examples.GraphQLExamples#handlerSetupGraphiQLAuthn}
126+
----
127+
128+
Please refer to the {@link io.vertx.ext.web.handler.graphql.GraphiQLHandlerOptions} documentation for further details.
129+
127130
== Fetching data
128131

129132
The GraphQL-Java API is very well suited for the asynchronous world: the asynchronous execution strategy is the default for queries (serial asynchronous for mutations).

vertx-web-graphql/src/main/generated/io/vertx/ext/web/handler/graphql/GraphiQLHandlerOptionsConverter.java

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,18 @@ public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json,
3030
obj.setGraphQLUri((String)member.getValue());
3131
}
3232
break;
33+
case "graphQLWSEnabled":
34+
if (member.getValue() instanceof Boolean) {
35+
obj.setGraphQLWSEnabled((Boolean)member.getValue());
36+
}
37+
break;
38+
case "graphQLWSUri":
39+
break;
40+
case "graphWSQLUri":
41+
if (member.getValue() instanceof String) {
42+
obj.setGraphWSQLUri((String)member.getValue());
43+
}
44+
break;
3345
case "headers":
3446
if (member.getValue() instanceof JsonObject) {
3547
java.util.Map<String, java.lang.String> map = new java.util.LinkedHashMap<>();
@@ -40,6 +52,11 @@ public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json,
4052
obj.setHeaders(map);
4153
}
4254
break;
55+
case "httpEnabled":
56+
if (member.getValue() instanceof Boolean) {
57+
obj.setHttpEnabled((Boolean)member.getValue());
58+
}
59+
break;
4360
case "query":
4461
if (member.getValue() instanceof String) {
4562
obj.setQuery((String)member.getValue());
@@ -50,6 +67,11 @@ public static void fromJson(Iterable<java.util.Map.Entry<String, Object>> json,
5067
obj.setVariables(((JsonObject)member.getValue()).copy());
5168
}
5269
break;
70+
case "wsConnectionParams":
71+
if (member.getValue() instanceof JsonObject) {
72+
obj.setWsConnectionParams(((JsonObject)member.getValue()).copy());
73+
}
74+
break;
5375
}
5476
}
5577
}
@@ -63,16 +85,24 @@ public static void toJson(GraphiQLHandlerOptions obj, java.util.Map<String, Obje
6385
if (obj.getGraphQLUri() != null) {
6486
json.put("graphQLUri", obj.getGraphQLUri());
6587
}
88+
json.put("graphQLWSEnabled", obj.isGraphQLWSEnabled());
89+
if (obj.getGraphQLWSUri() != null) {
90+
json.put("graphQLWSUri", obj.getGraphQLWSUri());
91+
}
6692
if (obj.getHeaders() != null) {
6793
JsonObject map = new JsonObject();
6894
obj.getHeaders().forEach((key, value) -> map.put(key, value));
6995
json.put("headers", map);
7096
}
97+
json.put("httpEnabled", obj.isHttpEnabled());
7198
if (obj.getQuery() != null) {
7299
json.put("query", obj.getQuery());
73100
}
74101
if (obj.getVariables() != null) {
75102
json.put("variables", obj.getVariables());
76103
}
104+
if (obj.getWsConnectionParams() != null) {
105+
json.put("wsConnectionParams", obj.getWsConnectionParams());
106+
}
77107
}
78108
}

vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/GraphiQLHandlerOptions.java

Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,31 @@ public class GraphiQLHandlerOptions {
3636
*/
3737
public static final boolean DEFAULT_ENABLED = WebEnvironment.development();
3838

39+
/**
40+
* Whether HTTP transport should be enabled by default = true.
41+
*/
42+
public static final boolean DEFAULT_HTTP_ENABLED = true;
43+
44+
/**
45+
* Default URI for HTTP and GraphQLWS endpoints = /graphql.
46+
*/
47+
public static final String DEFAULT_GRAPHQL_URI = "/graphql";
48+
49+
/**
50+
* Whether GraphQLWS transport should be enabled by default = true.
51+
*/
52+
public static final boolean DEFAULT_GRAPHQL_WS_ENABLED = true;
53+
3954
private boolean enabled = DEFAULT_ENABLED;
4055

41-
private String graphQLUri;
56+
private boolean httpEnabled = DEFAULT_HTTP_ENABLED;
57+
private String graphQLUri = DEFAULT_GRAPHQL_URI;
58+
59+
private boolean graphQLWSEnabled = DEFAULT_GRAPHQL_WS_ENABLED;
60+
private String graphQLWSUri = DEFAULT_GRAPHQL_URI;
4261

4362
private Map<String, String> headers;
63+
private JsonObject wsConnectionParams;
4464

4565
private String query;
4666

@@ -59,8 +79,12 @@ public GraphiQLHandlerOptions() {
5979
*/
6080
public GraphiQLHandlerOptions(GraphiQLHandlerOptions other) {
6181
enabled = other.enabled;
82+
httpEnabled = other.httpEnabled;
6283
graphQLUri = other.graphQLUri;
84+
graphQLWSEnabled = other.graphQLWSEnabled;
85+
graphQLWSUri = other.graphQLWSUri;
6386
headers = other.headers == null ? null : new HashMap<>(other.headers);
87+
wsConnectionParams = other.wsConnectionParams == null ? null : other.wsConnectionParams.copy();
6488
query = other.query;
6589
variables = other.variables == null ? null : other.variables.copy();
6690
}
@@ -85,7 +109,7 @@ public JsonObject toJson() {
85109
}
86110

87111
/**
88-
* @return true if the GraphiQL development tool should be enabled, false otherwise
112+
* @return {@code true} if the GraphiQL development tool should be enabled, {@code false} otherwise
89113
*/
90114
public boolean isEnabled() {
91115
return enabled;
@@ -94,15 +118,32 @@ public boolean isEnabled() {
94118
/**
95119
* Whether the GraphiQL development tool should be enabled. Defaults to {@code false}.
96120
*
97-
* @param enabled true to enable the GraphiQL development tool, false otherwise
98-
*
121+
* @param enabled {@code true} to enable the GraphiQL development tool, {@code false} otherwise
99122
* @return a reference to this, so the API can be used fluently
100123
*/
101124
public GraphiQLHandlerOptions setEnabled(boolean enabled) {
102125
this.enabled = enabled;
103126
return this;
104127
}
105128

129+
/**
130+
* @return {@code true} if the HTTP transport should be enabled, {@code true} otherwise
131+
*/
132+
public boolean isHttpEnabled() {
133+
return httpEnabled;
134+
}
135+
136+
/**
137+
* Whether the HTTP transport should be enabled. Defaults to {@code true}.
138+
*
139+
* @param httpEnabled {@code true} to enable the HTTP transport, {@code false} otherwise
140+
* @return a reference to this, so the API can be used fluently
141+
*/
142+
public GraphiQLHandlerOptions setHttpEnabled(boolean httpEnabled) {
143+
this.httpEnabled = httpEnabled;
144+
return this;
145+
}
146+
106147
/**
107148
* @return the GraphQL endpoint URI
108149
*/
@@ -111,17 +152,52 @@ public String getGraphQLUri() {
111152
}
112153

113154
/**
114-
* Set the GraphQL endpoint URI. Defaults to the path used to get the GraphiQL user interface.
115-
*
116-
* @param graphQLUri the GraphQL endpoint URI
155+
* Set the GraphQL HTTP endpoint URI. Defaults to {@link #DEFAULT_GRAPHQL_URI}.
117156
*
157+
* @param graphQLUri the GraphQL HTTP endpoint URI
118158
* @return a reference to this, so the API can be used fluently
119159
*/
120160
public GraphiQLHandlerOptions setGraphQLUri(String graphQLUri) {
121161
this.graphQLUri = graphQLUri;
122162
return this;
123163
}
124164

165+
/**
166+
* @return {@code true} if the GraphQLWS transport should be enabled, {@code true} otherwise
167+
*/
168+
public boolean isGraphQLWSEnabled() {
169+
return graphQLWSEnabled;
170+
}
171+
172+
/**
173+
* Whether the GraphQLWS transport should be enabled. Defaults to {@code true}.
174+
*
175+
* @param graphQLWSEnabled {@code true} to enable the GraphQLWS transport, {@code false} otherwise
176+
* @return a reference to this, so the API can be used fluently
177+
*/
178+
public GraphiQLHandlerOptions setGraphQLWSEnabled(boolean graphQLWSEnabled) {
179+
this.graphQLWSEnabled = graphQLWSEnabled;
180+
return this;
181+
}
182+
183+
/**
184+
* @return the GraphQLWS endpoint URI
185+
*/
186+
public String getGraphQLWSUri() {
187+
return graphQLWSUri;
188+
}
189+
190+
/**
191+
* Set the GraphQLWS endpoint URI. Defaults to {@link #DEFAULT_GRAPHQL_URI}.
192+
*
193+
* @param graphQLWSUri the GraphQLWS endpoint URI
194+
* @return a reference to this, so the API can be used fluently
195+
*/
196+
public GraphiQLHandlerOptions setGraphWSQLUri(String graphQLWSUri) {
197+
this.graphQLWSUri = graphQLWSUri;
198+
return this;
199+
}
200+
125201
/**
126202
* @return the fixed set of HTTP headers to add to GraphiQL requests
127203
*/
@@ -133,14 +209,31 @@ public Map<String, String> getHeaders() {
133209
* A fixed set of HTTP headers to add to GraphiQL requests. Defaults to {@code null}.
134210
*
135211
* @param headers the set of HTTP headers to add to GraphiQL requests
136-
*
137212
* @return a reference to this, so the API can be used fluently
138213
*/
139214
public GraphiQLHandlerOptions setHeaders(Map<String, String> headers) {
140215
this.headers = headers;
141216
return this;
142217
}
143218

219+
/**
220+
* @return the initial GraphQLWS connection params
221+
*/
222+
public JsonObject getWsConnectionParams() {
223+
return wsConnectionParams;
224+
}
225+
226+
/**
227+
* Initial GraphQLWS connection params. Defaults to {@code null}.
228+
*
229+
* @param wsConnectionParams the initial GraphQLWS connection params
230+
* @return a reference to this, so the API can be used fluently
231+
*/
232+
public GraphiQLHandlerOptions setWsConnectionParams(JsonObject wsConnectionParams) {
233+
this.wsConnectionParams = wsConnectionParams;
234+
return this;
235+
}
236+
144237
/**
145238
* @return the query to set as initial value in the GraphiQL user interface
146239
*/

vertx-web-graphql/src/main/java/io/vertx/ext/web/handler/graphql/impl/GraphiQLHandlerImpl.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@
3131
import java.util.Objects;
3232
import java.util.function.Function;
3333

34-
import static java.nio.charset.StandardCharsets.UTF_8;
35-
import static java.util.concurrent.TimeUnit.DAYS;
36-
import static java.util.concurrent.TimeUnit.SECONDS;
34+
import static java.nio.charset.StandardCharsets.*;
35+
import static java.util.concurrent.TimeUnit.*;
3736

3837
/**
3938
* @author Thomas Segismont
@@ -89,9 +88,17 @@ public void handle(RoutingContext rc) {
8988

9089
private String replacement(RoutingContext rc) {
9190
JsonObject json = new JsonObject();
91+
json.put("httpEnabled", options.isHttpEnabled());
9292
if (options.getGraphQLUri() != null) {
9393
json.put("graphQLUri", options.getGraphQLUri());
9494
}
95+
json.put("graphQLWSEnabled", options.isGraphQLWSEnabled());
96+
if (options.getGraphQLWSUri() != null) {
97+
json.put("graphQLWSUri", options.getGraphQLWSUri());
98+
}
99+
if (options.getWsConnectionParams() != null) {
100+
json.put("wsConnectionParams", options.getWsConnectionParams());
101+
}
95102
MultiMap headers = MultiMap.caseInsensitiveMultiMap();
96103
Map<String, String> fixedHeaders = options.getHeaders();
97104
if (fixedHeaders != null) {
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<!doctype html><html lang="en"><head><meta charset="utf-8"/><title>GraphiQL</title><script>window.VERTX_GRAPHIQL_CONFIG=__VERTX_GRAPHIQL_CONFIG__</script><link href="./static/css/2.7b8c1680.chunk.css" rel="stylesheet"><link href="./static/css/main.8faf0fb8.chunk.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div><script>!function(l){function e(e){for(var r,t,n=e[0],o=e[1],u=e[2],f=0,i=[];f<n.length;f++)t=n[f],p[t]&&i.push(p[t][0]),p[t]=0;for(r in o)Object.prototype.hasOwnProperty.call(o,r)&&(l[r]=o[r]);for(s&&s(e);i.length;)i.shift()();return c.push.apply(c,u||[]),a()}function a(){for(var e,r=0;r<c.length;r++){for(var t=c[r],n=!0,o=1;o<t.length;o++){var u=t[o];0!==p[u]&&(n=!1)}n&&(c.splice(r--,1),e=f(f.s=t[0]))}return e}var t={},p={1:0},c=[];function f(e){if(t[e])return t[e].exports;var r=t[e]={i:e,l:!1,exports:{}};return l[e].call(r.exports,r,r.exports,f),r.l=!0,r.exports}f.m=l,f.c=t,f.d=function(e,r,t){f.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:t})},f.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},f.t=function(r,e){if(1&e&&(r=f(r)),8&e)return r;if(4&e&&"object"==typeof r&&r&&r.__esModule)return r;var t=Object.create(null);if(f.r(t),Object.defineProperty(t,"default",{enumerable:!0,value:r}),2&e&&"string"!=typeof r)for(var n in r)f.d(t,n,function(e){return r[e]}.bind(null,n));return t},f.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return f.d(r,"a",r),r},f.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},f.p="./";var r=window.webpackJsonp=window.webpackJsonp||[],n=r.push.bind(r);r.push=e,r=r.slice();for(var o=0;o<r.length;o++)e(r[o]);var s=n;a()}([])</script><script src="./static/js/2.9f085c6c.chunk.js"></script><script src="./static/js/main.655dbc04.chunk.js"></script></body></html>
1+
<!doctype html><html lang="en"><head><meta charset="utf-8"/><meta name="viewport" content="width=device-width,initial-scale=1,shrink-to-fit=no"/><meta name="theme-color" content="#000000"/><title>GraphiQL</title><script>window.VERTX_GRAPHIQL_CONFIG=__VERTX_GRAPHIQL_CONFIG__</script><script defer="defer" src="./static/js/main.be49c935.js"></script><link href="./static/css/main.87812244.css" rel="stylesheet"></head><body><noscript>You need to enable JavaScript to run this app.</noscript><div id="root"></div></body></html>

vertx-web-graphql/src/main/resources/io/vertx/ext/web/handler/graphiql/static/css/2.7b8c1680.chunk.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

vertx-web-graphql/src/main/resources/io/vertx/ext/web/handler/graphiql/static/css/main.87812244.css

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

vertx-web-graphql/src/main/resources/io/vertx/ext/web/handler/graphiql/static/css/main.8faf0fb8.chunk.css

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)