Using Your API - AWS AppSync

Using Your API

Now that you have a GraphQL API with a schema uploaded, data sources configured, and resolvers connected to your types, you can test your API. The following two examples assume you’ve used the basic schema from Designing Your Schema.

In the AWS AppSync, choose the Queries tab and then enter the following text in the editor:

mutation add { addTodo(id:"123" name:"My TODO" description:"Testing AWS AppSync" priority:2){ id name description priority } }

Choose the button at the top to run your mutation. After the run is complete, the result from your selection set (that is, id, name, description, and priority) appear on the right. The data is also in the Amazon DynamoDB table for your data source, which you can verify using the console.

Now, run a query as follows:

query list { getTodos { id name } }

This returns your data, but it only returns the two fields (id and name) from your selection set. Note: If you are getting an error such as Validation error of type FieldUndefined, you might have followed the instructions for setting up relations and pagination. If so, use the queries in the next section.

Relations and Pagination

If you have set up the advanced schema with relations and pagination, your getTodos query will look a bit different. First, add a comment to your todo as follows:

mutation addComment { addComment(todoid:"123" content:"GraphQL is fun"){ todoid commentid content } }

In this case, the commentid is automatically created. Add a few more todos and comments, set the limit to 2 records, and then run getTodos as follows:

query list { getTodos(limit:2){ todos { id name status comments{ commentid content } } nextToken } }

Only two todos come back (with the comments) because you specified limit:2 in the arguments. A long string should be returned for the nextToken field. If you copy this string, you can pass it as a second argument in getTodos by replacing XXXXXXXXXXXX in getTodos(limit:2 nextToken:"XXXXXXXXXXXX"). For example:

query list { getTodos(limit:2 nextToken:"eyJ2ZXJzaW9uejE1a2RPanZPQzFCMlFTdGNxdUFBQUJxekNDQWFjR0NTcUdTSWIzRFFFSEJxQ0NBWmd3Z2dHVUFnRUFNSUlCalFZSktvWklodmNOQVFjQk1CNEdDV0NHU0FGbEF3UUJMakFSQkF5eEtWQjUvTlJxS2o5ZHBYc0NBUkNBZ2dGZUZFNW1MbTRkb25BOUg0U0FpOGhSZ1lucmRndmQz"){ todos { id name status comments{ commentid content } } nextToken } }

In a client application you would pass this token through as an argument based on your pagination flow and logic.

Next Steps

Now that you’ve created a schema, you can walk through how to use it with different data sources, learn more about the advanced resolver capabilities, or build a client application. For more information, see the following sections: