-
Notifications
You must be signed in to change notification settings - Fork 3
/
test.py
45 lines (32 loc) · 1.08 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
from rath.links.auth import ComposedAuthLink
from rath.links.aiohttp import AIOHttpLink
from rath.links.graphql_ws import GraphQLWSLink
from rath.links import compose, split
from rath import Rath
async def aload_token() -> str:
"""Loads the token from the environment"""
return "SERVER_TOKEN"
auth = ComposedAuthLink(token_loader=aload_token)
link = AIOHttpLink(endpoint_url="https://countries.trevorblades.com/")
ws = GraphQLWSLink(ws_endpoint_url="wss://countries.trevorblades.com/") #
end_link = split(link, ws, lambda op: op.node.operation == "SUBSCRIPTION")
with_auth = compose(auth, end_link)
with Rath(link=with_auth) as rath:
query = """query {
countries {
native
capital
}
}
"""
result = rath.query(query) # uses the http link
print(result)
subscription = """subscription {
newCountry {
native
capital
}
}
"""
for i in rath.subscribe(subscription): # uses the ws link
print(i) # will fail because the server does not support subscriptions