Skip to content

Commit 98c70ad

Browse files
authored
Merge pull request #144 from rsocket/graphql
Graphql
2 parents 2baf559 + ecb5658 commit 98c70ad

File tree

21 files changed

+629
-2
lines changed

21 files changed

+629
-2
lines changed

CHANGELOG.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
Changelog
22
---------
33

4+
v0.4.13
5+
=======
6+
- GraphQL basic support (See examples/graphql)
7+
48
v0.4.12
59
=======
610
- Fixed fragmentation for fire and forget

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ You may also install using some **extras**:
2525
| cli | Command line |
2626
| optimized | Frame parse/serialize optimizations |
2727
| cloudevents | CloudEvents integration |
28+
| graphql | GraphQL integration |
2829

2930
```shell
3031
pip install rsocket[reactivex]

docs/conf.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
author = 'jellofishi@pm.me'
2525

2626
# The full version, including alpha/beta/rc tags
27-
release = '0.4.12'
27+
release = '0.4.13'
2828

2929
# -- General configuration ---------------------------------------------------
3030

examples/graphql/__init__.py

Whitespace-only changes.

examples/graphql/client_graphql.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import asyncio
2+
import logging
3+
import sys
4+
from pathlib import Path
5+
6+
from gql import gql, Client
7+
8+
from rsocket.extensions.mimetypes import WellKnownMimeTypes
9+
from rsocket.graphql.rsocket_transport import RSocketTransport
10+
from rsocket.helpers import single_transport_provider
11+
from rsocket.rsocket_client import RSocketClient
12+
from rsocket.transports.tcp import TransportTCP
13+
14+
15+
async def main(server_port: int):
16+
connection = await asyncio.open_connection('localhost', server_port)
17+
18+
async with RSocketClient(single_transport_provider(TransportTCP(*connection)),
19+
metadata_encoding=WellKnownMimeTypes.MESSAGE_RSOCKET_COMPOSITE_METADATA) as client:
20+
with (Path(__file__).parent / 'rsocket.graphqls').open() as fd:
21+
schema = fd.read()
22+
23+
graphql = Client(
24+
schema=schema,
25+
transport=RSocketTransport(client),
26+
)
27+
28+
await greeting(graphql)
29+
30+
await subscription(graphql)
31+
32+
33+
async def subscription(graphql: Client):
34+
async for response in graphql.subscribe_async(
35+
document=gql("""
36+
subscription {
37+
greetings {message}
38+
}
39+
"""),
40+
get_execution_result=True):
41+
print(response.data)
42+
43+
44+
async def greeting(graphql: Client):
45+
response = await graphql.execute_async(
46+
gql("""query { greeting { message } }"""),
47+
get_execution_result=True)
48+
49+
assert response.data['greeting']['message'] == 'Hello world'
50+
51+
print(response.data)
52+
53+
54+
if __name__ == '__main__':
55+
port = sys.argv[1] if len(sys.argv) > 1 else 9191
56+
logging.basicConfig(level=logging.DEBUG)
57+
asyncio.run(main(port))

examples/graphql/java/.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
HELP.md
2+
target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
!**/src/main/**/target/
5+
!**/src/test/**/target/
6+
7+
### STS ###
8+
.apt_generated
9+
.classpath
10+
.factorypath
11+
.project
12+
.settings
13+
.springBeans
14+
.sts4-cache
15+
16+
### IntelliJ IDEA ###
17+
.idea
18+
*.iws
19+
*.iml
20+
*.ipr
21+
22+
### NetBeans ###
23+
/nbproject/private/
24+
/nbbuild/
25+
/dist/
26+
/nbdist/
27+
/.nb-gradle/
28+
build/
29+
!**/src/main/**/build/
30+
!**/src/test/**/build/
31+
32+
### VS Code ###
33+
.vscode/

examples/graphql/java/pom.xml

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>3.1.4</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>com.example</groupId>
12+
<artifactId>demo</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>demo</name>
15+
<description>Demo project for Spring Boot</description>
16+
<properties>
17+
<java.version>17</java.version>
18+
</properties>
19+
<dependencies>
20+
<dependency>
21+
<groupId>org.springframework.boot</groupId>
22+
<artifactId>spring-boot-starter-graphql</artifactId>
23+
</dependency>
24+
<dependency>
25+
<groupId>org.springframework.boot</groupId>
26+
<artifactId>spring-boot-starter-rsocket</artifactId>
27+
</dependency>
28+
29+
<dependency>
30+
<groupId>org.springframework.boot</groupId>
31+
<artifactId>spring-boot-starter-test</artifactId>
32+
<scope>test</scope>
33+
</dependency>
34+
<dependency>
35+
<groupId>io.projectreactor</groupId>
36+
<artifactId>reactor-test</artifactId>
37+
<scope>test</scope>
38+
</dependency>
39+
<dependency>
40+
<groupId>org.springframework.graphql</groupId>
41+
<artifactId>spring-graphql-test</artifactId>
42+
<scope>test</scope>
43+
</dependency>
44+
</dependencies>
45+
46+
<build>
47+
<plugins>
48+
<plugin>
49+
<groupId>org.springframework.boot</groupId>
50+
<artifactId>spring-boot-maven-plugin</artifactId>
51+
</plugin>
52+
</plugins>
53+
</build>
54+
55+
</project>
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.example.demo;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.graphql.data.method.annotation.QueryMapping;
6+
import org.springframework.graphql.data.method.annotation.SubscriptionMapping;
7+
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;
14+
15+
@SpringBootApplication
16+
public class DemoApplication {
17+
18+
public static void main(String[] args) {
19+
SpringApplication.run(DemoApplication.class, args);
20+
}
21+
22+
}
23+
24+
25+
@Controller
26+
class GreetingsController {
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+
}
44+
}
45+
46+
record Greeting(String message) {
47+
48+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
spring.rsocket.server.port=9191
2+
spring.graphql.rsocket.mapping=graphql
3+
server.port=0
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type Query {
2+
greeting: Greeting
3+
}
4+
5+
type Subscription {
6+
greetings: Greeting
7+
}
8+
9+
type Greeting {
10+
message: String
11+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.example.demo;
2+
3+
import org.junit.jupiter.api.Test;
4+
import org.springframework.boot.test.context.SpringBootTest;
5+
6+
@SpringBootTest
7+
class DemoApplicationTests {
8+
9+
@Test
10+
void contextLoads() {
11+
}
12+
13+
}

examples/graphql/rsocket.graphqls

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
type Query {
2+
greeting: Greeting
3+
}
4+
5+
type Subscription {
6+
greetings: Greeting
7+
}
8+
9+
type Greeting {
10+
message: String
11+
}

examples/graphql/schema.py

Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
from graphql.type.definition import (
2+
GraphQLArgument,
3+
GraphQLField,
4+
GraphQLNonNull,
5+
GraphQLObjectType,
6+
)
7+
from graphql.type.scalars import GraphQLString
8+
from graphql.type.schema import GraphQLSchema
9+
10+
11+
def resolve_raises(*_):
12+
raise Exception("Throws!")
13+
14+
15+
EchoType = GraphQLObjectType(
16+
name="Echo",
17+
fields={
18+
"message": GraphQLField(
19+
type_=GraphQLString,
20+
description="The message echoed back",
21+
)
22+
},
23+
description="The result of an echo request",
24+
)
25+
26+
GreetingType = GraphQLObjectType(
27+
name="Greeting",
28+
fields={
29+
"message": GraphQLField(
30+
type_=GraphQLString,
31+
description="The message echoed back",
32+
)
33+
},
34+
description="The result of an echo request",
35+
)
36+
37+
# Sync schema
38+
QueryRootType = GraphQLObjectType(
39+
name="QueryRoot",
40+
fields={
41+
"thrower": GraphQLField(
42+
GraphQLNonNull(GraphQLString),
43+
resolve=resolve_raises,
44+
),
45+
"request": GraphQLField(
46+
GraphQLNonNull(GraphQLString),
47+
resolve=lambda obj, info, *args: info.context["request"].query.get("q"),
48+
),
49+
"context": GraphQLField(
50+
GraphQLObjectType(
51+
name="context",
52+
fields={
53+
"session": GraphQLField(GraphQLString),
54+
"request": GraphQLField(
55+
GraphQLNonNull(GraphQLString),
56+
resolve=lambda obj, info: info.context["request"],
57+
),
58+
"property": GraphQLField(
59+
GraphQLString, resolve=lambda obj, info: info.context.property
60+
),
61+
},
62+
),
63+
resolve=lambda obj, info: info.context,
64+
),
65+
"test": GraphQLField(
66+
type_=GraphQLString,
67+
args={"who": GraphQLArgument(GraphQLString)},
68+
resolve=lambda obj, info, who=None: "Hello %s" % (who or "World"),
69+
),
70+
},
71+
)
72+
73+
74+
async def resolve_greeting(_obj, info):
75+
return "hello world"
76+
77+
78+
async def resolve_echo(_obj, info, input):
79+
return {'message': input}
80+
81+
82+
async def resolve_multiple_greetings(_obj, info):
83+
for index, greeting in enumerate(['Hello', 'Hi', 'Yo']):
84+
yield {'message': greeting}
85+
86+
87+
AsyncQueryType = GraphQLObjectType(
88+
"AsyncQueryType",
89+
{
90+
"greeting": GraphQLField(
91+
GraphQLString,
92+
resolve=resolve_greeting),
93+
"echo": GraphQLField(
94+
EchoType,
95+
args={"input": GraphQLArgument(GraphQLString)},
96+
resolve=resolve_echo)
97+
}
98+
)
99+
100+
MutationRootType = GraphQLObjectType(
101+
name="MutationRoot",
102+
fields={
103+
"writeTest": GraphQLField(
104+
type_=QueryRootType, resolve=lambda *args: QueryRootType
105+
)
106+
},
107+
)
108+
109+
SubscriptionsRootType = GraphQLObjectType(
110+
name="SubscriptionsRoot",
111+
fields={
112+
"multipleGreetings": GraphQLField(
113+
type_=GreetingType, subscribe=resolve_multiple_greetings
114+
)
115+
}
116+
)
117+
118+
AsyncSchema = GraphQLSchema(AsyncQueryType, subscription=SubscriptionsRootType)

0 commit comments

Comments
 (0)