How do I set up an AWS Amplify project to query an existing AWS AppSync API?
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.
- 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
listGamesin 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
null(i.e. no more records can be found for the query).
- 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
POSTrequest to the ALB's DNS name / URL and adds them to a HTML table.
- 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
POSTrequests 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!
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.
Hi, there. Thank you for your reply.
- Great - I will look into the
- 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
nextTokenbecomes null and then returns the data to the client.
- 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.
1 DynamoDB Pagination with AppSync: https://advancedweb.hu/how-to-use-dynamodb-with-appsync/#pagination
- Great - I will look into the
@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
nextTokenon 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.
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.)
FinalScorecould then have a GSI made for it (i.e. the field wouldn't be nested inside anything).
How to use Amplify Datastore to sync with data from DynamoDB and seed DynamoDB from a Lambaasked 4 months ago
How to achieve multiple Graphql endpointsasked 4 months ago
AWS Amplify and AWS Cognito: Website subscription verificationasked 6 months ago
Execution Timeout AWS AppSync APIasked 2 months ago
AppSync authorizationAccepted Answerasked 3 years ago
VTL resolver to authenticate multiple users from two different cognito poolsasked 25 days ago
How to set the inividual data source of a pipline resolver function via CLI Amplifyasked 2 months ago
[Amplify CLi ^7.0.0] How to access custom resources inside of amplify generated resources?asked 5 months ago
React app with Cognito User pool: Not authorized to access APIasked 2 years ago
How do I set up an AWS Amplify project to query an existing AWS AppSync API?asked a month ago