Skip to content

Commit

Permalink
fix: sub "_" with hash in resource logical ID in transformer v2 (#8600)
Browse files Browse the repository at this point in the history
* fix: generate resource names with _ correctly

* chore: clean up test code

* test: update test after merge from master
  • Loading branch information
edwardfoyle committed Nov 2, 2021
1 parent bf843b9 commit 6bb620b
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
3 changes: 2 additions & 1 deletion packages/amplify-graphql-transformer-core/package.json
Expand Up @@ -23,7 +23,8 @@
"scripts": {
"build": "tsc",
"watch": "tsc -w",
"clean": "rimraf ./lib"
"clean": "rimraf ./lib",
"test": "jest"
},
"dependencies": {
"@aws-amplify/graphql-transformer-interfaces": "1.10.0",
Expand Down
@@ -0,0 +1,39 @@
import { DefaultTransformHost } from '../transform-host';
import { GraphQLApi, TransformerAPIProps } from '../graphql-api';
import { App } from '@aws-cdk/core';
import { InlineTemplate } from '../cdk-compat/template-asset';
import { TransformerRootStack } from '../cdk-compat/root-stack';

describe('addResolver', () => {
const app = new App();
const stack = new TransformerRootStack(app, 'test-root-stack');
const transformHost = new DefaultTransformHost({ api: new GraphQLApi(stack, 'testId', { name: 'testApiName' }) });

it('generates resolver name with hash for non-alphanumeric type names', () => {
const cfnResolver = transformHost.addResolver(
'test_type',
'testField',
new InlineTemplate('testTemplate'),
new InlineTemplate('testTemplate'),
undefined,
undefined,
['testPipelineConfig'],
stack,
);
expect(cfnResolver.logicalId).toMatch('testtype4c79TestFieldResolver.LogicalID'); // have to use match instead of equals because the logicalId is a CDK token that has some non-deterministic stuff in it
});

it('generates resolver name with hash for non-alphanumeric field names', () => {
const cfnResolver = transformHost.addResolver(
'testType',
'test_field',
new InlineTemplate('testTemplate'),
new InlineTemplate('testTemplate'),
undefined,
undefined,
['testPipelineConfig'],
stack,
);
expect(cfnResolver.logicalId).toMatch('testTypeTestfield6a0fResolver.LogicalID'); // have to use match instead of equals because the logicalId is a CDK token that has some non-deterministic stuff in it
});
});
Expand Up @@ -17,7 +17,7 @@ import { CfnFunction, Code, Function, IFunction, ILayerVersion, Runtime } from '
import { AppSyncFunctionConfiguration } from './appsync-function';
import { IRole } from '@aws-cdk/aws-iam';
import { InlineTemplate, S3MappingFunctionCode } from './cdk-compat/template-asset';
import { ResolverResourceIDs, toCamelCase } from 'graphql-transformer-common';
import { ResolverResourceIDs, resourceName, toCamelCase } from 'graphql-transformer-common';
import { GraphQLApi } from './graphql-api';

export interface DefaultTransformHostOptions {
Expand Down Expand Up @@ -144,7 +144,7 @@ export class DefaultTransformHost implements TransformHostProvider {

const requestTemplateLocation = requestMappingTemplate.bind(this.api);
const responseTemplateLocation = responseMappingTemplate.bind(this.api);
const resolverName = toCamelCase([typeName, fieldName, 'Resolver']);
const resolverName = toCamelCase([resourceName(typeName), resourceName(fieldName), 'Resolver']);
const resourceId = resolverLogicalId ?? ResolverResourceIDs.ResolverResourceID(typeName, fieldName);

if (dataSourceName) {
Expand Down

0 comments on commit 6bb620b

Please sign in to comment.