Update to latest GraphQL over WebSocket protocol
This commit is contained in:
20
samples/webflux-websocket/build.gradle
Normal file
20
samples/webflux-websocket/build.gradle
Normal file
@@ -0,0 +1,20 @@
|
||||
plugins {
|
||||
id 'org.springframework.boot' version '2.4.0-SNAPSHOT'
|
||||
id 'io.spring.dependency-management' version '1.0.10.RELEASE'
|
||||
id 'java'
|
||||
}
|
||||
group = 'com.example'
|
||||
version = '0.0.1-SNAPSHOT'
|
||||
description = "GraphQL over WebSocket sample"
|
||||
sourceCompatibility = '1.8'
|
||||
|
||||
dependencies {
|
||||
implementation project(':spring-graphql-web')
|
||||
implementation 'org.springframework.boot:spring-boot-starter-webflux'
|
||||
implementation 'org.springframework.boot:spring-boot-starter-actuator'
|
||||
developmentOnly 'org.springframework.boot:spring-boot-devtools'
|
||||
testImplementation 'org.springframework.boot:spring-boot-starter-test'
|
||||
}
|
||||
test {
|
||||
useJUnitPlatform()
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
/*
|
||||
* Copyright 2002-2020 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package io.spring.sample.graphql;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class SampleApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SampleApplication.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
package io.spring.sample.graphql;
|
||||
|
||||
import java.time.Duration;
|
||||
|
||||
import graphql.schema.idl.RuntimeWiring;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
import org.springframework.boot.graphql.RuntimeWiringCustomizer;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SampleWiring implements RuntimeWiringCustomizer {
|
||||
|
||||
@Override
|
||||
public void customize(RuntimeWiring.Builder builder) {
|
||||
builder.type("Query", wiringBuilder -> wiringBuilder.dataFetcher("hello",
|
||||
env -> "Hello world!"));
|
||||
builder.type("Subscription", wiringBuilder -> wiringBuilder.dataFetcher("greetings",
|
||||
env -> Flux.just("Hi", "Bonjour", "Hola", "Cio", "Zdravo")
|
||||
.delayElements(Duration.ofMillis(500))));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,5 @@
|
||||
management.endpoints.web.exposure.include=health,metrics,info
|
||||
logging.level.org.springframework.web=debug
|
||||
logging.level.org.springframework.http=debug
|
||||
logging.level.org.springframework.graphql=debug
|
||||
logging.level.reactor.netty=debug
|
||||
11
samples/webflux-websocket/src/main/resources/schema.graphqls
Normal file
11
samples/webflux-websocket/src/main/resources/schema.graphqls
Normal file
@@ -0,0 +1,11 @@
|
||||
schema {
|
||||
query: Query
|
||||
subscription : Subscription
|
||||
}
|
||||
|
||||
type Query {
|
||||
hello: String
|
||||
}
|
||||
type Subscription {
|
||||
greetings: String
|
||||
}
|
||||
@@ -0,0 +1,55 @@
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8" />
|
||||
<title>GraphQL over WebSocket</title>
|
||||
<script type="text/javascript" src="https://unpkg.com/graphql-ws/umd/graphql-ws.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<script type="text/javascript">
|
||||
const client = graphqlWs.createClient({
|
||||
url: 'ws://localhost:8080/graphql/websocket',
|
||||
});
|
||||
|
||||
// query
|
||||
(async () => {
|
||||
const result = await new Promise((resolve, reject) => {
|
||||
let result;
|
||||
client.subscribe(
|
||||
{
|
||||
query: '{ hello }',
|
||||
},
|
||||
{
|
||||
next: (data) => (result = data),
|
||||
error: reject,
|
||||
complete: () => resolve(result),
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
console.log("Query result: " + result);
|
||||
})();
|
||||
|
||||
// subscription
|
||||
(async () => {
|
||||
const onNext = () => {
|
||||
console.log("Subscription data")
|
||||
};
|
||||
|
||||
await new Promise((resolve, reject) => {
|
||||
client.subscribe(
|
||||
{
|
||||
query: 'subscription { greetings }',
|
||||
},
|
||||
{
|
||||
next: onNext,
|
||||
error: reject,
|
||||
complete: resolve,
|
||||
},
|
||||
);
|
||||
});
|
||||
})();
|
||||
|
||||
</script>
|
||||
</body>
|
||||
</html>
|
||||
Reference in New Issue
Block a user