I'm prototyping AppSyncApi, is there some example ...
# help
s
I'm prototyping AppSyncApi, is there some example how to add api / resolver caching to appsync app? Aws has cloudformation template of resolver/caching but cdk does not have construct for it yet
found some kind of solution:
Copy code
import * as sst from '@serverless-stack/resources'
import * as appsync from '@aws-cdk/aws-appsync'

export default class AppsyncCacheExampleStack extends sst.Stack {
  constructor(scope: <http://sst.App|sst.App>, id: string, props?: sst.StackProps) {
    super(scope, id, props)

    const appSyncApi = new sst.AppSyncApi(this, 'AppSyncApi', {
      graphqlApi: {
        xrayEnabled: true,
        schema: ['graphql/cache-example.graphql'],
      },
      dataSources: {
        fooDS: 'src/cache-example/foo.handler',
      },
    })

    const apiId = appSyncApi.graphqlApi.apiId
    const ttl = 30

    new appsync.CfnApiCache(this, 'ApiCache', {
      apiCachingBehavior: 'PER_RESOLVER_CACHING',
      apiId,
      ttl,
      type: 'SMALL',
      // the properties below are optional
      atRestEncryptionEnabled: false,
      transitEncryptionEnabled: false,
    })

    new appsync.CfnResolver(this, 'MyCfnResolver', {
      apiId,
      fieldName: 'getFooById',
      typeName: 'Query',
      cachingConfig: {
        cachingKeys: ['$context.args.id'],
        ttl,
      },
      dataSourceName: appSyncApi.getDataSource('fooDS')?.name,
      kind: 'UNIT',
    })

    // Show the endpoint in the output
    this.addOutputs({
      AppsyncEndpoint: appSyncApi.url,
      apiId,
    })
  }
}