From 48aa09150dac0a2a0b736f0c74a1052390ed07d5 Mon Sep 17 00:00:00 2001 From: Dmitry <5000487+sadv1r@users.noreply.github.com> Date: Sun, 25 Jun 2023 14:01:19 +0300 Subject: [PATCH] Fixed GraphQL Playground Tabs --- README.md | 21 +++++++--- .../configuration/PlaygroundProperties.java | 23 +++++++++-- ...PlaygroundWebFluxMvcAutoConfiguration.java | 4 +- .../PlaygroundWebMvcAutoConfiguration.java | 6 +-- .../main/resources/templates/playground.html | 3 +- .../src/main/resources/application.yaml | 38 ++++++++++++++++++- .../src/main/resources/application.yaml | 28 +++++++++++++- 7 files changed, 106 insertions(+), 17 deletions(-) diff --git a/README.md b/README.md index 5cd6d5f..d6d757f 100644 --- a/README.md +++ b/README.md @@ -150,7 +150,8 @@ graphql: headers: x-test: test tabs: - - endpoint: http://localhost:8080/graphql + - name: GraphQL + endpoint: http://localhost:8080/graphql query: |- query($id: ID!) { artifactRepository(id: $id) { @@ -158,10 +159,20 @@ graphql: url } } - name: GraphQL - variables: '{"id": 1}' - responses: - - '{"data": null}' + variables: |- + { + "id": 1 + } + responses: + - > + { + "data": { + "artifactRepositories": [{ + "name": "test", + "url": "http://localhost:8080" + }] + } + } headers: x-test2: test2 cdn: unpkg diff --git a/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundProperties.java b/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundProperties.java index b9c1d4c..1bf7cd0 100644 --- a/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundProperties.java +++ b/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundProperties.java @@ -3,7 +3,6 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonUnwrapped; -import com.fasterxml.jackson.databind.annotation.JsonSerialize; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.core.io.ClassPathResource; import org.springframework.util.FileCopyUtils; @@ -382,7 +381,7 @@ public void setTracingSupported(Boolean tracingSupported) { } @JsonInclude(JsonInclude.Include.NON_NULL) - private class Tab { + private static class Tab { private String endpoint; @@ -390,9 +389,9 @@ private class Tab { private String name; -// private Object variables; TODO + private String variables; -// private Object responses; TODO + private List responses; private Map headers; @@ -420,6 +419,22 @@ public void setName(String name) { this.name = name; } + public String getVariables() { + return variables; + } + + public void setVariables(String variables) { + this.variables = variables; + } + + public List getResponses() { + return responses; + } + + public void setResponses(List responses) { + this.responses = responses; + } + public Map getHeaders() { return headers; } diff --git a/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundWebFluxMvcAutoConfiguration.java b/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundWebFluxMvcAutoConfiguration.java index de51c16..fb8597c 100644 --- a/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundWebFluxMvcAutoConfiguration.java +++ b/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundWebFluxMvcAutoConfiguration.java @@ -23,14 +23,14 @@ public RouterFunction reactivePlaygroundRouterFunction(Playgroun @Value("${spring.graphql.path:/graphql}") String serverPath) { RouterFunctions.Builder builder = RouterFunctions.route(); if (properties.isEnabled()) { - final HandlerFunction graphiql = e -> (Mono) (Mono) RenderingResponse.create("playground") + final HandlerFunction handler = e -> (Mono) (Mono) RenderingResponse.create("playground") .modelAttribute("cdnHost", properties.getCdn().getHost()) .modelAttribute("serverPath", serverPath) .modelAttribute("settings", properties.getSettings()) .modelAttribute("headers", properties.getHeaders()) .modelAttribute("tabs", properties.getTabs()) .build(); - builder = builder.GET(properties.getPath(), graphiql); + builder = builder.GET(properties.getPath(), handler); } return builder.build(); } diff --git a/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundWebMvcAutoConfiguration.java b/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundWebMvcAutoConfiguration.java index 49f6000..2308acd 100644 --- a/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundWebMvcAutoConfiguration.java +++ b/graphql-playground-spring-boot-starter/src/main/java/ru/sadv1r/spring/graphql/editor/playground/configuration/PlaygroundWebMvcAutoConfiguration.java @@ -17,18 +17,18 @@ public class PlaygroundWebMvcAutoConfiguration { @Bean @Order(-1) - public RouterFunction graphiQlRouterFunction(PlaygroundProperties properties, + public RouterFunction playgroundRouterFunction(PlaygroundProperties properties, @Value("${spring.graphql.path:/graphql}") String serverPath) { RouterFunctions.Builder builder = RouterFunctions.route(); if (properties.isEnabled()) { - final HandlerFunction graphiql = e -> RenderingResponse.create("playground") + final HandlerFunction handler = e -> RenderingResponse.create("playground") .modelAttribute("cdnHost", properties.getCdn().getHost()) .modelAttribute("serverPath", serverPath) .modelAttribute("settings", properties.getSettings()) .modelAttribute("headers", properties.getHeaders()) .modelAttribute("tabs", properties.getTabs()) .build(); - builder = builder.GET(properties.getPath(), graphiql); + builder = builder.GET(properties.getPath(), handler); } return builder.build(); } diff --git a/graphql-playground-spring-boot-starter/src/main/resources/templates/playground.html b/graphql-playground-spring-boot-starter/src/main/resources/templates/playground.html index f28a7f5..2495676 100644 --- a/graphql-playground-spring-boot-starter/src/main/resources/templates/playground.html +++ b/graphql-playground-spring-boot-starter/src/main/resources/templates/playground.html @@ -51,7 +51,8 @@ GraphQLPlayground.init(document.getElementById('root'), { endpoint: '[(${serverPath})]', settings: /*[[${settings}]]*/, - headers: /*[[${headers}]]*/ + headers: /*[[${headers}]]*/, + tabs: /*[[${tabs}]]*/ }) }) diff --git a/samples/web-sample/src/main/resources/application.yaml b/samples/web-sample/src/main/resources/application.yaml index 0e9bcad..714b41e 100644 --- a/samples/web-sample/src/main/resources/application.yaml +++ b/samples/web-sample/src/main/resources/application.yaml @@ -11,4 +11,40 @@ spring: variables: id: 1 headers: - x-test: test \ No newline at end of file + x-test: test + playground: + settings: + editor: + cursor-shape: line + font-family: Consolas + font-size: 14 + reuse-headers: true + theme: light + headers: + x-test: test + tabs: + - name: Query + endpoint: http://localhost:8080/graphql + query: |- + query($id: ID!) { + artifactRepository(id: $id) { + name + url + } + } + variables: |- + { + "id": 1 + } + responses: + - > + { + "data": { + "artifactRepositories": [{ + "name": "test", + "url": "http://localhost:8080" + }] + } + } + headers: + x-test2: test2 \ No newline at end of file diff --git a/samples/webflux-sample/src/main/resources/application.yaml b/samples/webflux-sample/src/main/resources/application.yaml index 4bb5224..d9bf602 100644 --- a/samples/webflux-sample/src/main/resources/application.yaml +++ b/samples/webflux-sample/src/main/resources/application.yaml @@ -15,4 +15,30 @@ spring: reuse-headers: true theme: light headers: - x-test: test \ No newline at end of file + x-test: test + tabs: + - name: Query + endpoint: http://localhost:8080/graphql + query: |- + query($id: ID!) { + artifactRepository(id: $id) { + name + url + } + } + variables: |- + { + "id": 1 + } + responses: + - > + { + "data": { + "artifactRepositories": [{ + "name": "test", + "url": "http://localhost:8080" + }] + } + } + headers: + x-test2: test2 \ No newline at end of file