Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GraphqlApi: Creating GraphqlApi and providing definition using fromSourceApis method , Dependency is not being added between AWS::AppSync::SourceApiAssociation resource and AWS::AppSync::GraphQLSchema which causes creation failures #29044

Closed
Labels
@aws-cdk/aws-appsync Related to AWS AppSync bug This issue is a bug. effort/medium Medium work item – several days of effort p2

Comments

@rahuldeverani
Copy link

Describe the bug

When we create mergedAPI using GraphqlApi class and provide the firstApi as sourceApi , and use fromSourceApis method to provide definition of merged API then cloudformation deployment fails with the error:

Resource handler returned message: "Failed to merge source API: to Merged API: with the following error: There's no types found in this API

Expected Behavior

CDK should implicitly add dependency in the template such that AWS::AppSync::SourceApiAssociation for merged api is created after AWS::AppSync::GraphQLSchema of sourceapi.

Current Behavior

Currently , in the generated template , the resource AWS::AppSync::SourceApiAssociation does not wait for the creation of AWS::AppSync::GraphQLSchema for the sourceApi which leads to this error.

Reproduction Steps

const authorizationLambdaFunction = new lambda.Function(this, 'AuthFunction', {
  code: lambda.Code.fromAsset('lambda'),
  runtime: lambda.Runtime.NODEJS_18_X,
  handler: 'hello.handler',
})

// first source API
const firstApi = new appsync.GraphqlApi(this, 'ProductsAPI', {
  name: 'ProductsAPI',
  definition: appsync.Definition.fromFile(path.join(__dirname,'appsync.merged-api-1.graphql')),
  authorizationConfig: {
    defaultAuthorization: {
      authorizationType: appsync.AuthorizationType.LAMBDA,
      lambdaAuthorizerConfig: {
        handler: authorizationLambdaFunction
      }
    }
  },
  logConfig: {
    fieldLogLevel: appsync.FieldLogLevel.ALL,
    excludeVerboseContent: false
  }
});

// Merged API
const mergedApi = new appsync.GraphqlApi(this, 'MergedAPI', {
  name: 'MergedAPI',
  definition: appsync.Definition.fromSourceApis({
    sourceApis: [
      {
        sourceApi: firstApi,
        mergeType: appsync.MergeType.AUTO_MERGE,
      },
    ],
  }),
  authorizationConfig: {
    defaultAuthorization: {
      authorizationType: appsync.AuthorizationType.LAMBDA,
      lambdaAuthorizerConfig: {
        handler: authorizationLambdaFunction
      }
    }
  }
});

Possible Solution

Add depends on between the resources such that AWS::AppSync::SourceApiAssociation depends on AWS::AppSync::GraphQLSchema

workaround :

manually add the dependency in cdk code:
mergedApi.node.addDependency(firstApi);

Additional Information/Context

No response

CDK CLI Version

2.124.0

Framework Version

No response

Node.js Version

v20.8.1

OS

MacOs

Language

TypeScript

Language Version

No response

Other information

No response

@rahuldeverani rahuldeverani added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Feb 9, 2024
@github-actions github-actions bot added the @aws-cdk/aws-appsync Related to AWS AppSync label Feb 9, 2024
@pahud
Copy link
Contributor

pahud commented Feb 9, 2024

Thank you for the report and workaround. Looks like we need an PR to implicitly add the dependency.

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Feb 9, 2024
@mergify mergify bot closed this as completed in #29455 Apr 9, 2024
mergify bot pushed a commit that referenced this issue Apr 9, 2024
### Issue # (if applicable)

Closes #29044.

### Reason for this change

When associating between the source GraphQL API and the merged API using `fromSourceApis`, generated association resource doesn't depend on the source GraphQL schema.

```ts
// This API has `CfnGraphQLSchema` by `definition` prop
const firstApi = new appsync.GraphqlApi(stack, 'FirstSourceAPI', {
  name: 'FirstSourceAPI',
  definition: appsync.Definition.fromFile(path.join(__dirname, 'appsync.merged-api-1.graphql')),
});

// This merged API generates `CfnSourceApiAssociation`
new appsync.GraphqlApi(stack, 'MergedAPI', {
  name: 'MergedAPI',
  definition: appsync.Definition.fromSourceApis({
    sourceApis: [
      {
        sourceApi: firstApi,
        mergeType: appsync.MergeType.MANUAL_MERGE,
      },
    ],
  }),
});
```

The same is true if the `SourceApiAssociation` construct is used explicitly.

```ts
// This API has `CfnGraphQLSchema` by `definition` prop
const firstApi = new appsync.GraphqlApi(stack, 'FirstSourceAPI', {
  name: 'FirstSourceAPI',
  definition: appsync.Definition.fromFile(path.join(__dirname, 'appsync.merged-api-1.graphql')),
});

// This merged API does not generate `CfnSourceApiAssociation`
const mergedApi = new appsync.GraphqlApi(stack, 'MergedAPI', {
  name: 'MergedAPI',
  definition: appsync.Definition.fromSourceApis({
    sourceApis: [],
    mergedApiExecutionRole: mergedApiExecutionRole,
  }),
});

// This construct has `CfnSourceApiAssociation`
new appsync.SourceApiAssociation(stack, 'SourceApiAssociation1', {
  sourceApi: firstApi,
  mergedApi: mergedApi,
  mergeType: appsync.MergeType.MANUAL_MERGE,
  mergedApiExecutionRole: mergedApiExecutionRole,
});
```

### Description of changes

The `sourceApi` passed by the `fromSourceApis` method or the `SourceApiAssociation` construct has `addSchemaDependency` method. Using this method, we can make the association to depend on the schema in the `sourceApi`.
But, if the api is an IMPORTED resource to begin with, it has no schema in the CDK layer, so there is nothing we can do about it. (The method does nothing.)

### Description of how you validated changes

Both unit and existing integ tests.

### Checklist
- [x] My code adheres to the [CONTRIBUTING GUIDE](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md) and [DESIGN GUIDELINES](https://github.com/aws/aws-cdk/blob/main/docs/DESIGN_GUIDELINES.md)

----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Copy link

github-actions bot commented Apr 9, 2024

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment