#!/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'