Skip to content

Commit

Permalink
fix: allow specifying outputpath for resolvers and schema (#356)
Browse files Browse the repository at this point in the history
  • Loading branch information
kcwinner authored Jan 8, 2022
1 parent 102959c commit 7481982
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .npmignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ const project = new awscdk.AwsCdkConstructLibrary({
// Ignore our generated appsync files
npmignore: [
'appsync/*',
'customtest/*',
],
gitignore: [
'appsync/*',
'customtest/*',
],

// Jsii packaging
Expand Down
2 changes: 2 additions & 0 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ new AppSyncTransformer(scope: Construct, id: string, props: AppSyncTransformerPr
* **enableDynamoPointInTimeRecovery** (<code>boolean</code>) Whether to enable dynamo Point In Time Recovery. __*Default*__: false
* **fieldLogLevel** (<code>[FieldLogLevel](#aws-cdk-aws-appsync-fieldloglevel)</code>) Optional. __*Default*__: FieldLogLevel.NONE
* **nestedStackName** (<code>string</code>) Specify a custom nested stack name. __*Default*__: "appsync-nested-stack"
* **outputPath** (<code>string</code>) Path where generated resolvers are output. __*Default*__: "./appsync"
* **postCdkTransformers** (<code>Array<any></code>) Optional. __*Default*__: undefined
* **preCdkTransformers** (<code>Array<any></code>) Optional. __*Default*__: undefined
* **syncEnabled** (<code>boolean</code>) Whether to enable Amplify DataStore and Sync Tables. __*Default*__: false
Expand Down Expand Up @@ -179,6 +180,7 @@ Name | Type | Description
**enableDynamoPointInTimeRecovery**?🔹 | <code>boolean</code> | Whether to enable dynamo Point In Time Recovery.<br/>__*Default*__: false
**fieldLogLevel**?🔹 | <code>[FieldLogLevel](#aws-cdk-aws-appsync-fieldloglevel)</code> | Optional.<br/>__*Default*__: FieldLogLevel.NONE
**nestedStackName**?🔹 | <code>string</code> | Specify a custom nested stack name.<br/>__*Default*__: "appsync-nested-stack"
**outputPath**?🔹 | <code>string</code> | Path where generated resolvers are output.<br/>__*Default*__: "./appsync"
**postCdkTransformers**?🔹 | <code>Array<any></code> | Optional.<br/>__*Default*__: undefined
**preCdkTransformers**?🔹 | <code>Array<any></code> | Optional.<br/>__*Default*__: undefined
**syncEnabled**?🔹 | <code>boolean</code> | Whether to enable Amplify DataStore and Sync Tables.<br/>__*Default*__: false
Expand Down
10 changes: 9 additions & 1 deletion src/appsync-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as fs from 'fs';
import * as path from 'path';
import {
GraphqlApi,
AuthorizationType,
Expand Down Expand Up @@ -46,6 +47,12 @@ export interface AppSyncTransformerProps {
*/
readonly schemaPath: string;

/**
* Path where generated resolvers are output
* @default "./appsync"
*/
readonly outputPath?: string;

/**
* Optional. {@link AuthorizationConfig} type defining authorization for AppSync GraphqlApi. Defaults to API_KEY
* @default API_KEY authorization config
Expand Down Expand Up @@ -199,6 +206,7 @@ export class AppSyncTransformer extends Construct {

const transformerConfiguration: SchemaTransformerProps = {
schemaPath: props.schemaPath,
outputPath: props.outputPath,
syncEnabled: props.syncEnabled ?? false,
customVtlTransformerRootDirectory: props.customVtlTransformerRootDirectory,
};
Expand Down Expand Up @@ -267,7 +275,7 @@ export class AppSyncTransformer extends Construct {
? props.fieldLogLevel
: FieldLogLevel.NONE,
},
schema: Schema.fromAsset('./appsync/schema.graphql'),
schema: Schema.fromAsset(path.join(transformer.outputPath, 'schema.graphql')),
xrayEnabled: props.xrayEnabled ?? false,
});

Expand Down
20 changes: 10 additions & 10 deletions src/transformer/schema-transformer.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import * as fs from 'fs';
import { normalize, join } from 'path';
import * as path from 'path';
import { ModelAuthTransformer, ModelAuthTransformerConfig } from 'graphql-auth-transformer';
import { ModelConnectionTransformer } from 'graphql-connection-transformer';
import { DynamoDBModelTransformer } from 'graphql-dynamodb-transformer';
Expand Down Expand Up @@ -176,7 +176,7 @@ export class SchemaTransformer {
*/
public getResolvers() {
const statements = ['Query', 'Mutation'];
const resolversDirPath = normalize('./appsync/resolvers');
const resolversDirPath = path.normalize(path.join(this.outputPath, 'resolvers'));
if (fs.existsSync(resolversDirPath)) {
const files = fs.readdirSync(resolversDirPath);
files.forEach(file => {
Expand All @@ -194,7 +194,7 @@ export class SchemaTransformer {
if (!this.outputs.noneResolvers || !this.outputs.noneResolvers[compositeKey]) compositeKey = fieldName;
}

let filepath = normalize(`${resolversDirPath}/${file}`);
let filepath = path.normalize(path.join(resolversDirPath, file));

if (statements.indexOf(typeName) >= 0 || (this.outputs.noneResolvers && this.outputs.noneResolvers[compositeKey])) {
if (!this.resolvers[compositeKey]) {
Expand Down Expand Up @@ -270,7 +270,7 @@ export class SchemaTransformer {
*/
private writeSchema(schema: any) {
if (!fs.existsSync(this.outputPath)) {
fs.mkdirSync(this.outputPath);
fs.mkdirSync(this.outputPath, { recursive: true });
}

fs.writeFileSync(`${this.outputPath}/schema.graphql`, schema);
Expand All @@ -282,24 +282,24 @@ export class SchemaTransformer {
*/
private writeResolversToFile(resolvers: any) {
if (!fs.existsSync(this.outputPath)) {
fs.mkdirSync(this.outputPath);
fs.mkdirSync(this.outputPath, { recursive: true });
}

const resolverFolderPath = normalize(this.outputPath + '/resolvers');
const resolverFolderPath = path.normalize(path.join(this.outputPath, 'resolvers'));
if (fs.existsSync(resolverFolderPath)) {
const files = fs.readdirSync(resolverFolderPath);
files.forEach(file => fs.unlinkSync(resolverFolderPath + '/' + file));
fs.rmdirSync(resolverFolderPath);
}

if (!fs.existsSync(resolverFolderPath)) {
fs.mkdirSync(resolverFolderPath);
fs.mkdirSync(resolverFolderPath, { recursive: true });
}

Object.keys(resolvers).forEach((key: any) => {
const resolver = resolvers[key];
const fileName = key.replace('.vtl', '');
const resolverFilePath = normalize(`${resolverFolderPath}/${fileName}`);
const resolverFilePath = path.normalize(path.join(resolverFolderPath, fileName));
fs.writeFileSync(resolverFilePath, resolver);
});
}
Expand All @@ -319,10 +319,10 @@ export class SchemaTransformer {
},
};

const configDir = join(__dirname, '..', '..', projectDir);
const configDir = path.join(__dirname, '..', '..', projectDir);

try {
const configPath = join(configDir, TRANSFORM_CONFIG_FILE_NAME);
const configPath = path.join(configDir, TRANSFORM_CONFIG_FILE_NAME);
const configExists = fs.existsSync(configPath);
if (configExists) {
const configStr = fs.readFileSync(configPath);
Expand Down
30 changes: 30 additions & 0 deletions test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1215,4 +1215,34 @@ test('Can Set Custom Directory', () => {
xrayEnabled: false,
customVtlTransformerRootDirectory: customDir,
});
});

test('Can set custom output path', () => {
const mockApp = new App();
const stack = new Stack(mockApp, 'custom-vtl-stack');

const appsyncTransformer = new AppSyncTransformer(stack, 'custom-vtl-transformer', {
schemaPath: customVtlTestSchemaPath,
outputPath: './customtest/appsync',
authorizationConfig: apiKeyAuthorizationConfig,
xrayEnabled: false,
});

expect(appsyncTransformer.resolvers).toMatchObject({
QuerylistThingCustom: {
typeName: 'Query',
fieldName: 'listThingCustom',
requestMappingTemplate: 'customtest/appsync/resolvers/Query.listThingCustom.req',
responseMappingTemplate: 'customtest/appsync/resolvers/Query.listThingCustom.res',
},
});

expect(appsyncTransformer.nestedAppsyncStack).toHaveResourceLike('AWS::AppSync::Resolver', {
FieldName: 'listThingCustom',
TypeName: 'Query',
DataSourceName: 'NONE',
Kind: 'UNIT',
RequestMappingTemplate: '{\n "version": "2018-05-29"\n}',
ResponseMappingTemplate: '$util.toJson({})',
});
});

0 comments on commit 7481982

Please sign in to comment.