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

Commit a08544e

Browse files
feat(#212): ability to configure global headers for all Playground tabs
1 parent f103b4e commit a08544e

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

README.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,8 @@ graphql.playground:
294294
schema.polling.interval: 2000
295295
schema.disableComments: true
296296
tracing.hideTracingResponse: true
297+
headers:
298+
headerFor: AllTabs
297299
tabs:
298300
- name: Example Tab
299301
query: classpath:exampleQuery.graphql
@@ -314,6 +316,11 @@ respectively. Note that these values may not be empty.
314316

315317
`pageTitle` defaults to `Playground`.
316318

319+
`headers` allows you to specify headers for the default tab. Note that if your are using Spring Security and CSRF is
320+
enabled CSRF, the CSRF token will be automatically added to the headers. These headers will also be added to all the tabs
321+
configured under the [Tabs](#tabs) section. If a header is defined both in this 'global' header list and the header list
322+
of the individual tabs, the 'local' version will be used for that tab.
323+
317324
## CDN
318325

319326
The currently bundled version is `1.7.20`, which is - as of writing this - the latest release of `GraphQL Playground React`.

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import javax.validation.constraints.NotEmpty;
88
import java.util.Collections;
99
import java.util.List;
10+
import java.util.Map;
1011

1112
@Data
1213
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@@ -26,5 +27,7 @@ public class PlaygroundProperties {
2627

2728
private PlaygroundSettings settings;
2829

30+
private Map<String, String> headers = Collections.emptyMap();
31+
2932
private List<PlaygroundTab> tabs = Collections.emptyList();
3033
}

playground-spring-boot-autoconfigure/src/main/resources/templates/playground.html

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,20 +19,16 @@
1919
</div>
2020

2121
<script th:inline="javascript">
22-
function addCsrfHeaderTo(properties, csrf) {
23-
if (!properties.headers) {
24-
properties.headers = {};
25-
}
26-
properties.headers[csrf.headerName] = csrf.token;
27-
}
28-
2922
let csrf = /*[[${_csrf}]]*/ null;
3023
let properties = /*[[${properties}]]*/ {};
24+
if (!properties.headers) {
25+
properties.headers = {};
26+
}
3127
if (csrf) {
32-
addCsrfHeaderTo(properties, csrf);
33-
if (properties.tabs) {
34-
properties.tabs.forEach(tab => addCsrfHeaderTo(tab, csrf));
35-
}
28+
properties.headers[csrf.headerName] = csrf.token;
29+
}
30+
if (properties.tabs) {
31+
properties.tabs.forEach(tab => tab.headers = Object.assign({}, properties.headers || {}, tab.headers || {}));
3632
}
3733
properties.setTitle = false;
3834
window.addEventListener('load', function (event) {

0 commit comments

Comments
 (0)