Skip to content

Commit ca476c6

Browse files
committed
graphql support non async resolvers
1 parent 3018830 commit ca476c6

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

examples/graphql/client_graphql.py

+15
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,28 @@ async def main(server_port: int):
2525
transport=RSocketTransport(client),
2626
)
2727

28+
await query_schema(graphql)
29+
2830
await greeting(graphql)
2931
await subscription(graphql)
3032

3133
await set_message(graphql, "updated message")
3234
await verify_message(graphql, "updated message")
3335

3436

37+
async def query_schema(graphql: Client):
38+
response = await graphql.execute_async(
39+
gql("""{
40+
__schema {
41+
types {
42+
name
43+
}
44+
}
45+
}"""),
46+
get_execution_result=True)
47+
print(response.data)
48+
49+
3550
async def subscription(graphql: Client):
3651
async for response in graphql.subscribe_async(
3752
document=gql("""

rsocket/graphql/server_helper.py

+12-11
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55

66
from graphql import execute, parse, GraphQLSchema, ExecutionResult
77
from graphql import subscribe
8+
from graphql.pyutils import is_awaitable
89

910
from rsocket.frame_helpers import str_to_bytes
1011
from rsocket.helpers import create_future
@@ -25,12 +26,11 @@ def graphql_handler(schema: GraphQLSchema, route: str,
2526
async def graphql_query(payload: Payload):
2627
document, params = parse_payload(payload, json_deserialize)
2728

28-
execution_result = await execute(
29-
schema,
30-
document,
31-
variable_values=params.variables,
32-
operation_name=params.operation_name
33-
)
29+
execution_result = execute(schema, document, variable_values=params.variables,
30+
operation_name=params.operation_name)
31+
32+
if is_awaitable(execution_result):
33+
execution_result = await execution_result
3434

3535
rsocket_payload = graphql_to_rsocket_payload(execution_result, json_serialize)
3636

@@ -41,11 +41,12 @@ async def graphql_subscription(payload: Payload):
4141
async def generator() -> AsyncGenerator[Tuple[Payload, bool], None]:
4242
document, params = parse_payload(payload, json_deserialize)
4343

44-
async for execution_result in await subscribe(
45-
schema,
46-
document,
47-
operation_name=params.operation_name
48-
):
44+
subscription = subscribe(schema, document, operation_name=params.operation_name)
45+
46+
if is_awaitable(subscription):
47+
subscription = await subscription
48+
49+
async for execution_result in subscription:
4950
rsocket_payload = graphql_to_rsocket_payload(execution_result, json_serialize)
5051
yield rsocket_payload, False
5152

0 commit comments

Comments
 (0)