Last active
August 13, 2023 23:32
-
-
Save gonzaloserrano/e78ec54a304978eab25e447bd55d63e9 to your computer and use it in GitHub Desktop.
Execute graphQL query or mutation with cURL escaping JSON characters using jq
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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