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

Commit b302e33

Browse files
feat(#212): ability to set custom static folder for Playground resources
1 parent 79b4f5d commit b302e33

File tree

8 files changed

+54
-23
lines changed

8 files changed

+54
-23
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
- [Enable GraphQL Playground](#enable-graphql-playground)
1919
- [Basic settings](#basic-settings)
2020
- [CDN](#cdn)
21+
- [Custom static resources](#custom-static-resources)
2122
- [Customizing GraphQL Playground](#customizing-graphql-playground)
2223
- [Tabs](#tabs)
2324
- [Supported GraphQL-Java Libraries](#supported-graphql-java-libraries)
@@ -273,6 +274,7 @@ graphql.playground:
273274
mapping: /playground
274275
endpoint: /graphql
275276
subscriptionsEndpoint: /subscriptions
277+
staticFolder.basePath: my-playground-resources-folder
276278
enabled: true
277279
pageTitle: Playground
278280
cdn:
@@ -328,6 +330,18 @@ The CDN option uses `jsDelivr` CDN, if enabled. By default, it will load the lat
328330
Available CDN versions can be found on the project's
329331
[jsDelivr page](https://www.jsdelivr.com/package/npm/graphql-playground-react). The CDN option is disabled by default.
330332

333+
## Custom static resources
334+
335+
You can also specify a custom local version of Playground by setting the base path for `Playground` resources in
336+
the `staticPath.base` property. Under this directory, you have to provide the following files:
337+
338+
* `static/css/index.css`
339+
* `static/js/middleware.js`
340+
* `favicon.png`
341+
* `logo.png`
342+
343+
This is identical to the directory structure of the CDN under the `build` subfolder (where these files can be found).
344+
331345
## Customizing GraphQL Playground
332346

333347
Further GraphQL Playground settings can be specified under the `settings` group, which are documented in the official

playground-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/playground/boot/PlaygroundController.java

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,18 @@
77
import org.springframework.web.bind.annotation.GetMapping;
88

99
import javax.servlet.http.HttpServletRequest;
10+
import java.nio.file.Path;
11+
import java.nio.file.Paths;
1012

1113
@Controller
1214
@RequiredArgsConstructor
1315
public class PlaygroundController {
1416

1517
private static final String CDN_ROOT = "https://cdn.jsdelivr.net/npm/graphql-playground-react";
16-
private static final String CDN_CSS = "build/static/css/index.css";
17-
private static final String CDN_FAVICON = "build/favicon.png";
18-
private static final String CDN_SCRIPT = "build/static/js/middleware.js";
19-
private static final String CDN_LOGO = "build/logo.png";
20-
21-
private static final String LOCAL_CSS = "/vendor/playground/playground.css";
22-
private static final String LOCAL_FAVICON = "/vendor/playground/favicon.png";
23-
private static final String LOCAL_SCRIPT = "/vendor/playground/playground.js";
24-
private static final String LOCAL_LOGO = "/vendor/playground/assets/logo.png";
18+
private static final String CSS_PATH = "static/css/index.css";
19+
private static final String FAVICON_PATH = "favicon.png";
20+
private static final String SCRIPT_PATH = "static/js/middleware.js";
21+
private static final String LOGO_PATH = "logo.png";
2522

2623
private static final String CSS_URL_ATTRIBUTE_NAME = "cssUrl";
2724
private static final String FAVICON_URL_ATTRIBUTE_NAME = "faviconUrl";
@@ -46,21 +43,25 @@ public String playground(final Model model, final HttpServletRequest request) {
4643
}
4744

4845
private String getCdnUrl(final String assetUrl) {
49-
return String.format("%s@%s/%s", CDN_ROOT, propertiesConfiguration.getPlayground().getCdn().getVersion(),
46+
return String.format("%s@%s/build/%s", CDN_ROOT, propertiesConfiguration.getPlayground().getCdn().getVersion(),
5047
assetUrl);
5148
}
5249

50+
private Path getLocalUrl(final String cssPath) {
51+
return Paths.get(propertiesConfiguration.getPlayground().getStaticPath().getBase(), cssPath);
52+
}
53+
5354
private void setCdnUrls(final Model model) {
54-
model.addAttribute(CSS_URL_ATTRIBUTE_NAME, getCdnUrl(CDN_CSS));
55-
model.addAttribute(FAVICON_URL_ATTRIBUTE_NAME, getCdnUrl(CDN_FAVICON));
56-
model.addAttribute(SCRIPT_URL_ATTRIBUTE_NAME, getCdnUrl(CDN_SCRIPT));
57-
model.addAttribute(LOGO_URL_ATTRIBUTE_NAME, getCdnUrl(CDN_LOGO));
55+
model.addAttribute(CSS_URL_ATTRIBUTE_NAME, getCdnUrl(CSS_PATH));
56+
model.addAttribute(FAVICON_URL_ATTRIBUTE_NAME, getCdnUrl(FAVICON_PATH));
57+
model.addAttribute(SCRIPT_URL_ATTRIBUTE_NAME, getCdnUrl(SCRIPT_PATH));
58+
model.addAttribute(LOGO_URL_ATTRIBUTE_NAME, getCdnUrl(LOGO_PATH));
5859
}
5960

6061
private void setLocalAssetUrls(final Model model) {
61-
model.addAttribute(CSS_URL_ATTRIBUTE_NAME, LOCAL_CSS);
62-
model.addAttribute(FAVICON_URL_ATTRIBUTE_NAME, LOCAL_FAVICON);
63-
model.addAttribute(SCRIPT_URL_ATTRIBUTE_NAME, LOCAL_SCRIPT);
64-
model.addAttribute(LOGO_URL_ATTRIBUTE_NAME, LOCAL_LOGO);
62+
model.addAttribute(CSS_URL_ATTRIBUTE_NAME, getLocalUrl(CSS_PATH));
63+
model.addAttribute(FAVICON_URL_ATTRIBUTE_NAME, getLocalUrl(FAVICON_PATH));
64+
model.addAttribute(SCRIPT_URL_ATTRIBUTE_NAME, getLocalUrl(SCRIPT_PATH));
65+
model.addAttribute(LOGO_URL_ATTRIBUTE_NAME, getLocalUrl(LOGO_PATH));
6566
}
6667
}

playground-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/playground/boot/properties/PlaygroundCdn.java

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

33
import lombok.Data;
44

5-
import javax.validation.constraints.NotEmpty;
5+
import javax.validation.constraints.NotBlank;
66

77
@Data
88
public class PlaygroundCdn {
99

1010
private boolean enabled;
11-
@NotEmpty
11+
@NotBlank
1212
private String version = "latest";
1313
}

playground-spring-boot-autoconfigure/src/main/java/com/oembedler/moon/playground/boot/properties/PlaygroundProperties.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import lombok.Data;
66
import org.springframework.boot.context.properties.NestedConfigurationProperty;
77

8-
import javax.validation.constraints.NotEmpty;
8+
import javax.validation.constraints.NotBlank;
99
import java.util.Collections;
1010
import java.util.List;
1111
import java.util.Map;
@@ -14,16 +14,20 @@
1414
@JsonInclude(JsonInclude.Include.NON_EMPTY)
1515
public class PlaygroundProperties {
1616

17-
@NotEmpty
17+
@NotBlank
1818
private String endpoint = "/graphql";
1919

20-
@NotEmpty
20+
@NotBlank
2121
private String subscriptionEndpoint = "/subscriptions";
2222

2323
@NestedConfigurationProperty
2424
@JsonIgnore
2525
private PlaygroundCdn cdn = new PlaygroundCdn();
2626

27+
@NestedConfigurationProperty
28+
@JsonIgnore
29+
private PlaygroundStaticPathSettings staticPath = new PlaygroundStaticPathSettings();
30+
2731
@JsonIgnore
2832
private String pageTitle = "Playground";
2933

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.oembedler.moon.playground.boot.properties;
2+
3+
import lombok.Data;
4+
5+
import javax.validation.constraints.NotBlank;
6+
7+
@Data
8+
public class PlaygroundStaticPathSettings {
9+
10+
@NotBlank
11+
private String base = "/vendor/playground";
12+
}

0 commit comments

Comments
 (0)