|
| 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)) |
0 commit comments