How do I set up an AWS Amplify project to query an existing AWS AppSync API?
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
isnull
(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!
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.
@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
ornextToken
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).
Relevant questions
How to use Amplify Datastore to sync with data from DynamoDB and seed DynamoDB from a Lamba
asked 4 months agoHow to achieve multiple Graphql endpoints
asked 4 months agoAWS Amplify and AWS Cognito: Website subscription verification
asked 6 months agoExecution Timeout AWS AppSync API
asked 2 months agoAppSync authorization
Accepted Answerasked 3 years agoVTL resolver to authenticate multiple users from two different cognito pools
asked 25 days agoHow to set the inividual data source of a pipline resolver function via CLI Amplify
asked 2 months ago[Amplify CLi ^7.0.0] How to access custom resources inside of amplify generated resources?
asked 5 months agoReact app with Cognito User pool: Not authorized to access API
asked 2 years agoHow do I set up an AWS Amplify project to query an existing AWS AppSync API?
asked a month ago
Hi, there. Thank you for your reply.
add codegen
command.nextToken
becomes null and then returns the data to the client.Clarifications:
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