forked from anvilco/python-anvil
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmake_graphql_request.py
More file actions
147 lines (122 loc) · 3.86 KB
/
make_graphql_request.py
File metadata and controls
147 lines (122 loc) · 3.86 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
import os
from gql.dsl import DSLQuery, dsl_gql
from python_anvil.api import Anvil
from python_anvil.http import get_gql_ds
API_KEY = os.environ.get("ANVIL_API_KEY")
# or set your own key here
# API_KEY = 'my-api-key'
def call_current_user_query(anvil: Anvil) -> dict:
"""Get the user data attached to the current API key.
:param anvil:
:type anvil: Anvil
:return:
"""
# See the reference docs for examples of all queries and mutations:
# https://www.useanvil.com/docs/api/graphql/reference/
# pylint: disable=unused-variable
user_query = """
query CurrentUser {
currentUser {
eid
name
organizations {
eid
slug
name
casts {
eid
name
}
welds {
eid
name
}
}
}
}
"""
# You can also use `gql`'s query builder. Below is the equivalent of the
# string above, but can potentially be a better interface if you're
# building a query in multiple steps. See the official `gql` docs for more
# details: https://gql.readthedocs.io/en/stable/advanced/dsl_module.html
# Use `ds` to create your queries
ds = get_gql_ds(anvil.gql_client)
ds_user_query_builder = ds.Query.currentUser.select(
ds.User.eid,
ds.User.name,
ds.User.organizations.select(
ds.Organization.eid,
ds.Organization.slug,
ds.Organization.name,
ds.Organization.casts.select(
ds.Cast.eid,
ds.Cast.name,
),
ds.Organization.welds.select(
ds.Weld.eid,
ds.Weld.name,
),
),
)
ds_query = dsl_gql(DSLQuery(ds_user_query_builder))
res = anvil.query(query=ds_query, variables=None)
return res["currentUser"]
def call_weld_query(anvil: Anvil, weld_eid: str):
"""Call the weld query.
The weld() query is an example of a query that takes variables.
:param anvil:
:type anvil: Anvil
:param weld_eid:
:type weld_eid: str
:return:
"""
# pylint: disable=unused-variable
weld_query = """
query WeldQuery (
$eid: String,
) {
weld (
eid: $eid,
) {
eid
name
forges {
eid
slug
name
}
}
}
"""
variables = {"eid": weld_eid}
# You can also use `gql`'s query builder. Below is the equivalent of the
# string above, but can potentially be a better interface if you're
# building a query in multiple steps. See the official `gql` docs for more
# details: https://gql.readthedocs.io/en/stable/advanced/dsl_module.html
# Use `ds` to create your queries
ds = get_gql_ds(anvil.gql_client)
ds_weld_query_builder = ds.Query.weld.args(eid=weld_eid).select(
ds.Weld.eid,
ds.Weld.name,
ds.Weld.forges.select(
ds.Forge.eid,
ds.Forge.slug,
ds.Forge.name,
),
)
ds_query = dsl_gql(DSLQuery(ds_weld_query_builder))
# You can call the query with the string literal and variables like usual
# res = anvil.query(query=weld_query, variables=variables)
# Or, use only the `dsl_gql` query. `variables` not needed as it was
# already used in `.args()`.
res = anvil.query(query=ds_query)
return res["weld"]
def call_queries():
anvil = Anvil(api_key=API_KEY)
current_user = call_current_user_query(anvil)
first_weld = current_user["organizations"][0]["welds"][0]
weld_data = call_weld_query(anvil, weld_eid=first_weld["eid"])
print("currentUser: ", current_user)
print("First weld details: ", weld_data)
if __name__ == "__main__":
call_queries()