Skip to content

Commit

Permalink
fix: update gql v2 custom transformer loading logic (#9252)
Browse files Browse the repository at this point in the history
* fix: update gql v2 custom transformer loading logic

* test: add custom transformer v2 test

Co-authored-by: Colin Ihrig <colihrig@amazon.com>
  • Loading branch information
cjihrig and cjihrig-aws committed Dec 13, 2021
1 parent beed4f9 commit f728b4b
Show file tree
Hide file tree
Showing 8 changed files with 90 additions and 5 deletions.
6 changes: 6 additions & 0 deletions packages/amplify-e2e-core/src/utils/transformConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,9 @@ export function getTransformConfig(projectRoot: string, apiName: string): Transf
const metaFilePath = path.join(projectRoot, 'amplify', 'backend', 'api', apiName, TRANSFORM_CONFIG_FILE_NAME);
return <TransformConfig>JSON.parse(fs.readFileSync(metaFilePath, 'utf8'));
}

export function setTransformConfig(projectRoot: string, apiName: string, config) {
const metaFilePath = path.join(projectRoot, 'amplify', 'backend', 'api', apiName, TRANSFORM_CONFIG_FILE_NAME);

fs.writeFileSync(metaFilePath, JSON.stringify(config, null, 2));
}
1 change: 1 addition & 0 deletions packages/amplify-e2e-tests/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"clean-e2e-resources": "ts-node ./src/cleanup-e2e-resources.ts"
},
"dependencies": {
"@aws-amplify/graphql-transformer-core": "0.15.0",
"amplify-cli-core": "2.4.3",
"amplify-e2e-core": "2.4.5",
"aws-amplify": "^4.2.8",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
type Todo @model @simple {
id: ID!
content: String
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
const { TransformerPluginBase } = require('@aws-amplify/graphql-transformer-core');

class SimpleCustomTransformer extends TransformerPluginBase {
constructor() {
super('simple-custom-transformer', 'directive @simple on OBJECT');
}

object() {} // Must be implemented.
}

module.exports = { default: SimpleCustomTransformer };
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import {
addApiWithoutSchema,
apiGqlCompile,
createNewProjectDir,
deleteProject,
deleteProjectDir,
getSchemaPath,
getTransformConfig,
initJSProjectWithProfile,
setTransformConfig,
updateApiSchema,
} from 'amplify-e2e-core';
import * as path from 'path';

describe('GraphQL transformer v2 - Custom transformers', () => {
let projRoot: string;
let projFolderName: string;

beforeEach(async () => {
projFolderName = 'graphqlv2customtransformer';
projRoot = await createNewProjectDir(projFolderName);
});

afterEach(async () => {
try {
await deleteProject(projRoot);
} catch (_) {
// No-op.
}

deleteProjectDir(projRoot);
});

it('create a project including a custom transformer', async () => {
const projName = 'v2customtransformer';
const schemaDir = 'custom_transformers';
const schemaFile = 'simple_custom_model.graphql';
const schemaName = path.join(schemaDir, schemaFile);
const schemaPath = getSchemaPath(schemaName);
const customTransformerName = 'simple_custom_transformer.js';
const customTransformerPath = schemaPath.replace(schemaFile, customTransformerName);

await initJSProjectWithProfile(projRoot, { name: projName });
await addApiWithoutSchema(projRoot);
await updateApiSchema(projRoot, projName, schemaName);

// GQL compile should fail initially because the transformer does
// not know about the custom directive.
await expect(async () => {
await apiGqlCompile(projRoot);
}).rejects.toThrow();

// Add the custom directive.
const config = getTransformConfig(projRoot, projName);
config.transformers ??= [];
config.transformers.push(customTransformerPath);
setTransformConfig(projRoot, projName, config);

// GQL compile should pass now because the transformer knows about
// the custom directive.
await apiGqlCompile(projRoot);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,4 @@ export interface TransformerProjectConfig {
resolvers: Record<string, string>;
stacks: Record<string, Template>;
config: TransformConfig;
// Custom transformer plugins
transformers?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,7 @@ export interface TransformConfig {
[option: string]: any;
};
};

// Custom transformer plugins
transformers?: string[];
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,8 @@ function getTransformerFactory(
}

const customTransformersConfig = await readTransformerConfiguration(resourceDir);
const customTransformers = (
customTransformersConfig && customTransformersConfig.transformers ? customTransformersConfig.transformers : []
)
const customTransformerList = customTransformersConfig?.config?.transformers;
const customTransformers = (Array.isArray(customTransformerList) ? customTransformerList : [])
.map(transformer => {
const fileUrlMatch = /^file:\/\/(.*)\s*$/m.exec(transformer);
const modulePath = fileUrlMatch ? fileUrlMatch[1] : transformer;
Expand Down

0 comments on commit f728b4b

Please sign in to comment.