Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to send a GraphQL query to AWS AppSync from the commandline?

The AWS CLI for AppSync has a lovely array of functions to manage it remotely from the command line of my workstation such that mostly I do not have to use the browser console.

But to do a query I have to go into the web browser console and find GraphQl queries under AppSync. I can change all manner of things via the CLI, but I can't find a command that simply issues a graphql query.

Have I missed it? Is it there?
I don't want to look at this screen anymore...

AWS AppSync Queries

like image 571
John Mee Avatar asked Feb 05 '20 02:02

John Mee


Video Answer


3 Answers

The Appsync queries page is actually a conjunction of several things together. You cannot issue queries from the CLI according to (https://docs.aws.amazon.com/cli/latest/reference/appsync/index.html)

You can however use a GUI Client tool to send a POST to your Appsync endpoint. Like Postman or Insomnia (my personal favorite). However is your goal is to truly send GraphQL compliant queries through the CLI, then you will have to resort to 'curl's

Here is an example python script I have that sends a curl request to my Appsync API.

#!/usr/bin/env python3
import os

cmd = """curl -i -H 'Content-Type: application/json' -H "x-api-key: <ENTER YOUR API KEY FROM THE APPSYNC SETTINGS PAGE>" -H "Host: <ENTER YOUR HOST ENDPOINT FROM THE APPSYNC API SETTINGS PAGE >" -X POST -d '{"query": "query {listEvents {items {id}}}"}' https://<ENTER YOUR HOST ENDPOINT FROM THE APPSYNC API SETTINGS PAGE>/graphql"""

def doGraphqlRequest():    
    os.system(cmd)

print("Starting request to Appsync endpoint")
doGraphQLRequest()
print("Finsihed request to Appsync endpoint")

To explain a bit, you are making a POST request with your query to your appsync given '/graphql/ endpoint. You have 3 headers (Denoted by the -H flag)

  1. The x-api-key: Only applicable if you use API KEY as the auth type. Other auth types work too, you might have a AuthToken: Bearer , and Cognito works too but is significantly more complicated from CLI
  2. The host: This is the name of the ec2 host given by your api. You can find it by looking at your assigned endpoint and deleteing the https:// and /graphql
  3. The Content-Type: application/json. This is kinda standard, not super sure why but it's a must have.

Hope this helps!

like image 150
dbala Avatar answered Oct 17 '22 08:10

dbala


graphql-python/gql supports AWS AppSync since version 3.0.0rc0.

It supports queries, mutation and even subscriptions on the realtime endpoint.

It supports IAM, api key and JWT authentication methods.

And it has a gql-cli script which allows you to execute queries, mutations and subscriptions from the command line.

The documentation is available here

For queries and mutations, use the --transport appsync_http argument:

# Put the request in a file
$ echo 'mutation createMessage($message: String!) {
  createMessage(input: {message: $message}) {
    id
    message
    createdAt
  }
}' > mutation.graphql

# Execute the request using gql-cli with --transport appsync_http
$ cat mutation.graphql | gql-cli $AWS_GRAPHQL_API_ENDPOINT --transport appsync_http -V message:"Hello world!"

For subscriptions, use the --transport appsync_websockets argument:

echo "subscription{onCreateMessage{message}}" | gql-cli $AWS_GRAPHQL_API_ENDPOINT --transport appsync_websockets
like image 25
leszek.hanusz Avatar answered Oct 17 '22 07:10

leszek.hanusz


You can do it with Curl:

$ curl -H 'x-api-key: <API KEY>' -d '{"query":"query {...}"}' <API URL>
like image 1
Neil McGuigan Avatar answered Oct 17 '22 08:10

Neil McGuigan