Skip to content

Commit

Permalink
feat: support js runtime for unit resolver
Browse files Browse the repository at this point in the history
  • Loading branch information
krml4913 committed Sep 16, 2023
1 parent 227d479 commit 41114b9
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 15 deletions.
62 changes: 62 additions & 0 deletions src/__tests__/resolvers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,68 @@ describe('Resolvers', () => {
`);
});

it('should generate JS Resources with specific code', () => {
const api = new Api(
given.appSyncConfig({
dataSources: {
myTable: {
name: 'myTable',
type: 'AMAZON_DYNAMODB',
config: { tableName: 'data' },
},
},
pipelineFunctions: {
getUser: {
name: 'getUser',
dataSource: 'myTable',
},
},
}),
plugin,
);
expect(
api.compileResolver({
type: 'Query',
kind: 'UNIT',
field: 'user',
dataSource: 'myTable',
code: 'resolvers/getUserFunction.js',
}),
).toMatchInlineSnapshot(`
Object {
"GraphQlResolverQueryuser": Object {
"DependsOn": Array [
"GraphQlSchema",
],
"Properties": Object {
"ApiId": Object {
"Fn::GetAtt": Array [
"GraphQlApi",
"ApiId",
],
},
"Code": "Content of resolvers/getUserFunction.js",
"DataSourceName": Object {
"Fn::GetAtt": Array [
"GraphQlDsmyTable",
"Name",
],
},
"FieldName": "user",
"Kind": "UNIT",
"MaxBatchSize": undefined,
"Runtime": Object {
"Name": "APPSYNC_JS",
"RuntimeVersion": "1.0.0",
},
"TypeName": "Query",
},
"Type": "AWS::AppSync::Resolver",
},
}
`);
});

it('should generate Resources with direct Lambda', () => {
const api = new Api(
given.appSyncConfig({
Expand Down
29 changes: 15 additions & 14 deletions src/resources/Resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,16 @@ export class Resolver {
FieldName: this.config.field,
};

const isJsResolver = !(
const isUnitJsResolver =
this.config.kind === 'UNIT' && 'code' in this.config;

const isPipelineJsResolver = !(
this.config.kind === 'UNIT' ||
'request' in this.config ||
'response' in this.config
);

if (!isJsResolver) {
if (!isUnitJsResolver && !isPipelineJsResolver) {
const requestMappingTemplates = this.resolveMappingTemplate('request');
if (requestMappingTemplates) {
Properties.RequestMappingTemplate = requestMappingTemplates;
Expand All @@ -47,6 +50,16 @@ export class Resolver {
if (responseMappingTemplate) {
Properties.ResponseMappingTemplate = responseMappingTemplate;
}
} else {
if (this.config.code) {
Properties.Code = this.resolveJsCode(this.config.code);
} else if (!this.config.code) {
Properties.Code = DEFAULT_JS_RESOLVERS;
}
Properties.Runtime = {
Name: 'APPSYNC_JS',
RuntimeVersion: '1.0.0',
};
}

if (this.config.caching) {
Expand Down Expand Up @@ -85,18 +98,6 @@ export class Resolver {
MaxBatchSize: this.config.maxBatchSize,
};
} else {
if (isJsResolver) {
if (this.config.code) {
Properties.Code = this.resolveJsCode(this.config.code);
} else if (!this.config.code) {
Properties.Code = DEFAULT_JS_RESOLVERS;
}
Properties.Runtime = {
Name: 'APPSYNC_JS',
RuntimeVersion: '1.0.0',
};
}

Properties = {
...Properties,
Kind: 'PIPELINE',
Expand Down
2 changes: 1 addition & 1 deletion src/types/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ export type BaseResolverConfig = {
type: string;
request?: string | false;
response?: string | false;
code?: string;
caching?:
| {
ttl?: number;
Expand All @@ -171,7 +172,6 @@ export type UnitResolverConfig = BaseResolverConfig & {

export type PipelineResolverConfig = BaseResolverConfig & {
kind?: 'PIPELINE';
code?: string;
functions: string[];
};

Expand Down

0 comments on commit 41114b9

Please sign in to comment.