Skip to content

(aws-appsync): Unable to addsubscription using code-first - 'DataSourceName is required for UNIT resolver' #16071

@rjjaegeraws

Description

@rjjaegeraws

When attempting to add a subscription using code-first, the cdk deploy fails with the error "DataSourceName is required for UNIT resolver".

Reproduction Steps

import * as cdk from '@aws-cdk/core';
import * as appsync from '@aws-cdk/aws-appsync';
import * as db from '@aws-cdk/aws-dynamodb';


export class DemoStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);
    
      const string = appsync.GraphqlType.string();
      const string_required = appsync.GraphqlType.string({ isRequired: true });
      const int = appsync.GraphqlType.int();
      const id_gql_required = appsync.GraphqlType.id({ isRequired: true });
      const id_gql = appsync.GraphqlType.id({ isRequired: false });
      
       
      const api = new appsync.GraphqlApi(this, 'Api', {
        name: 'demo',
      });
      
        //Data Source
      const commentsTable = new db.Table(this, 'CommentsTable', {
        partitionKey: {
          name: 'eventId',
          type: db.AttributeType.STRING,
        },
        sortKey: {
          name: 'commentId',
          type: db.AttributeType.STRING
        }
      });
      
      const commentsDS = api.addDynamoDbDataSource('commentsDataSource', commentsTable);
   
       const eventsTable = new db.Table(this, 'EventsTable', {
        partitionKey: {
          name: 'id',
          type: db.AttributeType.STRING,
        }
      });
      
      const eventsDS = api.addDynamoDbDataSource('eventsDataSource', eventsTable);
      
      const comment = new appsync.ObjectType('Comment', {
        definition: {
          eventId: appsync.GraphqlType.id({ isRequired: true }),
          commentId: appsync.GraphqlType.string({ isRequired: true }),
          content: appsync.GraphqlType.string({ isRequired: true }),
          createdAt: appsync.GraphqlType.string({ isRequired: true }),
        },
      });
      
      api.addType(comment);
      
      const commentConnect = new appsync.ObjectType('CommentConnection', {
        definition: {
          items: comment.attribute({isList: true}),
          nextToken: appsync.GraphqlType.string()
        }
      });
      
      api.addType(commentConnect);
      
      const event = new appsync.ObjectType('Event', {
        definition: {
          id: appsync.GraphqlType.id({ isRequired: true }),
          name: appsync.GraphqlType.string({ isRequired: false }),
          where: appsync.GraphqlType.string({ isRequired: false }),
          when: appsync.GraphqlType.string({ isRequired: false }),
          description: appsync.GraphqlType.string({ isRequired: false }),
          comments: new appsync.ResolvableField({
            returnType:  commentConnect.attribute(),
            args: { limit: int, nextToken: string },
            dataSource: commentsDS,
            requestMappingTemplate: appsync.MappingTemplate.fromString(`
{
    "version": "2017-02-28",
    "operation": "Query",
    "query": {
        "expression": "eventId = :eventId",
        "expressionValues": {
            ":eventId": {
                "S": "$context.source.id"
            }
        }
    },
    "limit": #if($context.arguments.limit) $context.arguments.limit #else 10 #end,
    "nextToken": #if($context.arguments.nextToken) "$context.arguments.nextToken" #else null #end
}
`),
            responseMappingTemplate: appsync.MappingTemplate.fromString(`
{
    "items": $util.toJson($context.result.items),
    "nextToken": $util.toJson($context.result.nextToken)
}
`)})},
});
      
      api.addType(event);
      
        
      const eventConnect = new appsync.ObjectType('EventConnection', {
        definition: {
          items: event.attribute({isList: true}),
          nextToken: appsync.GraphqlType.string()
        }
      });
      
      api.addType(eventConnect);
      
      
    api.addQuery('getEvent',
        new appsync.ResolvableField({
          returnType: event.attribute(),
          args: { id: id_gql_required },
          dataSource: eventsDS, 
          requestMappingTemplate: appsync.MappingTemplate.fromString(`
{
    "version": "2017-02-28",
    "operation": "GetItem",
    "key": {
        "id": { "S": "$context.arguments.id" }
    }
}`),
          responseMappingTemplate: appsync.MappingTemplate.fromString(`$util.toJson($context.result)`),
        })
      );
      
  
  api.addQuery('listEvents', 
    new appsync.ResolvableField({
      returnType: eventConnect.attribute(),
      args: { limit: id_gql, nextToken: string },
       dataSource: eventsDS, 
          requestMappingTemplate: appsync.MappingTemplate.fromString(`
{ 
    "version": "2017-02-28",
    "operation": "Scan",
    "limit": #if($context.arguments.limit) $context.arguments.limit #else 10 #end,
    "nextToken": #if($context.arguments.nextToken) "$context.arguments.nextToken" #else null #end
}`),
          responseMappingTemplate: appsync.MappingTemplate.fromString(`
{
    "items": $util.toJson($context.result.items),
    "nextToken": $util.toJson($context.result.nextToken)
}`)
    })
  );
  
  
  api.addSubscription('subscribeToEventComments', 
    new appsync.ResolvableField({
      returnType: comment.attribute(),
      args: { eventId: string_required },
      directives: [appsync.Directive.subscribe('commentOnEvent')],
    })
  );
      
  
  }
}

What did you expect to happen?

That the stack deploys successfully. The CDK documentation: https://docs.aws.amazon.com/cdk/api/latest/docs/aws-appsync-readme.html shows the following to add a subscription:

api.addSubscription('addedFilm', new appsync.ResolvableField({
  returnType: film.attribute(),
  args: { id: appsync.GraphqlType.id({ isRequired: true }) },
  directive: [appsync.Directive.subscribe('addFilm')],
}));

What actually happened?

CloudFormation fails to deploy the stack:

7:21:51 PM | UPDATE_FAILED        | AWS::AppSync::Resolver      | ApiSubscriptionsub...tsResolverFBD5E657
DataSourceName is required for UNIT resolver.

Environment

  • **CDK CLI Version : 1.118.0 (build a4f0418)
  • Framework Version:
  • Node.js Version: v14.17.5
  • OS : Amazon Linux 2
  • Language (Version): TypeScript

Other


This is 🐛 Bug Report

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions