8

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

John Mee
  • 50,179
  • 34
  • 152
  • 186

5 Answers5

8

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!

dbala
  • 146
  • 1
  • 2
  • 1
    Rather than forking curl, python could use `requests`, unless you know of a python graphql client that will do this with similar ease? – John Mee Feb 06 '20 at 00:53
  • Thx. Just making sure I hadn't overlooked the command. You confirmed I haven't and thus I can now proceed to loudly complain about it :-) – John Mee Feb 06 '20 at 00:55
  • No worries John. Complain away to the AppSync team as a feature request. What I think we would be further interested in know from you is 'What is the problem you are trying to solve that you believe an Appsync cli command for querying would fix? Is the problem with the graphiql page we host on the aws console? What exactly are your painpoints?' Would absolutely love to hear them if you could DM me or send and email to baladavi@amazon.com. Also, absolutely you can use the 'requests' library, it's just my style to do it the dumb way. First test with cli only, then scriptify the cli command:) – dbala Feb 06 '20 at 02:09
  • 2
    Three pains: 1) TESTING: if I can script it I can write tests to run against endpoints to check that everything is up and working as expected 2) DEV: when the browser times out it usually forgets the query I was working on; remembering to periodically saving my query to a scratch file is painful; forgetting is worse 3) DEV: I generally prefer to develop on files in an IDE, rather than a browser text input, which I do, but the dev cycle is (hack backend in IDE -> deploy -> hack query in browser)(repeat). It's a slow and sucky cycle. – John Mee Feb 06 '20 at 03:21
  • Too simplistic for my purposes; i need to get through Cognito. Got any guides? Is there nothing in boto which will v4 sign a requests object, or calculate the credentials, for us? – John Mee Feb 14 '20 at 05:48
  • 1
    Hey John. If you need to get through Cognito then you can still make an HTTP call like this but instead of an x-api-key you will pass an "Authorization: " When Auth is done with Cognito (I'm simplifying here a lot but to explain), Cognito returns 3 tokens, an AccessToken, an IDToken, and a RefreshToken. Each request to the AppSync API which sets auth to be Cognito requires the access token passed in the http headers. For development, if you need an access token to use, look into 'admin initiate auth' from Cognito docs. It will give you an admin token for dev use – dbala Feb 20 '20 at 00:19
  • Thanks, I reframed the question as python over here, and finally made some progress: https://stackoverflow.com/questions/60293311 – John Mee Feb 20 '20 at 01:01
  • Works! However, update for 2022, I had to remove the -H "Host: " – todbott Jun 26 '22 at 11:38
1

You can do it with Curl:

$ curl -H 'x-api-key: <API KEY>' -d '{"query":"query {...}"}' <API URL>
Neil McGuigan
  • 46,580
  • 12
  • 123
  • 152
1

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
leszek.hanusz
  • 5,152
  • 2
  • 38
  • 56
0

I do not know from the CLI but you can use Curl from windows/linux console.

It is described in AWS at API_KEY Authorization as:

curl -XPOST -H "Content-Type:application/graphql" -H "x-api-key:ABC123" -d '{ "query": "query { movies { id } }" }' https://YOURAPPSYNCENDPOINT/graphql

Just replace the 3 fields with the ones that you can find in your AWS account at the APPSYNC SERVICE:

  1. API_KEY
  2. APP END POINT
  3. Query

If you use windows, remember that it doesn't like single quotation marks, so try using just " and \" instead of ' for the query, for example:

  1. Ubuntu console:

     curl -XPOST -H "Content-Type:application/graphql" -H "x-api-key:**YOUR_API_KEY**" -d **'{ "query":"query { listTodos { items { title } } }" }'** https://**YOUR_END_POINT**.amazonaws.com/graphql
    
  2. Windows console (check query quotes):

     curl -XPOST -H "Content-Type:application/graphql" -H "x-api-key:**YOUR_API_KEY**" -d **"{ \\"query\\":\\"query { listTodos { items { title } } }\\" }"** https://**YOUR_END_POINT**.amazonaws.com/graphql
    
Dharman
  • 30,962
  • 25
  • 85
  • 135
J. Pablo García
  • 499
  • 7
  • 19
0

If you need to use a Bearer token:

# gql.sh 
TOKEN=$1

API_URL='insert api url from AWS console here'
QUERY='your query here'
VARIABLES='{"id":"5"}' 

echo "vars:"
echo "TOKEN $TOKEN"
echo
echo "QUERY $QUERY"
echo
echo "VARIABLES $VARIABLES"
echo
echo "API_URL $API_URL"
echo

curl $API_URL \
-s -X POST \
-H "Authorization: Bearer $TOKEN" \
-H 'content-type: application/json' \
-d '{"query": "'"$QUERY"'", "variables": '$VARIABLES'}'

call with gql.sh [insert token here]

jcollum
  • 43,623
  • 55
  • 191
  • 321