By using AWS re:Post, you agree to the Terms of Use

How do I set up an AWS Amplify project to query an existing AWS AppSync API?

1

Hi,

I am new to AWS Amplify and would like guidance on how to send a query to an existing GraphQL API on AWS AppSync. I am unsure how to start as a lot of Amplify coverage creates a new AppSync API using the Amplify CLI.

Objectives

  • Set up a Node.js project to work with an existing AWS AppSync API, using AWS Amplify as the GraphQL client.
  • Send a single query to an existing AWS AppSync API. The query lists game results from a DynamoDB table and is called listGames in my GraphQL schema.
  • I need to repeat the query in order to fetch all available database records that satisfy the query. This would mean adding results to an array/object until the nextToken is null (i.e. no more records can be found for the query).

Context

  • This application is deployed in an Amazon ECS container using AWS Fargate.
  • The ECS service is fronted by an Application Load Balancer (ALB).
  • A leader board web page fetches game results through a POST request to the ALB's DNS name / URL and adds them to a HTML table.

Notes

  • For now, API key is my authentication method. I would soon like to switch to a task IAM role in ECS.
  • The ECS deployment described in 'Context' is working but it sends POST requests without AWS libraries. It is my understanding that I would need to use an AWS library in order to use an IAM role for AppSync authentication (used as a task IAM role in ECS). Please correct me if I am mistaken.

I would greatly appreciate any help you can give me. Thank you for your time!

1 Answer
0

Once you have your AppSync API defined you should see on the overview page (when you click on the name of the API) the command to add the API to an Amplify app:

amplify add codegen --apiId <YOUR_API_ID>

Please note that AppSync is a managed service that will remove the need for you to host your GraphQL on ECS or anything like that. The mapping of the GraphQL queries to the resolvers (such as the ones for DynamoDB) will do the fetching of the records for you. See for example the template mapping for DynamoDB resolvers here.

answered 5 months ago
  • Hi, there. Thank you for your reply.

    1. Great - I will look into the add codegen command.
    2. The reason I created a proxy to call AppSync is because my DynamoDB table has more than 1,000 items which cannot all be retrieved in a single scan operation [1]. As such, the proxy gets results until nextToken becomes null and then returns the data to the client.

    Clarifications:

    • My GraphQL API remains on AppSync, but I plan to turn it into an Amplify project so it can be updated in code outside of the AppSync console.
    • The client (a web page) sends an AppSync query to the proxy (running on AWS Fargate). The proxy then forwards the request to the API on AppSync, builds up the scan results and returns the data to the client.

    I believe I still require the proxy I have described. Please inform me if there is something I am missing from my understanding.

    References:

    [1] DynamoDB Pagination with AppSync: https://advancedweb.hu/how-to-use-dynamodb-with-appsync/#pagination

  • @Toby, your reference link explains how to use the pagination without any proxy. The pagination is done on the client-side. In most cases, you can't display more than a few results (10, 20, 50, 100...) on the client anyway, and you don't want to wait for the end of fetching thousands of items from the server. You can implement the logic of limit or nextToken on the client app using any graphQL client or Amplify in this case.

  • You're right about calling everything on the client. I overthought that aspect.

    However, given my data is for a leader board, I do not see how I can display the N highest scores without considering all items.

  • GraphQL and AppSync support sortDirection: DESC on a query. For example, https://docs.amplify.aws/guides/api-graphql/query-with-sorting/q/platform/js/

  • Hi, that's a great suggestion. Unfortunately, I'm going to struggle to make a GSI in DynamoDB because the field I want to sort by is nested like so: Teams --> Team --> FinalScore. Each database record contains the results of each team in a class at school.

    I believe I would have to create and maintain a second Dynamo table where each record is for one team's scores. (They can be linked back to their class record in the original table using the same partition key and sort key.) FinalScore could then have a GSI made for it (i.e. the field wouldn't be nested inside anything).

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions