Skip to content
This repository was archived by the owner on Dec 19, 2023. It is now read-only.

Commit fd1c312

Browse files
feat(#212): GraphQL Playground starter (with basic settings)
1 parent 75a4210 commit fd1c312

File tree

18 files changed

+316
-1
lines changed

18 files changed

+316
-1
lines changed

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -251,6 +251,32 @@ to set the classpath resources that should be loaded.
251251

252252
Headers that are used when sending the Altair queries can be set by defining them in the `altair.headers` group.
253253

254+
# Enable GraphQL Playground
255+
256+
GraphQL Playground becomes accessible at root `/playground` (or as configured in `playground.mapping`)
257+
if `playground-spring-boot-starter` is added as a dependency to a boot application.
258+
259+
It uses an embedded `GraphQL Playground React`, according to the [official guide](https://github.com/prisma/graphql-playground#as-html-page),
260+
using the 'minimum HTML' approach.
261+
262+
Available Spring Boot configuration parameters (either `application.yml` or `application.properties`):
263+
264+
```yaml
265+
playground:
266+
mapping: /playground
267+
endpoint:
268+
graphql: /graphql
269+
subscriptions: /subscriptions
270+
enabled: true
271+
pageTitle: Playground
272+
cdn:
273+
enabled: false
274+
version: latest
275+
```
276+
The currently bundled version is `1.7.20`, which is - as of writing this - the latest release of `GraphQL Playground React`.
277+
The CDN option uses `jsDelivr` CDN. By default, it will load the latest available release.
278+
Available CDN versions can be found here: https://www.jsdelivr.com/package/npm/graphql-playground-react
279+
254280
# Supported GraphQL-Java Libraries
255281

256282
The following libraries have auto-configuration classes for creating a `GraphQLSchema`.

build.gradle

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,9 @@ release {
187187
'com.graphql-java-kickstart:graphql-spring-boot-test',
188188
'com.graphql-java-kickstart:graphql-spring-boot-test-autoconfigure',
189189
'com.graphql-java-kickstart:voyager-spring-boot-autoconfigure',
190-
'com.graphql-java-kickstart:voyager-spring-boot-starter'
190+
'com.graphql-java-kickstart:voyager-spring-boot-starter',
191+
'com.graphql-java-kickstart:playgroud-spring-boot-autoconfigure',
192+
'com.graphql-java-kickstart:playgroud-spring-boot-starter'
191193
]
192194
}
193195

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 Oembedler Inc. and Contributors
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6+
7+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8+
9+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
* The MIT License (MIT)
3+
*
4+
* Copyright (c) 2016 oEmbedler Inc. and Contributors
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
7+
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
8+
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
9+
* persons to whom the Software is furnished to do so, subject to the following conditions:
10+
*
11+
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12+
*
13+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
14+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
15+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
16+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
17+
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
18+
*/
19+
dependencies{
20+
annotationProcessor "org.springframework.boot:spring-boot-configuration-processor:$LIB_SPRING_BOOT_VER"
21+
22+
compile "org.springframework.boot:spring-boot-autoconfigure:$LIB_SPRING_BOOT_VER"
23+
compile "org.springframework.boot:spring-boot-starter-web:$LIB_SPRING_BOOT_VER"
24+
compile "org.springframework.boot:spring-boot-starter-thymeleaf:$LIB_SPRING_BOOT_VER"
25+
26+
testCompile "org.springframework.boot:spring-boot-starter-web:$LIB_SPRING_BOOT_VER"
27+
testCompile "org.springframework.boot:spring-boot-starter-test:$LIB_SPRING_BOOT_VER"
28+
}
29+
30+
compileJava.dependsOn(processResources)
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.oembedler.moon.playground.boot;
2+
3+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
4+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
5+
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
import org.springframework.web.servlet.DispatcherServlet;
9+
10+
@Configuration
11+
@ConditionalOnWebApplication
12+
@ConditionalOnClass(DispatcherServlet.class)
13+
public class PlaygroundAutoConfiguration {
14+
15+
@Bean
16+
@ConditionalOnProperty(value = "playground.enabled", havingValue = "true", matchIfMissing = true)
17+
PlaygroundController altairController() {
18+
return new PlaygroundController();
19+
}
20+
}
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package com.oembedler.moon.playground.boot;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
8+
@Controller
9+
public class PlaygroundController {
10+
11+
private static final String CDN_ROOT = "https://cdn.jsdelivr.net/npm/graphql-playground-react";
12+
private static final String CDN_CSS = "build/static/css/index.css";
13+
private static final String CDN_FAVICON = "build/favicon.png";
14+
private static final String CDN_SCRIPT = "build/static/js/middleware.js";
15+
private static final String CDN_LOGO = "build/logo.png";
16+
17+
private static final String LOCAL_CSS = "/vendor/playground/playground.css";
18+
private static final String LOCAL_FAVICON = "/vendor/playground/favicon.png";
19+
private static final String LOCAL_SCRIPT = "/vendor/playground/playground.js";
20+
private static final String LOCAL_LOGO = "/vendor/playground/assets/logo.png";
21+
22+
private static final String CSS_URL_ATTRIBUTE_NAME = "cssUrl";
23+
private static final String FAVICON_URL_ATTRIBUTE_NAME = "faviconUrl";
24+
private static final String SCRIPT_URL_ATTRIBUTE_NAME = "scriptUrl";
25+
private static final String LOGO_URL_ATTRIBUTE_NAME = "logoUrl";
26+
27+
@Value("${playground.endpoint.graphql:/graphql}")
28+
private String graphqlEndpoint;
29+
30+
@Value("${playground.endpoint.subscriptions:/subscriptions}")
31+
private String subscriptionsEndpoint;
32+
33+
@Value("${playground.cdn.enabled:false}")
34+
private Boolean cdnEnabled;
35+
36+
@Value("${playground.cdn.version:latest}")
37+
private String cdnVersion;
38+
39+
@Value("${playground.pageTitle:Playground}")
40+
private String pageTitle;
41+
42+
@GetMapping("${playground.mapping:/playground}")
43+
public String playground(final Model model) {
44+
if (cdnEnabled) {
45+
setCdnUrls(model);
46+
} else {
47+
setLocalAssetUrls(model);
48+
}
49+
model.addAttribute("graphqlEndpoint", graphqlEndpoint);
50+
model.addAttribute("subscriptionsEndpoint", subscriptionsEndpoint);
51+
model.addAttribute("pageTitle", pageTitle);
52+
return "playground";
53+
}
54+
55+
private String getCdnUrl(final String assetUrl) {
56+
return String.format("%s@%s/%s", CDN_ROOT, cdnVersion, assetUrl);
57+
}
58+
59+
private void setCdnUrls(final Model model) {
60+
model.addAttribute(CSS_URL_ATTRIBUTE_NAME, getCdnUrl(CDN_CSS));
61+
model.addAttribute(FAVICON_URL_ATTRIBUTE_NAME, getCdnUrl(CDN_FAVICON));
62+
model.addAttribute(SCRIPT_URL_ATTRIBUTE_NAME, getCdnUrl(CDN_SCRIPT));
63+
model.addAttribute(LOGO_URL_ATTRIBUTE_NAME, getCdnUrl(CDN_LOGO));
64+
}
65+
66+
private void setLocalAssetUrls(final Model model) {
67+
model.addAttribute(CSS_URL_ATTRIBUTE_NAME, LOCAL_CSS);
68+
model.addAttribute(FAVICON_URL_ATTRIBUTE_NAME, LOCAL_FAVICON);
69+
model.addAttribute(SCRIPT_URL_ATTRIBUTE_NAME, LOCAL_SCRIPT);
70+
model.addAttribute(LOGO_URL_ATTRIBUTE_NAME, LOCAL_LOGO);
71+
}
72+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
{
2+
"groups": [
3+
{
4+
"name": "playground"
5+
}
6+
],
7+
"properties": [
8+
{
9+
"name": "playground.mapping",
10+
"defaultValue": "/playground",
11+
"type": "java.lang.String"
12+
},
13+
{
14+
"name": "playground.endpoint.graphql",
15+
"defaultValue": "/graphql",
16+
"type": "java.lang.String"
17+
},
18+
{
19+
"name": "playground.endpoint.subscriptions",
20+
"defaultValue": "/subscriptions",
21+
"type": "java.lang.String"
22+
},
23+
{
24+
"name": "playground.cdn.enabled",
25+
"defaultValue": false,
26+
"type": "java.lang.Boolean"
27+
},
28+
{
29+
"name": "playground.cdn.version",
30+
"defaultValue": "latest",
31+
"type": "java.lang.String"
32+
},
33+
{
34+
"name": "playground.pageTitle",
35+
"defaultValue": "Playground",
36+
"type": "java.lang.String"
37+
}
38+
]
39+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
org.springframework.boot.autoconfigure.EnableAutoConfiguration=com.oembedler.moon.playground.boot.PlaygroundAutoConfiguration
31.3 KB
Loading
4.94 KB
Loading

0 commit comments

Comments
 (0)