Skip to content

Instantly share code, notes, and snippets.

@gonzaloserrano
Last active August 13, 2023 23:32
Show Gist options
  • Save gonzaloserrano/e78ec54a304978eab25e447bd55d63e9 to your computer and use it in GitHub Desktop.
Save gonzaloserrano/e78ec54a304978eab25e447bd55d63e9 to your computer and use it in GitHub Desktop.
Execute graphQL query or mutation with cURL escaping JSON characters using jq
# usage from another script:
# source /path/to/graphql.sh
# graphql http://localhost:8080/graphql "$SOME_QUERY_OR_MUTATION"
# where $SOME_QUERY_OR_MUTATION is the raw query or mutation, with quotes, new lines etc.
require() {
command -v "$1" >/dev/null 2>&1 || { echo >&2 "$1 program is required"; exit 1; }
}
graphql() {
require curl
require jq
QUERY=$(jq -aRs . <<< "$2")
curl -H "Content-Type: application/json" "$1" -d "{\"query\": ${QUERY}}" | jq .
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment