-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhello.py
executable file
·53 lines (41 loc) · 1.07 KB
/
hello.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
46
47
48
49
50
51
52
53
import asyncio
import typing
import sys
import cannula
SCHEMA = """
type Query {
hello(who: String!): String
}
"""
# Basic API setup with the schema we defined
api = cannula.CannulaAPI(schema=SCHEMA)
# The query resolver takes a `source` and `info` objects
# and any arguments defined by the schema. Here we
# only accept a single argument `who`.
@api.query()
async def hello(
source: typing.Any,
info: cannula.ResolveInfo,
who: str,
) -> str:
# Here the field_name is 'hello' so we'll
# return 'hello {who}!'
return f"{info.field_name} {who}!"
# Pre-parse your query to speed up your requests.
SAMPLE_QUERY = cannula.gql(
"""
query HelloWorld ($who: String!) {
hello(who: $who)
}
"""
)
async def run_hello(who: str = "world"):
results = await api.call(SAMPLE_QUERY, variables={"who": who})
print(results.data)
return results.data
if __name__ == "__main__":
who = "world"
if len(sys.argv) > 1:
who = sys.argv[1]
loop = asyncio.get_event_loop()
loop.run_until_complete(run_hello(who))