Skip to content

Instantly share code, notes, and snippets.

@mttjohnson
Created June 7, 2018 05:00
Show Gist options
  • Select an option

  • Save mttjohnson/ca7096da068e770aa0cd4c043e612981 to your computer and use it in GitHub Desktop.

Select an option

Save mttjohnson/ca7096da068e770aa0cd4c043e612981 to your computer and use it in GitHub Desktop.
Making GraphQL Calls With Curl (for Magento 2)
# This example uses a GraphQL query meant for a Magento 2 (2.3 Alpha) endpoint
# Take a formatted GraphQL query and convert it to a JSON encoded string
GRAPHQL_QUERY=$(
echo '
{
products(
search: "Messenger"
filter: {
sku: {like: "24-MB%"}
price: {lt: "50"}
}
pageSize: 5
sort: {
price: DESC
}
)
{
items
{
name
sku
description
price {
regularPrice {
amount {
value
currency
}
}
}
}
total_count
page_info {
page_size
}
}
}
' | jq --slurp --compact-output --raw-input '.'
)
# Build the payload as a JSON document compacted on to a single line
PAYLOAD=$(
echo '
{
"query": ' "${GRAPHQL_QUERY}" ',
"variables": null,
"operationName": null
}
' | jq --compact-output --raw-output '.'
)
# Send the HTTP request with the JSON payload and output the response in a readable form
curl -sk -X POST -H "Content-Type:application/json" \
--data "${PAYLOAD}" https://m23presamp.dev/graphql | jq .
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment