Skip to content

@aws-cdk/aws-appsync-alpha: No way to enable caching for AWS AppSync and its ResolvableFields #19284

Open
@SchollSimon

Description

@SchollSimon

General Issue

CDK: AWS AppSync per resolver caching

The Question

We are trying to enable per-resolver caching for some of AWS AppSync queries with cdk:

  1. L2 Construct GraphqlApi -> import { GraphqlApi } from '@aws-cdk/aws-appsync-alpha'; does not offer any possibility to add caching configuration. Workaround currently is to use CfnApiCache, which is a bit unintuitive.
    Here a sample:
const api = new GraphqlApi(this, 'MyAPI', {
  ...getApiConfiguration(this.config),
});

const cache = new CfnApiCache(this, 'MyAPICache', {
  apiCachingBehavior: 'PER_RESOLVER_CACHING',
  apiId: api.apiId,
  ttl: Duration.seconds(30).toSeconds(),
  type: 'SMALL',
  atRestEncryptionEnabled: true,
  transitEncryptionEnabled: true,
});
cache.addDependsOn(api.node.defaultChild as CfnGraphQLApi);
  1. A major issue without any workaround: L1 Construct -> CfnResolver and its corresponding L2 Construct Resolver offer a resolver caching configuration, but using them doesn't work as their queries are not concatenated into the schema. Working with ResolvableFields for adding queries to my GraphQlApi works, but ResolvableFields isn't a L2 Construct, it has no caching config neither i can access its underlying cloudformation resouce.

Here a sample on adding my query using ResolvableField construct:

api.addQuery(
  'getDummy',
  new ResolvableField({
    returnType: GraphqlType.string(),
    pipelineConfig: appSyncFunctions,
    requestMappingTemplate: requestTemplateLocalResolver(),
    responseMappingTemplate: responseTemplateLocalResolver(),
  })
);

Like described, there is no way to define a caching config for that field.

Here my attempt to define a resolver:

api.createResolver({
  typeName: 'Query',
  fieldName: 'getDummy',
  pipelineConfig: appSyncFunctions,
  requestMappingTemplate: requestTemplateLocalResolver(),
  responseMappingTemplate: responseTemplateLocalResolver(),
  cachingConfig: {
    ttl: Duration.days(1),
    cachingKeys: ['$context.arguments.id'],
  },
});

This resolver is not added to my graphql schema.

And finally having Resolver and ResolvableField at the same time to add operations to my GraphQlApi feels very complicated. Maybe i am missing something to define per-resolver caching behaviour with CDK, would be grateful to get any feedback soon.

Best Regards

CDK CLI Version

2.15.0

Framework Version

2.15.0

Node.js Version

v16.13.1

OS

Windows 10

Language

Typescript

Language Version

4.5.4

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions