Skip to content

Commit b02bc91

Browse files
aarestudariuszkuc
authored andcommitted
include Playground IDE in graphql-kotlin-spring-server (#377)
Include [Prisma Labs Playground IDE](https://github.com/prisma-labs/graphql-playground) in `graphql-kotlin-spring-server` auto-configuration library. By default, playground will be enabled under `/playground` route. You can disable it by specifying `graphql.playground.enabled=false` property. Similarly default endpoint can be changed by setting new value to the `graphql.playground.endpoint` property.
1 parent 51b9cab commit b02bc91

File tree

8 files changed

+86
-119
lines changed

8 files changed

+86
-119
lines changed

examples/federation/extend-app/src/main/resources/graphql-playground.html

Lines changed: 0 additions & 58 deletions
This file was deleted.

examples/spring/src/main/resources/application.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@ graphql:
22
subscriptions:
33
# Send a ka message every 1000 ms (1 second)
44
keepAliveInterval: 1000
5+
6+
playground:
7+
enabled: true
8+
endpoint: "/"

examples/spring/src/main/resources/graphql-playground.html

Lines changed: 0 additions & 59 deletions
This file was deleted.

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/GraphQLAutoConfiguration.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ import java.util.Optional
4747
RoutesConfiguration::class,
4848
SchemaAutoConfiguration::class,
4949
FederationAutoConfiguration::class,
50-
SubscriptionAutoConfiguration::class
50+
SubscriptionAutoConfiguration::class,
51+
PlaygroundAutoConfiguration::class
5152
)
5253
@EnableConfigurationProperties(GraphQLConfigurationProperties::class)
5354
class GraphQLAutoConfiguration {

graphql-kotlin-spring-server/src/main/kotlin/com/expediagroup/graphql/spring/GraphQLConfigurationProperties.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class GraphQLConfigurationProperties {
2929
var packages: List<String> = emptyList()
3030
var federation: FederationConfigurationProperties = FederationConfigurationProperties()
3131
var subscriptions: SubscriptionConfigurationProperties = SubscriptionConfigurationProperties()
32+
var playground: PlaygroundConfigurationProperties = PlaygroundConfigurationProperties()
3233
}
3334

3435
/**
@@ -48,3 +49,13 @@ class SubscriptionConfigurationProperties {
4849
/** Keep the websocket alive and send a message to the client every interval in ms. Default to not sending messages */
4950
var keepAliveInterval: Long? = null
5051
}
52+
53+
/**
54+
* Playground configuration properties.
55+
*/
56+
class PlaygroundConfigurationProperties {
57+
/** Boolean flag indicating whether to enabled Prisma Labs Playground GraphQL IDE */
58+
var enabled: Boolean = true
59+
/** Prisma Labs Playground GraphQL IDE endpoint, defaults to "/playground" */
60+
var endpoint: String = "/playground"
61+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2019 Expedia, Inc
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.expediagroup.graphql.spring
18+
19+
import kotlinx.coroutines.ExperimentalCoroutinesApi
20+
import org.springframework.beans.factory.annotation.Value
21+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
22+
import org.springframework.context.annotation.Bean
23+
import org.springframework.context.annotation.Configuration
24+
import org.springframework.core.io.Resource
25+
import org.springframework.web.reactive.function.server.RouterFunction
26+
import org.springframework.web.reactive.function.server.ServerResponse
27+
import org.springframework.web.reactive.function.server.coRouter
28+
import org.springframework.web.reactive.function.server.html
29+
import org.springframework.web.reactive.function.server.bodyAndAwait
30+
31+
/**
32+
* SpringBoot auto configuration for generating Playground Service.
33+
*/
34+
@ConditionalOnProperty(value = ["graphql.playground.enabled"], havingValue = "true", matchIfMissing = true)
35+
@Configuration
36+
class PlaygroundAutoConfiguration(
37+
private val config: GraphQLConfigurationProperties,
38+
@Value("classpath:/graphql-playground.html") private val playgroundHtml: Resource
39+
) {
40+
41+
@Bean
42+
@ExperimentalCoroutinesApi
43+
fun playgroundRoute(): RouterFunction<ServerResponse> {
44+
val body = playgroundHtml.inputStream.bufferedReader().use { reader ->
45+
reader.readText()
46+
.replace("\${graphQLEndpoint}", config.endpoint)
47+
.replace("\${subscriptionsEndpoint}", config.subscriptions.endpoint)
48+
}
49+
return coRouter {
50+
GET(config.playground.endpoint) {
51+
ok().html().bodyAndAwait(body)
52+
}
53+
}
54+
}
55+
}

graphql-kotlin-spring-server/src/main/resources/META-INF/spring-configuration-metadata.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,18 @@
2727
"type": "java.lang.Long",
2828
"defaultValue": "null",
2929
"sourceType": "com.expediagroup.graphql.spring.SubscriptionConfigurationProperties"
30+
},
31+
{
32+
"name": "graphql.playground.enabled",
33+
"type": "java.lang.Boolean",
34+
"sourceType": "com.expediagroup.graphql.spring.PlaygroundConfigurationProperties",
35+
"defaultValue": true
36+
},
37+
{
38+
"name": "graphql.playground.endpoint",
39+
"type": "java.lang.String",
40+
"sourceType": "com.expediagroup.graphql.spring.PlaygroundConfigurationProperties",
41+
"defaultValue": "/playground"
3042
}
3143
]
3244
}

examples/federation/base-app/src/main/resources/graphql-playground.html renamed to graphql-kotlin-spring-server/src/main/resources/graphql-playground.html

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@
5050
</div>
5151
<script>window.addEventListener('load', function (event) {
5252
GraphQLPlayground.init(document.getElementById('root'), {
53-
endpoint: '/graphql'
53+
endpoint: '/${graphQLEndpoint}',
54+
subscriptionEndpoint: '/${subscriptionsEndpoint}'
5455
})
5556
})</script>
5657
</body>

0 commit comments

Comments
 (0)