# gqlclient.sh -rw-r--r-- 703 bytes View raw
                                                                                
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
#!/bin/sh -eu

# Example:
#
# exec gqlclient -V name=emersion https://meta.sr.ht/query -H "Authorization: Bearer $token" <<"EOF"
# query ($name: String!) {
# 	userByName(username: $name) {
# 		canonicalName
# 	}
# }
# EOF

vars='{}'
while getopts V: opt; do
	case "$opt" in
	V)
		k="$(echo "$OPTARG" | cut -d'=' -f1)"
		v="$(echo "$OPTARG" | cut -d'=' -f2)"
		vars="$(echo "$vars" | jq '. + {"'"$k"'": $v}' --arg v "$v")"
		;;
	?)
		exit 1
		;;
	esac
done
shift "$(($OPTIND - 1))"

query="$(cat)"
body='{"query": $query, "variables": $vars}'

jq --null-input "$body" --arg query "$query" --argjson vars "$vars" | \
	curl --no-progress-meter -H "Content-Type: application/json" -d @- "$@" | \
	jq '.data'