Skip to content

Commit b50fa4a

Browse files
committed
added streaming example
1 parent 61b5a22 commit b50fa4a

File tree

3 files changed

+35
-41
lines changed

3 files changed

+35
-41
lines changed

examples/graphql/client_graphql.py

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
import logging
33
import sys
44

5-
import aiohttp
65
from gql import gql, Client
76

8-
from rsocket.extensions.helpers import route, composite
97
from rsocket.extensions.mimetypes import WellKnownMimeTypes
108
from rsocket.graphql.rsocket_transport import RSocketTransport
119
from rsocket.helpers import single_transport_provider
12-
from rsocket.payload import Payload
1310
from rsocket.rsocket_client import RSocketClient
14-
from rsocket.transports.aiohttp_websocket import TransportAioHttpClient
1511
from rsocket.transports.tcp import TransportTCP
1612

1713

@@ -33,48 +29,22 @@ async def main(server_port: int):
3329
transport=RSocketTransport(client),
3430
)
3531

36-
# response = await client.request_response(Payload(metadata=composite(route('ping'))))
37-
#
38-
# assert response.data == b'pong'
39-
4032
await greeting(graphql)
4133

42-
# await echo(graphql)
43-
44-
# await subscription(graphql)
34+
await subscription(graphql)
4535

4636

4737
async def subscription(graphql: Client):
4838
async for response in graphql.subscribe_async(
4939
document=gql("""
50-
subscription multipleGreetings {
51-
multipleGreetings
40+
subscription {
41+
greetings {message}
5242
}
5343
"""),
5444
get_execution_result=True):
5545
print(response.data)
5646

5747

58-
async def echo(graphql: Client):
59-
message = 'and now for something completely different'
60-
61-
response = await graphql.execute_async(
62-
document=gql("""
63-
query echo($input: String) {
64-
echo(input: $input) {
65-
message
66-
}
67-
}
68-
"""),
69-
variable_values={'input': message},
70-
get_execution_result=True
71-
)
72-
73-
assert response.data['echo']['message'] == message
74-
75-
print(response.data)
76-
77-
7848
async def greeting(graphql: Client):
7949
response = await graphql.execute_async(
8050
gql("""query { greeting { message } }"""),

examples/graphql/java/src/main/java/com/example/demo/DemoApplication.java

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,26 +3,46 @@
33
import org.springframework.boot.SpringApplication;
44
import org.springframework.boot.autoconfigure.SpringBootApplication;
55
import org.springframework.graphql.data.method.annotation.QueryMapping;
6+
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
67
import org.springframework.stereotype.Controller;
8+
import reactor.core.publisher.Flux;
9+
10+
import java.time.Duration;
11+
import java.time.Instant;
12+
import java.util.function.Supplier;
13+
import java.util.stream.Stream;
714

815
@SpringBootApplication
916
public class DemoApplication {
1017

11-
public static void main(String[] args) {
12-
SpringApplication.run(DemoApplication.class, args);
13-
}
18+
public static void main(String[] args) {
19+
SpringApplication.run(DemoApplication.class, args);
20+
}
1421

1522
}
1623

1724

1825
@Controller
1926
class GreetingsController {
20-
@QueryMapping
21-
Greeting greeting() {
22-
return new Greeting("Hello world");
23-
}
27+
@SubscriptionMapping
28+
Flux<Greeting> greetings() {
29+
return Flux.fromStream(Stream.generate(
30+
new Supplier<Greeting>() {
31+
@Override
32+
public Greeting get() {
33+
return new Greeting("Hello world @" + Instant.now() + "!");
34+
}
35+
}
36+
)).delayElements(Duration.ofSeconds(1))
37+
.take(10);
38+
}
39+
40+
@QueryMapping
41+
Greeting greeting() {
42+
return new Greeting("Hello world");
43+
}
2444
}
2545

26-
record Greeting (String message) {
46+
record Greeting(String message) {
2747

2848
}

examples/graphql/java/src/main/resources/graphql/rsocket.graphqls

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ type Query {
22
greeting: Greeting
33
}
44

5+
type Subscription {
6+
greetings: Greeting
7+
}
8+
59
type Greeting {
610
message: String
711
}

0 commit comments

Comments
 (0)