3

I have this code generated by cdk cli(typecript-language):

import { Stack, StackProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import * as appsync from '@aws-cdk/aws-appsync';

export class BookStoreGraphqlApiStack extends Stack {
    constructor(scope: Construct, id: string, props?: StackProps) {
        super(scope, id, props);

        const api = new appsync.GraphqlApi(this, 'MyApi', {
            name: 'my-book-api',
            schema: appsync.Schema.fromAsset('graphql/schema.graphql')
        });
    }
}

And I get this error:

Argument of type 'this' is not assignable to parameter of type 'Construct'.
  Type 'BookStoreGraphqlApiStack' is missing the following properties from type 'Construct': onValidate, onPrepare, onSynthesize

My package.json:

"devDependencies": {
    "@types/jest": "^26.0.10",
    "@types/node": "10.17.27",
    "aws-cdk": "2.2.0",
    "jest": "^26.4.2",
    "ts-jest": "^26.2.0",
    "ts-node": "^9.0.0",
    "typescript": "~3.9.7"
  },
  "dependencies": {
    "@aws-cdk/aws-appsync": "^1.136.0",
    "aws-cdk-lib": "2.2.0",
    "constructs": "^10.0.12",
    "source-map-support": "^0.5.16"
  }

Node version: v16.13.0

aws-cdk version: 2.2.0

Andrija Gajic
  • 314
  • 3
  • 15

2 Answers2

9

You are mixing dependencies from CDK V1 and V2, which are incompatible. See the migration guide.


[2023 Note]: The AppSync L2 constructs are now stable and moved to aws-cdk-lib. The aws-cdk/aws-appsync-alpha package is no longer needed.


To get a valid V2 setup, change the AppSync package to import * as appsync from '@aws-cdk/aws-appsync-alpha'. In V2, alpha APIs have separate packages, stable ones are in a common aws-cdk-lib. AppSync's L2 constructs are in the alpha category.

Here is an overview of import patterns for both CDK versions:

// V2
import { Construct } from 'constructs'; // construct is in a separate package
import { Stack, StackProps, aws_s3 as s3 } from 'aws-cdk-lib'; // common package for stable construct
import * as appsync from '@aws-cdk/aws-appsync-alpha'  // alpha constructs in separate packages

// V1 - separate packages core and for each service
import * as cdk from '@aws-cdk/core';
import * as appsync from '@aws-cdk/aws-appsync';

This table shows where to find the dependencies for each CDK version:

Package CDK Version Constructs
aws-cdk-lib v2 L1 (CfnSomething) constructs and stable L2/L3 constructs
@aws-cdk/aws-something-alpha v2 experimental L2/L3 consturcts
@aws-cdk/aws-something v1 all
fedonev
  • 20,327
  • 2
  • 25
  • 34
  • 2
    I had the same problem, I was following the CDK V1 example (https://docs.aws.amazon.com/cdk/latest/guide/serverless_example.html) but I had already installed CDK v2, so I needed to follow the CDK V2 example ( https://docs.aws.amazon.com/cdk/v2/guide/serverless_example.html). – Carlos Dec 19 '21 at 20:10
  • Thanks for this, the docs still haven't been updated. Thanks Amazon This kinda mess gives us job security I guess. – talves Jun 15 '22 at 22:16
  • You can find the documentation here: https://docs.aws.amazon.com/cdk/api/v2/ and https://docs.aws.amazon.com/cdk/api/v2/docs/aws-construct-library.html – Shivang Chaturvedi Jul 11 '22 at 15:41
0

Turns out this is a known problem with aws-cdk, since v2 isn't properly migrated, the solution is:

npm un -g aws-cdk
npm i -g aws-cdk@1.136.0
Andrija Gajic
  • 314
  • 3
  • 15