Skip to content

Commit

Permalink
feat(amplify-provider-awscloudformation): merge user config with tran…
Browse files Browse the repository at this point in the history
…sform generated resolvers
  • Loading branch information
danielleadams committed Sep 29, 2021
1 parent b122947 commit ace39d5
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 2 deletions.
5 changes: 5 additions & 0 deletions packages/amplify-e2e-core/src/utils/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,8 @@ export function updateConfig(projectDir: string, projectName: string, config: an
const configPath = path.join(projectDir, 'amplify', 'backend', 'api', projectName, TRANSFORM_CONFIG_FILE_NAME);
fs.writeFileSync(configPath, JSON.stringify(config, null, 4));
}

export function addCustomResolver(projectDir: string, projectName: string, resolverName: string, resolver: string) {
const resolverPath = path.join(projectDir, 'amplify', 'backend', 'api', projectName, 'resolvers', resolverName);
fs.writeFileSync(resolverPath, resolver);
}
46 changes: 46 additions & 0 deletions packages/amplify-e2e-tests/src/__tests__/resolvers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import {
initJSProjectWithProfile,
deleteProject,
createNewProjectDir,
deleteProjectDir,
addFeatureFlag,
addApiWithSchema,
amplifyPush,
addCustomResolver,
apiGqlCompile,
} from 'amplify-e2e-core';
import { join } from 'path';
import * as fs from 'fs-extra';

describe('overriding generated resolvers', () => {
let projectDir: string;
let apiName = 'simpleapi';

beforeAll(async () => {
projectDir = await createNewProjectDir('overrideresolvers');
await initJSProjectWithProfile(projectDir, {});

addFeatureFlag(projectDir, 'graphqltransformer', 'useexperimentalpipelinedtransformer', true);
});

afterAll(async () => {
await deleteProject(projectDir);
deleteProjectDir(projectDir);
});

it('adds the overwritten resolver to the build', async () => {
const resolverName = 'Query.listTodos.req.vtl';
const resolver = '$util.unauthorized()';
const generatedResolverPath = join(projectDir, 'amplify', 'backend', 'api', apiName, 'build', 'pipelineFunctions', resolverName);

await addApiWithSchema(projectDir, 'simple_model.graphql');
await apiGqlCompile(projectDir, true);

expect(fs.readFileSync(generatedResolverPath)).not.toContain(resolver);

addCustomResolver(projectDir, apiName, resolverName, resolver);
await apiGqlCompile(projectDir, true);

expect(fs.readFileSync(generatedResolverPath)).toEqual(resolver);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import { mergeUserConfigWithTransformOutput } from '../../graphql-transformer/utils';
import { TransformerProjectConfig, DeploymentResources } from '@aws-amplify/graphql-transformer-core';

describe('mergeUserConfigWithTransformOutput', () => {
let userConfig;
let transformerOutput;

beforeAll(() => {
transformerOutput = {
resolvers: {},
pipelineFunctions: {
'Query.listTodos.req.vtl': '## [Start] List Request. **\n' + '#set( $limit = $util.defaultIfNull($context.args.limit, 100) )\n',
},
functions: {},
schema: '',
stackMapping: {},
stacks: {},
rootStack: null,
} as DeploymentResources;
});

describe('has user-created resolvers', () => {
beforeAll(() => {
userConfig = {
schema: '',
functions: {},
pipelineFunctions: {},
resolvers: {
'Query.listTodos.req.vtl': '$util.unauthorized\n',
},
stacks: {},
config: { Version: 5, ElasticsearchWarning: true },
} as TransformerProjectConfig;
});

it('merges the custom resolver with transformer output', () => {
const output = mergeUserConfigWithTransformOutput(userConfig, transformerOutput);

expect(output.pipelineFunctions['Query.listTodos.req.vtl']).toEqual('$util.unauthorized\n');
});
});

describe('has user created pipeline function', () => {
beforeAll(() => {
userConfig = {
schema: '',
functions: {},
pipelineFunctions: {
'Query.listTodos.req.vtl': '$util.unauthorized\n',
},
resolvers: {},
stacks: {},
config: { Version: 5, ElasticsearchWarning: true },
} as TransformerProjectConfig;
});

it('merges custom pipeline function with transformer output', () => {
const output = mergeUserConfigWithTransformOutput(userConfig, transformerOutput);

expect(output.pipelineFunctions['Query.listTodos.req.vtl']).toEqual('$util.unauthorized\n');
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ import {
import { SearchableModelTransformer } from '@aws-amplify/graphql-searchable-transformer';
import { ProviderName as providerName } from '../constants';
import { hashDirectory } from '../upload-appsync-files';
import { writeDeploymentToDisk } from './utils';
import { mergeUserConfigWithTransformOutput, writeDeploymentToDisk } from './utils';
import { loadProject as readTransformerConfiguration } from './transform-config';
import { loadProject } from 'graphql-transformer-core';
import { AppSyncAuthConfiguration } from '@aws-amplify/graphql-transformer-core';
Expand Down Expand Up @@ -432,5 +432,8 @@ async function _buildProject(opts: ProjectOptions<TransformerFactoryArgs>) {
stacks: opts.projectConfig.stacks || {},
featureFlags: new AmplifyCLIFeatureFlagAdapter(),
});
return transform.transform(userProjectConfig.schema.toString());

const transformOutput = transform.transform(userProjectConfig.schema.toString());

return mergeUserConfigWithTransformOutput(userProjectConfig, transformOutput);
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,20 @@ export function readFromPath(directory: string): any {
return accum;
}

export function mergeUserConfigWithTransformOutput(userConfig: any, transformOutput: any) {
const userResolvers = userConfig.resolvers || {};
const userPipelineFunctions = userConfig.pipelineFunctions || {};
const pipelineFunctions = transformOutput.pipelineFunctions;

for (const userResolver of Object.keys(userResolvers)) pipelineFunctions[userResolver] = userResolvers[userResolver];
for (const userResolver of Object.keys(userPipelineFunctions)) pipelineFunctions[userResolver] = userPipelineFunctions[userResolver];

return {
...transformOutput,
pipelineFunctions,
};
}

/**
* Writes a deployment to disk at a path.
*/
Expand Down

0 comments on commit ace39d5

Please sign in to comment.