Created
June 7, 2018 05:00
-
-
Save mttjohnson/ca7096da068e770aa0cd4c043e612981 to your computer and use it in GitHub Desktop.
Making GraphQL Calls With Curl (for Magento 2)
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
| # 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