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

feat(appsync): merged APIs #26895

Merged
merged 3 commits into from
Aug 30, 2023
Merged

feat(appsync): merged APIs #26895

merged 3 commits into from
Aug 30, 2023

Conversation

jumic
Copy link
Contributor

@jumic jumic commented Aug 26, 2023

Add support for AppSync Merged API feature.

At the moment, a GraphQL schema can be passed using the schema property. I deprecated this property because it is not used for merged APIs.
Depecreated syntax:

const api = new appsync.GraphqlApi(this, 'Api', {
  name: 'demo',
  schema: appsync.SchemaFile.fromAsset(path.join(__dirname, 'schema.graphql')),
});

Instead, I introduced a new property apiSource that can be used to create a AppSync GraphQL API or Merged API.
GraphQL API:

const api = new appsync.GraphqlApi(this, 'Api', {
  name: 'demo',
  apiSource: appsync.ApiSource.fromSchema(appsync.SchemaFile.fromAsset(path.join(__dirname, 'schema.graphql'))),
  // short version
  apiSource: appsync.ApiSource.fromFile(path.join(__dirname, 'schema.graphql')),
});

Merged API:

const api = new appsync.GraphqlApi(this, 'Api', {
  name: 'demo',
  apiSource: appsync.ApiSource.fromSourceApis({
    sourceApis: [
      {
        sourceApi: firstApi,
        mergeType: appsync.MergeType.MANUAL_MERGE,
      },
      {
        sourceApi: secondApi,
        mergeType: appsync.MergeType.AUTO_MERGE,
      },
    ],
  }),
});

Closes #25960.


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

@aws-cdk-automation aws-cdk-automation requested a review from a team August 26, 2023 14:11
@github-actions github-actions bot added distinguished-contributor [Pilot] contributed 50+ PRs to the CDK effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2 labels Aug 26, 2023
@aws-cdk-automation aws-cdk-automation added the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Aug 26, 2023
@rix0rrr rix0rrr changed the title feat(appsync): support Merged APIs feat(appsync): merged APIs Aug 29, 2023
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I love it!

I have one lingering concern on naming, but I'm also having a hard time coming up with something 😅

