universal GraphQL query.sh script

master
Alexander Tomokhov 2024-01-09 01:31:49 +04:00
parent 7f4983cf42
commit c8f585b5d8
1 changed files with 30 additions and 0 deletions

30
query.sh Executable file
View File

@ -0,0 +1,30 @@
#! /usr/bin/env bash
set -o errtrace
set -o nounset
set -o pipefail
shopt -s inherit_errexit
trap 'echo $LINENO: "$BASH_COMMAND"; exit 1' ERR
: "${DOMAIN:?specify domain name}"
readonly AUTH_HEADER="Authorization: Bearer ${API_TOKEN}"
QUERY="$1"
REQUEST=$(cat << EOF
{
"query": "$(echo $QUERY)",
"variables": null
}
EOF
)
readonly REQUEST
if RESPONSE="$(curl --fail-with-body --show-error -s "https://api.$DOMAIN/graphql" \
--compressed \
-H 'Content-Type: application/json' \
--data-raw "$REQUEST" -k -H "$AUTH_HEADER")"
then
jq --sort-keys <<<"$RESPONSE"
else
echo "$RESPONSE"
fi