-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathvariables.py
71 lines (61 loc) Β· 2.53 KB
/
variables.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
"""
Query variables demo application.
This is intended for newcomers to Python and/or GraphQL to see in one place
what the minimum components are to do a request and print the response, using
variables. This is just meant as a demo though, as it is too simple to be
resuable or robust.
See also the basic demo script in the same directory.
This script makes use of variables which are sent in the JSON payload. The
variables are already setup so no arguments are needed for this script. In
the GQL explorer, the variables data would go in the Query Variables pane.
Query variables
https://stackoverflow.com/questions/48693825/making-a-graphql-mutation-from-my-python-code-getting-error
According to that SO post, query variables must be sent in on the JSON
payload with the 'variables' key.
request.post(..., json={'query': query, 'variables': variables}, ...)
"""
import json
import config
import requests
# Simple query with parametized repo owner and name values, to fetch the last
# 3 commits on the default branch.
payload = {
"query": """
query BasicQueryTest($owner: String!) {
repository(owner: $owner, name: "aggre-git") {
defaultBranchRef {
target {
... on Commit {
history(first: 3) {
edges {
node {
abbreviatedOid
committedDate
pushedDate
message
additions
changedFiles
deletions
committer {
user {
login
}
}
}
}
}
}
}
}
}
}
""",
"variables": {"owner": "michaelcurrin", "name": "aggre-git"},
}
# Request headers - GitHub auth token is needed.
headers = {"Authorization": f"token {config.ACCESS_TOKEN}"}
# Send the POST request.
resp = requests.post(config.BASE_URL, json=payload, headers=headers)
# Pretty print the output.
prettified = json.dumps(resp.json(), indent=4)
print(prettified)