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

refactor(appsync): abstract schema stringify to utility functions #10019

Merged
merged 6 commits into from
Aug 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
99 changes: 94 additions & 5 deletions packages/@aws-cdk/aws-appsync/lib/private.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
function concatAndDedup<T>(left: T[], right: T[]): T[] {
return left.concat(right).filter((elem, index, self) => {
return index === self.indexOf(elem);
});
}
import { Directive } from './schema-base';
import { InterfaceType } from './schema-intermediate';

/**
* Utility enum for Schema class
Expand All @@ -12,6 +9,69 @@ export enum SchemaMode {
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[];
}

/**
* 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 {
BryanPan342 marked this conversation as resolved.
Show resolved Hide resolved
const typeName = (): string => { return options.name ? ` ${options.name}` : ''; };
const interfaces = generateInterfaces(options.interfaceTypes);
const directives = generateDirectives(options.directives);
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 @@ -118,4 +178,33 @@ export class Between extends BaseKeyCondition {
public args(): string[] {
return [this.arg1, this.arg2];
}
}

function concatAndDedup<T>(left: T[], right: T[]): T[] {
return left.concat(right).filter((elem, index, self) => {
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, -1);
}

/**
* Utility function to generate directives
*
* @param directives the directives of a given type
* @param delimiter the separator betweeen directives (by default we will add a space)
*/
function generateDirectives(directives?: Directive[], delimiter?: string): string {
if (!directives || directives.length === 0) return '';
return directives.reduce((acc, directive) =>
`${acc}${directive.statement}${delimiter ?? ' '}`, ' ').slice(0, -1);
}
50 changes: 13 additions & 37 deletions packages/@aws-cdk/aws-appsync/lib/schema-intermediate.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { shapeAddition } from './private';
import { Resolver } from './resolver';
import { Directive, IField, IIntermediateType } from './schema-base';
import { BaseTypeOptions, GraphqlType, ResolvableFieldOptions } from './schema-field';
Expand Down Expand Up @@ -59,13 +60,12 @@ export class InterfaceType implements IIntermediateType {
* Generate the string of this object type
*/
public toString(): string {
let schemaAddition = `interface ${this.name} {\n`;
Object.keys(this.definition).forEach( (key) => {
const attribute = this.definition[key];
const args = attribute.argsToString();
schemaAddition = `${schemaAddition} ${key}${args}: ${attribute.toString()}\n`;
return shapeAddition({
prefix: 'interface',
name: this.name,
fields: Object.keys(this.definition).map((key) =>
`${key}${this.definition[key].argsToString()}: ${this.definition[key].toString()}`),
});
return `${schemaAddition}}`;
}

/**
Expand Down Expand Up @@ -159,38 +159,14 @@ export class ObjectType extends InterfaceType implements IIntermediateType {
* Generate the string of this object type
*/
public toString(): string {
let title = this.name;
if (this.interfaceTypes && this.interfaceTypes.length) {
title = `${title} implements`;
this.interfaceTypes.map((interfaceType) => {
title = `${title} ${interfaceType.name},`;
});
title = title.slice(0, -1);
}
const directives = this.generateDirectives(this.directives);
let schemaAddition = `type ${title} ${directives}{\n`;
Object.keys(this.definition).forEach( (key) => {
const attribute = this.definition[key];
const args = attribute.argsToString();
schemaAddition = `${schemaAddition} ${key}${args}: ${attribute.toString()}\n`;
});
return `${schemaAddition}}`;
}

/**
* Utility function to generate directives
*
* @param directives the directives of a given type
* @param delimiter the separator betweeen directives
* @default - ' '
*/
private generateDirectives(directives?: Directive[], delimiter?: string): string {
let schemaAddition = '';
if (!directives) { return schemaAddition; }
directives.map((directive) => {
schemaAddition = `${schemaAddition}${directive.statement}${delimiter ?? ' '}`;
return shapeAddition({
prefix: 'type',
name: this.name,
interfaceTypes: this.interfaceTypes,
directives: this.directives,
fields: Object.keys(this.definition).map((key) =>
`${key}${this.definition[key].argsToString()}: ${this.definition[key].toString()}`),
});
return schemaAddition;
}

/**
Expand Down