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): import existing graphql api #9254

Merged
merged 22 commits into from
Aug 15, 2020
Merged
Show file tree
Hide file tree
Changes from 16 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions packages/@aws-cdk/aws-appsync/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,24 @@ demoDS.createResolver({
});
```

## Imports

Any GraphQL Api that has been created outside the stack can be imported from
another stack into your CDK app. Utilizing the `fromXxx` function, you have
the ability to add data sources and resolvers through a `IGraphQLApi` interface.

```ts
const importedApi = appsync.GraphQLApi.fromGraphQLApiAttributes(stack, 'IApi', {
graphqlApiId: api.apiId,
graphqlArn: api.arn,
});
importedApi.addDynamoDbDataSource('TableDataSource', 'Table', table);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Suggested change
importedApi.addDynamoDbDataSource('TableDataSource', 'Table', table);
importedApi.addDynamoDbDataSource(table, 'tableId');

and change everywhere else

```

If you don't specify `graphqlArn` in `fromXxxAttributes`, CDK will autogenerate
the expected `arn` for the imported api, given the `apiId`. For creating data
sources and resolvers, an `apiId` is sufficient.

## Permissions

When using `AWS_IAM` as the authorization type for GraphQL API, an IAM Role
Expand Down
6 changes: 3 additions & 3 deletions packages/@aws-cdk/aws-appsync/lib/data-source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { IGrantable, IPrincipal, IRole, Role, ServicePrincipal } from '@aws-cdk/
import { IFunction } from '@aws-cdk/aws-lambda';
import { Construct, IResolvable } from '@aws-cdk/core';
import { CfnDataSource } from './appsync.generated';
import { GraphQLApi } from './graphqlapi';
import { IGraphQLApi } from './graphqlapi-base';
import { BaseResolverProps, Resolver } from './resolver';

/**
Expand All @@ -13,7 +13,7 @@ export interface BaseDataSourceProps {
/**
* The API to attach this data source to
*/
readonly api: GraphQLApi;
readonly api: IGraphQLApi;
/**
* The name of the data source
*/
Expand Down Expand Up @@ -91,7 +91,7 @@ export abstract class BaseDataSource extends Construct {
*/
public readonly ds: CfnDataSource;

protected api: GraphQLApi;
protected api: IGraphQLApi;
protected serviceRole?: IRole;

constructor(scope: Construct, id: string, props: BackedDataSourceProps, extended: ExtendedDataSourceProps) {
Expand Down
190 changes: 190 additions & 0 deletions packages/@aws-cdk/aws-appsync/lib/graphqlapi-base.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,190 @@
import { ITable } from '@aws-cdk/aws-dynamodb';
import { IFunction } from '@aws-cdk/aws-lambda';
import { CfnResource, IResource, Resource } from '@aws-cdk/core';
import { DynamoDbDataSource, HttpDataSource, LambdaDataSource, NoneDataSource } from './data-source';

/**
* Optional configuration for data sources
*/
export interface DataSourceOptions {
/**
* The name of the data source
*
* @default - Automatically generated name '<DataSourceType>CDKDataSource'
* i.e. (LambdaCDKDataSource)
*/
readonly name?: string;

/**
* The description of the data source
*
* @default - No description
*/
readonly description?: string;
}

/**
* Interface for GraphQL
*/
export interface IGraphQLApi extends IResource {

/**
* an unique AWS AppSync GraphQL API identifier
* i.e. 'lxz775lwdrgcndgz3nurvac7oa'
BryanPan342 marked this conversation as resolved.
Show resolved Hide resolved
*
* @attribute
*/
readonly apiId: string;

/**
BryanPan342 marked this conversation as resolved.
Show resolved Hide resolved
* the ARN of the API
*
* @attribute
*/
readonly arn: string;

/**
* add a new dummy data source to this API. Useful for pipeline resolvers
* and for backend changes that don't require a data source.
*
* @param options The optional configuration for this data source
* @default name - 'NoneCDKDataSource'
* description - No description
*/
addNoneDataSource(options?: DataSourceOptions): NoneDataSource;

/**
* add a new DynamoDB data source to this API
*
* @param table The DynamoDB table backing this data source
* @param options The optional configuration for this data source
* @default name - 'DynamoDbCDKDataSource'
* description - No description
BryanPan342 marked this conversation as resolved.
Show resolved Hide resolved
*/
addDynamoDbDataSource(table: ITable, options?: DataSourceOptions): DynamoDbDataSource;

/**
* add a new http data source to this API
*
* @param endpoint The http endpoint
* @param options The optional configuration for this data source
* @default name - 'HttpCDKDataSource'
* description - No description
*/
addHttpDataSource(endpoint: string, options?: DataSourceOptions): HttpDataSource;

/**
* add a new Lambda data source to this API
*
* @param lambdaFunction The Lambda function to call to interact with this data source
* @param options The optional configuration for this data source
* @default name - 'LambdaCDKDataSource'
* description - No description
*/
addLambdaDataSource(lambdaFunction: IFunction, options?: DataSourceOptions): LambdaDataSource;

/**
* Add schema dependency if not imported
*
* @param construct the dependee
*/
addSchemaDependency(construct: CfnResource): boolean;
}

/**
* Base Class for GraphQL API
*/
export abstract class GraphQLApiBase extends Resource implements IGraphQLApi {

/**
* an unique AWS AppSync GraphQL API identifier
* i.e. 'lxz775lwdrgcndgz3nurvac7oa'
*/
public abstract readonly apiId: string;

/**
* the ARN of the API
*/
public abstract readonly arn: string;
Copy link
Contributor

Choose a reason for hiding this comment

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

did we want to give this a specific prefix before the arn - since we're introducing this, let's make sure it aligns with how we define arn elsewhere

Copy link
Contributor Author

@BryanPan342 BryanPan342 Aug 6, 2020

Choose a reason for hiding this comment

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

if we change this here i feel like we need to change it for every where else.. including the GraphQLAPI.. i think this breaking change should happen in another pr


/**
* add a new none data source to this API. Useful for pipeline resolvers
* and for backend changes that don't require a data source.
*
* @param options The optional configuration for this data source
* @default name - 'NoneCDKDataSource'
* description - No description
*/
public addNoneDataSource(options?: DataSourceOptions): NoneDataSource {
const name = options?.name ?? 'NoneCDKDataSource';
return new NoneDataSource(this, name, {
api: this,
name: name,
description: options?.description,
});
}

/**
* add a new DynamoDB data source to this API
*
* @param table The DynamoDB table backing this data source
* @param options The optional configuration for this data source
* @default name - 'DynamoDbCDKDataSource'
* description - No description
*/
public addDynamoDbDataSource(table: ITable, options?: DataSourceOptions): DynamoDbDataSource {
const name = options?.name ?? 'DynamoDbCDKDataSource';
return new DynamoDbDataSource(this, name, {
api: this,
table,
name,
description: options?.description,
});
}

/**
* add a new http data source to this API
*
* @param endpoint The http endpoint
* @param options The optional configuration for this data source
* @default name - 'HttpCDKDataSource'
* description - No description
*/
public addHttpDataSource(endpoint: string, options?: DataSourceOptions): HttpDataSource {
const name = options?.name ?? 'HttpCDKDataSource';
return new HttpDataSource(this, name, {
api: this,
endpoint,
name,
description: options?.description,
});
}

/**
* add a new Lambda data source to this API
*
* @param lambdaFunction The Lambda function to call to interact with this data source
* @param options The optional configuration for this data source
* @default name - 'LambdaCDKDataSource'
* description - No description
*/
public addLambdaDataSource(lambdaFunction: IFunction, options?: DataSourceOptions): LambdaDataSource {
const name = options?.name ?? 'LambdaCDKDataSource';
return new LambdaDataSource(this, name, {
api: this,
lambdaFunction,
name,
description: options?.description,
});
}

/**
* Add schema dependency if not imported
*
* @param construct the dependee
*/
public addSchemaDependency(construct: CfnResource): boolean {
construct;
return false;
}
}
Loading