packages/aws-cdk-lib/aws-appsync/README.md Outdated Show resolved Hide resolved
@@ -423,7 +465,7 @@ const logConfig: appsync.LogConfig = {
new appsync.GraphqlApi(this, 'api', {
authorizationConfig: {},
name: 'myApi',
schema: appsync.SchemaFile.fromAsset(path.join(__dirname, 'myApi.graphql')),
apiSource: appsync.ApiSource.fromFile(path.join(__dirname, 'myApi.graphql')),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know I came up with apiSource myself, but I'm not a 100% happy with it 😅 . Have you given any thought to alternatives? Is there a term that AppSync itself uses for that might apply here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably schema should be part of the name. Both GraphQL APIs and Merged APIs have a schema configuration in the AWS console:

  • GraphQL API: Design your schema using GraphQL SDL, attach resolvers, and quickly deploy AWS resources.
  • Merged API: Your Merged API’s schema is created from the schemas of your included Source APIs, and is read only. Changes to the schema must be initiated in your source APIs.

How about schemaSource or schemaDefinition?

@elthrasher @Lock128 After using the Merged API in our hackathon project: Do you have any feedback or ideas for a new name? We are looking for a better name than apiSource (see examples in the pull request).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like "definition". How about just definition?: Definition or apiDefinition?: ApiDefinition ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's call it definition. Other attributes like name don't use api as a prefix, so I'm fine with definition.

/**
* Merging option used to associate the source API to the Merged API
*
* @default - Manual merge, requires the user to trigger schema merges manually.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ooo, what does that mean?

Doesn't automatic sound like a better default?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Developers must manually press the Merge Now button in the AWS console to merge changes from source APIs into the Merged API. This is the default behaviour in CloudFormation (see documentation) and when you manually create a Merged API in the AWS console.

However, I used the AUTO_MERGE merge type in my project. So it's fine for me if we change it default to AUTO_MERGE in CDK. Please let me know which default value we should use.

Copy link
Contributor

@rix0rrr rix0rrr Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Whatever default we choose should be friendly to CI/CD. If a merge cannot be triggered from CloudFormation, then I think automatic should be the default.

I'm impressed this service team actually built this feature! Let's use it!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, I changed the default to AUTO_MERGE.

* @param filePath the file path of the schema file
* @returns API Source with schema from file
*/
public static fromFile(filePath: string): ApiSource {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

❤️

@@ -511,6 +619,15 @@ export class GraphqlApi extends GraphqlApiBase {

this.validateAuthorizationProps(modes);

if ((props.schema !== undefined) === (props.apiSource !== undefined)) {
throw new Error('You cannot specify both properties schema and apiSource.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And you must do at least one (so, exactly one).

(props.schema !== undefined) === (props.apiSource !== undefined) will also be true if both are undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done, another if-statement checks if schema or definition is set.

packages/aws-cdk-lib/aws-appsync/lib/graphqlapi.ts Outdated Show resolved Hide resolved
*/
public readonly schema: ISchema;
public readonly apiSource: ApiSource;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's make it easy on ourselves and make this private. I don't want to expose things on the public API unless absolutely necessary.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't that be a breaking change? Currently, I can access the schema of the construct, but later I cannot?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

schema will still be there, it just got moved.

(But it will throw if the API was not created from a schema)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should schema be deprecated? I deprecated it in this PR. That means, it will be removed in CDK v3.
The schema will then be inaccessible, which could be considered a breaking change (information is no longer available). However, I am not sure how this property is used by other developers.

Copy link
Contributor

@rix0rrr rix0rrr Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for letting me know, I had missed that.

Let's leave .schema as it is for now without deprecating. I'm also not quite sure what people would use it for; I'm inclined to think it's not necessary, but I also don't want to make too many assumptions.

@aws-cdk-automation aws-cdk-automation removed the pr/needs-community-review This PR needs a review from a Trusted Community Member or Core Team Member. label Aug 29, 2023
@mergify mergify bot dismissed rix0rrr’s stale review August 29, 2023 20:13

Pull request has been modified.

@jumic jumic requested a review from rix0rrr August 29, 2023 21:31
Copy link
Contributor

@rix0rrr rix0rrr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@mergify
Copy link
Contributor

mergify bot commented Aug 30, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@aws-cdk-automation
Copy link
Collaborator

AWS CodeBuild CI Report

  • CodeBuild project: AutoBuildv2Project1C6BFA3F-wQm2hXv2jqQv
  • Commit ID: 7f76948
  • Result: SUCCEEDED
  • Build Logs (available for 30 days)

Powered by github-codebuild-logs, available on the AWS Serverless Application Repository

@mergify mergify bot merged commit fe930a5 into aws:main Aug 30, 2023
8 checks passed
@mergify
Copy link
Contributor

mergify bot commented Aug 30, 2023

Thank you for contributing! Your pull request will be updated from main and then merged automatically (do not update manually, and be sure to allow changes to be pushed to your fork).

@jumic
Copy link
Contributor Author

jumic commented Aug 30, 2023

Thanks!

@rix0rrr Thank you also for your help with this PR. 👍

this.mergedApiExecutionRole = sourceApiOptions.mergedApiExecutionRole;
} else {
const sourceApiArns = sourceApiOptions.sourceApis?.map(sourceApiOption => {
return sourceApiOption.sourceApi.arn;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need this role to be able to access all top level fields like: :*. Can you fix this to add both the source api arn alone and the source api arn with wildcard for all top level fields? https://docs.aws.amazon.com/appsync/latest/devguide/merged-api.html#execution-roles-merged-api

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for pointing this out. I will fix it next week. I'm on holiday at the moment.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, I am actually going to be in this code for an enhancement here so I can go ahead and submit these changes if you want?

@TheOptimist
Copy link

I don't mean to necro this, but the deprecation comment when using schema still references apiSource, which was really confusing until I came to read this PR :)

* @deprecated use apiSource.schema instead

What's next? I can't see myself having the time to make a PR for this change any time soon...an issue for documentation? Can someone else make a PR faster than me (probably)?

@ndejaco2
Copy link
Contributor

ndejaco2 commented Nov 3, 2023

I don't mean to necro this, but the deprecation comment when using schema still references apiSource, which was really confusing until I came to read this PR :)

* @deprecated use apiSource.schema instead

What's next? I can't see myself having the time to make a PR for this change any time soon...an issue for documentation? Can someone else make a PR faster than me (probably)?

#27831

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
distinguished-contributor [Pilot] contributed 50+ PRs to the CDK effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging this pull request may close these issues.

appsync: support Merged APIs
6 participants