AppSync

AppSync

Basic support for AppSync is included in LocalStack Pro. The local AppSync API allows you to spin up local GraphQL APIs and directly expose your data sources (e.g., DynamoDB tables) to external clients.

For example, you can create a DynamoDB table "posts" with a key attribute id, and define a GraphQL schema in a file schema.graphql like this:

  1. schema {
  2. query: Query
  3. }
  4. type Query {
  5. getPosts: [Post!]!
  6. }
  7. type Post {
  8. id: DDBString!
  9. }
  10. type DDBString {
  11. S: String!
  12. }

… and then use the AppSync API (or CloudFormation) to create the following entities:

  1. a GraphQL API
  2. a data source of type AMAZON_DYNAMODB that references the "posts" DynamoDB table
  3. a request mapping template with a content like this:
  1. {
  2. "version" : "2017-02-28",
  3. "operation" : "Scan"
  4. }
  1. a response mapping template with a content like this:
  1. $util.toJson($context.result["Items"])

Once things have been wired up properly, and assuming the ID of your GraphQL API is "api123", you should be able to run the following GraphQL query to retrieve all items from the "posts" DynamoDB table:

  1. $ curl -d '{"query":"query {getPosts{id{S}}}"}' http://localhost:4605/graphql/api123

For more details, please refer to the self-contained sample published in this Github repository.

Custom GraphQL API IDs

It is possible to use a predefined ID when creating GraphQL APIs by setting the tag _custom_id_. For example:

  1. $ awslocal appsync create-graphql-api --name my-api --authentication-type API_KEY --tags _custom_id_=faceb00c
  2. {
  3. "graphqlApi": {
  4. "name": "my-api",
  5. "apiId": "faceb00c",
  6. "authenticationType": "API_KEY",
  7. "arn": "arn:aws:appsync:us-east-1:000000000000:apis/my-api",
  8. "uris": {
  9. "GRAPHQL": "http://localhost:4566/graphql/faceb00c",
  10. "REALTIME": "ws://localhost:4510/graphql/faceb00c"
  11. },
  12. "tags": {
  13. "_custom_id_": "faceb00c"
  14. }
  15. }
  16. }

GraphQL Endpoints

There are three configurable strategies that govern how GraphQL API endpoints are created. The strategy can be configured via the GRAPHQL_ENDPOINT_STRATEGY environment variable.

ValueFormatDescription
domain<api-id>.appsync-api.localhost.localstack.cloud:4566This will be the default strategy in the future that uses the localhost.localstack.cloud domain to route to your localhost
pathlocalhost:4566/appsync-api/<api-id>/graphqlAn alternative that can be useful if you cannot resolve LocalStack’s localhost domain
legacylocalhost:4566/graphql/<api-id>The old shape of the endpoint, which is currently the default but will be phased out

Last modified May 25, 2022: Add GRAPHQL_ENDPOINT_STRATEGY (#168) (ae7512e6)