AppSync console Query tips

TL;DR - Use query variables, arguments and aliases with GraphQL

Challenge and Solution

You have created your AppSync API and you are playing with it by executing Queries in the AppSync console.
emoji-alien You might find that you:

  • are executing the same query/mutation time after time. Each time changing the arguments.
  • are doing a lot of copying and pasting. Creating a resources, getting the id back, copying it, pasting it in all the other queries.
  • are in need of using arguments for your queries that you can fill in with you own variables.

You're in luck. All challenges can be solved. emoji-relaxed

Here it is in one glance:


Now we are:

  • using arguments and variables to make our live easy and only specify the postId once.
    Query variables:

    { "postId": "27ae1dd1-c861-4814-91b9-10df7db94682" }

    Query:

    mutation DeletePost($postId: ID!) { deletePost(input: {id: $postId}) { id } }
  • using aliases so that we can execute the same mutation multiple times in the same query.

    mutation CreateCommentForPost($postId: ID!) { create1: createComment(input: {postID: $postId, content: "This is a short comment"}) { content } create2: createComment(input: {postID: $postId, content: "This is a second comment"}) { content } create3: createComment(input: {postID: $postId, content: "This is the last comment"}) { content } }

Hooray! One step closer to a DRY live! emoji-sunglasses

Credits

Author: https://twitter.com/TheNickVanHoof
Featured image by Sam Dan Truong on Unsplash.
I used the Amplify CLI One-to-many relationship (e.g., “Blogs” with “Posts” and “Comments”) example for this demo.