Home > OS >  AWS CDK, typescript - Argument of type 'this' is not assignable to parameter of type '
AWS CDK, typescript - Argument of type 'this' is not assignable to parameter of type '

Time:12-20

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

CodePudding user response:

You are mixing dependencies from CDK V1 and the newly released CDK V2, which are incompatible.

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';

CodePudding user response:

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]
  • Related