Skip to content

Commit

Permalink
chore(appsync): removes codefirst schema generation (#23250)
Browse files Browse the repository at this point in the history
Removes all of the code first schema generation code in favor of this living in a separate module. This will make stabilizing the other constructs easier while allowing for continuing development of high level utilities within the `aws-cdk-appsync-utilities` library.

BREAKING CHANGE: Renames `Schema` to `SchemaFile` that implements `ISchema`. Removes all `addXxx` type methods from `GraphQlApi`.


----

### All Submissions:

* [ ] Have you followed the guidelines in our [Contributing guide?](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md)

### Adding new Construct Runtime Dependencies:

* [ ] This PR adds new construct runtime dependencies following the process described [here](https://github.com/aws/aws-cdk/blob/main/CONTRIBUTING.md/#adding-construct-runtime-dependencies)

### New Features

* [ ] Have you added the new feature to an [integration test](https://github.com/aws/aws-cdk/blob/main/INTEGRATION_TESTS.md)?
	* [ ] Did you use `yarn integ` to deploy the infrastructure and generate the snapshot (i.e. `yarn integ` without `--dry-run`)?

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
MrArnoldPalmer committed Dec 7, 2022
1 parent bb0de2b commit 2bd1e41
Show file tree
Hide file tree
Showing 43 changed files with 139 additions and 4,408 deletions.
568 changes: 12 additions & 556 deletions packages/@aws-cdk/aws-appsync/README.md

Large diffs are not rendered by default.

82 changes: 5 additions & 77 deletions packages/@aws-cdk/aws-appsync/lib/graphqlapi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { ArnFormat, CfnResource, Duration, Expiration, IResolvable, Stack } from
import { Construct } from 'constructs';
import { CfnApiKey, CfnGraphQLApi, CfnGraphQLSchema, CfnDomainName, CfnDomainNameApiAssociation } from './appsync.generated';
import { IGraphqlApi, GraphqlApiBase } from './graphqlapi-base';
import { Schema } from './schema';
import { IIntermediateType } from './schema-base';
import { ResolvableField } from './schema-field';
import { ObjectType } from './schema-intermediate';
import { ISchema } from './schema';

/**
* enum with all possible values for AppSync authorization type
Expand Down Expand Up @@ -307,7 +304,7 @@ export interface GraphqlApiProps {
* @default - schema will be generated code-first (i.e. addType, addObjectType, etc.)
*
*/
readonly schema?: Schema;
readonly schema: ISchema;
/**
* A flag indicating whether or not X-Ray tracing is enabled for the GraphQL API.
*
Expand Down Expand Up @@ -456,7 +453,7 @@ export class GraphqlApi extends GraphqlApiBase {
/**
* the schema attached to this api
*/
public readonly schema: Schema;
public readonly schema: ISchema;

/**
* The Authorization Types for this GraphQL Api
Expand Down Expand Up @@ -507,8 +504,8 @@ export class GraphqlApi extends GraphqlApiBase {
this.arn = this.api.attrArn;
this.graphqlUrl = this.api.attrGraphQlUrl;
this.name = this.api.name;
this.schema = props.schema ?? new Schema();
this.schemaResource = this.schema.bind(this);
this.schema = props.schema;
this.schemaResource = new CfnGraphQLSchema(this, 'Schema', this.schema.bind(this));

if (props.domainName) {
this.domainNameResource = new CfnDomainName(this, 'DomainName', {
Expand Down Expand Up @@ -707,75 +704,6 @@ export class GraphqlApi extends GraphqlApiBase {
});
}

/**
* Escape hatch to append to Schema as desired. Will always result
* in a newline.
*
* @param addition the addition to add to schema
* @param delimiter the delimiter between schema and addition
* @default - ''
*
*/
public addToSchema(addition: string, delimiter?: string): void {
this.schema.addToSchema(addition, delimiter);
}

/**
* Add type to the schema
*
* @param type the intermediate type to add to the schema
*
*/
public addType(type: IIntermediateType): IIntermediateType {
return this.schema.addType(type);
}

/**
* Add a query field to the schema's Query. CDK will create an
* Object Type called 'Query'. For example,
*
* type Query {
* fieldName: Field.returnType
* }
*
* @param fieldName the name of the query
* @param field the resolvable field to for this query
*/
public addQuery(fieldName: string, field: ResolvableField): ObjectType {
return this.schema.addQuery(fieldName, field);
}

/**
* Add a mutation field to the schema's Mutation. CDK will create an
* Object Type called 'Mutation'. For example,
*
* type Mutation {
* fieldName: Field.returnType
* }
*
* @param fieldName the name of the Mutation
* @param field the resolvable field to for this Mutation
*/
public addMutation(fieldName: string, field: ResolvableField): ObjectType {
return this.schema.addMutation(fieldName, field);
}

/**
* Add a subscription field to the schema's Subscription. CDK will create an
* Object Type called 'Subscription'. For example,
*
* type Subscription {
* fieldName: Field.returnType
* }
*
* @param fieldName the name of the Subscription
* @param field the resolvable field to for this Subscription
*/
public addSubscription(fieldName: string, field: ResolvableField): ObjectType {
return this.schema.addSubscription(fieldName, field);
}


/**
* The AppSyncDomainName of the associated custom domain
*/
Expand Down
3 changes: 0 additions & 3 deletions packages/@aws-cdk/aws-appsync/lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,5 @@ export * from './data-source';
export * from './mapping-template';
export * from './resolver';
export * from './schema';
export * from './schema-intermediate';
export * from './schema-field';
export * from './schema-base';
export * from './graphqlapi';
export * from './graphqlapi-base';
124 changes: 0 additions & 124 deletions packages/@aws-cdk/aws-appsync/lib/private.ts
Original file line number Diff line number Diff line change
@@ -1,85 +1,3 @@
import { AuthorizationType } from './graphqlapi';
import { Directive } from './schema-base';
import { InterfaceType } from './schema-intermediate';

/**
* Utility enum for Schema class
*/
export enum SchemaMode {
FILE = 'FILE',
CODE = 'CODE',
};

/**
* Generates an addition to the schema
*
* ```
* prefix name interfaces directives {
* field
* field
* ...
* }
* ```
*/
export interface SchemaAdditionOptions {
/**
* the prefix for this additon (type, interface, enum, input, schema)
*/
readonly prefix: string;
/**
* the name for this addition (some additions dont need this [i.e. schema])
*
* @default - no name
*/
readonly name?: string;
/**
* the interface types if this is creating an object type
*
* @default - no interfaces
*/
readonly interfaceTypes?: InterfaceType[];
/**
* the directives for this type
*
* @default - no directives
*/
readonly directives?: Directive[];
/**
* the fields to reduce onto the addition
*/
readonly fields: string[];
/**
* the authorization modes for this graphql type
*/
readonly modes?: AuthorizationType[];
}

/**
* Generates an addition to the schema
*
* @param options the options to produced a stringfied addition
*
* @returns the following shape:
*
* ```
* prefix name interfaces directives {
* field
* field
* ...
* }
* ```
*/
export function shapeAddition(options: SchemaAdditionOptions): string {
const typeName = (): string => { return options.name ? ` ${options.name}` : ''; };
const interfaces = generateInterfaces(options.interfaceTypes);
const directives = generateDirectives({
directives: options.directives,
modes: options.modes,
});
return options.fields.reduce((acc, field) =>
`${acc} ${field}\n`, `${options.prefix}${typeName()}${interfaces}${directives} {\n`) + '}';
}

/**
* Utility class to represent DynamoDB key conditions.
*/
Expand Down Expand Up @@ -193,45 +111,3 @@ function concatAndDedup<T>(left: T[], right: T[]): T[] {
return index === self.indexOf(elem);
});
}

/**
* Utility function to generate interfaces for object types
*
* @param interfaceTypes the interfaces this object type implements
*/
function generateInterfaces(interfaceTypes?: InterfaceType[]): string {
if (!interfaceTypes || interfaceTypes.length === 0) return '';
return interfaceTypes.reduce((acc, interfaceType) =>
`${acc} ${interfaceType.name} &`, ' implements').slice(0, -2);
}

/**
* options to generate directives
*/
interface generateDirectivesOptions {
/**
* the directives of a given type
*/
readonly directives?: Directive[];
/**
* thee separator betweeen directives
*
* @default - a space
*/
readonly delimiter?: string;
/**
* the authorization modes
*/
readonly modes?: AuthorizationType[];
}

/**
* Utility function to generate directives
*/
function generateDirectives(options: generateDirectivesOptions): string {
if (!options.directives || options.directives.length === 0) return '';
// reduce over all directives and get string version of the directive
// pass in the auth modes for checks to happen on compile time
return options.directives.reduce((acc, directive) =>
`${acc}${directive._bindToAuthModes(options.modes).toString()}${options.delimiter ?? ' '}`, ' ').slice(0, -1);
}
Loading

0 comments on commit 2bd1e41

Please sign in to comment.