Andrija Gajic
Andrija Gajic

Reputation: 382

AWS CDK, typescript - Argument of type 'this' is not assignable to parameter of type 'Construct'

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

Upvotes: 3

Views: 3717

Answers (2)

fedonev
fedonev

Reputation: 25809

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

Upvotes: 9

Andrija Gajic
Andrija Gajic

Reputation: 382

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 [email protected]

Upvotes: -1

Related Questions