Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
public makePostResolver(baseURL: string, path: string, type: string, field: string, nonNullArgs: string[], headers: HttpHeader[]) {
const parsedHeaders = headers.map(header => qref(`$headers.put("${header.key}", "${header.value}")`));
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(HttpResourceIDs.HttpDataSourceID(baseURL), 'Name'),
FieldName: field,
TypeName: type,
RequestMappingTemplate: this.replaceEnv(
print(
compoundExpression([
nonNullArgs.length > 0 ? this.makeNonNullChecks(nonNullArgs) : null,
set(ref('headers'), ref('utils.http.copyHeaders($ctx.request.headers)')),
qref('$headers.put("Content-Type", "application/json")'),
qref('$headers.put("accept-encoding", "application/json")'),
...parsedHeaders,
HttpMappingTemplate.postRequest({
resourcePath: path,
params: obj({
body: ref('util.toJson($ctx.args.body)'),
public makePutResolver(baseURL: string, path: string, type: string, field: string, nonNullArgs: string[], headers: HttpHeader[]) {
const parsedHeaders = headers.map(header => qref(`$headers.put("${header.key}", "${header.value}")`));
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(HttpResourceIDs.HttpDataSourceID(baseURL), 'Name'),
FieldName: field,
TypeName: type,
RequestMappingTemplate: this.replaceEnv(
print(
compoundExpression([
nonNullArgs.length > 0 ? this.makeNonNullChecks(nonNullArgs) : null,
set(ref('headers'), ref('utils.http.copyHeaders($ctx.request.headers)')),
qref('$headers.put("Content-Type", "application/json")'),
qref('$headers.put("accept-encoding", "application/json")'),
...parsedHeaders,
HttpMappingTemplate.putRequest({
resourcePath: path,
params: obj({
body: ref('util.toJson($ctx.args.body)'),
public makeGetResolver(type: string, nameOverride?: string, isSyncEnabled: boolean = false, queryTypeName: string = 'Query') {
const fieldName = nameOverride ? nameOverride : graphqlName('get' + toUpper(type));
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(type), 'Name'),
FieldName: fieldName,
TypeName: queryTypeName,
RequestMappingTemplate: print(
DynamoDBMappingTemplate.getItem({
key: ifElse(
ref(ResourceConstants.SNIPPETS.ModelObjectKey),
raw(`$util.toJson(\$${ResourceConstants.SNIPPETS.ModelObjectKey})`),
obj({
id: ref('util.dynamodb.toDynamoDBJson($ctx.args.id)'),
}),
true
),
isSyncEnabled,
})
public makeSyncResolver(type: string, queryTypeName: string = 'Query') {
const fieldName = graphqlName('sync' + toUpper(plural(type)));
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(type), 'Name'),
FieldName: fieldName,
TypeName: queryTypeName,
RequestMappingTemplate: print(
DynamoDBMappingTemplate.syncItem({
filter: ifElse(ref('context.args.filter'), ref('util.transform.toDynamoDBFilterExpression($ctx.args.filter)'), nul()),
limit: ref('util.defaultIfNull($ctx.args.limit, 100)'),
lastSync: ref('util.toJson($util.defaultIfNull($ctx.args.lastSync, null))'),
nextToken: ref('util.toJson($util.defaultIfNull($ctx.args.nextToken, null))'),
})
),
ResponseMappingTemplate: print(DynamoDBMappingTemplate.dynamoDBResponse()),
});
}
/**
public blankResolver(type: string, field: string) {
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: 'NONE',
FieldName: field,
TypeName: type,
RequestMappingTemplate: print(
obj({
version: str('2017-02-28'),
payload: obj({}),
})
),
ResponseMappingTemplate: print(ref(`util.toJson($context.source.${field})`)),
});
}
public makePatchResolver(baseURL: string, path: string, type: string, field: string, nonNullArgs: string[], headers: HttpHeader[]) {
const parsedHeaders = headers.map(header => qref(`$headers.put("${header.key}", "${header.value}")`));
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(HttpResourceIDs.HttpDataSourceID(baseURL), 'Name'),
FieldName: field,
TypeName: type,
RequestMappingTemplate: this.replaceEnv(
print(
compoundExpression([
nonNullArgs.length > 0 ? this.makeNonNullChecks(nonNullArgs) : null,
set(ref('headers'), ref('utils.http.copyHeaders($ctx.request.headers)')),
qref('$headers.put("Content-Type", "application/json")'),
qref('$headers.put("accept-encoding", "application/json")'),
...parsedHeaders,
HttpMappingTemplate.patchRequest({
resourcePath: path,
params: obj({
body: ref('util.toJson($ctx.args.body)'),
public generateSubscriptionResolver(fieldName: string, subscriptionTypeName: string = 'Subscription') {
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: 'NONE',
FieldName: fieldName,
TypeName: subscriptionTypeName,
RequestMappingTemplate: print(
raw(`{
"version": "2018-05-29",
"payload": {}
}`)
),
ResponseMappingTemplate: print(raw(`$util.toJson(null)`)),
});
}
public makeListResolver(type: string, nameOverride?: string, isSyncEnabled: boolean = false, queryTypeName: string = 'Query') {
const fieldName = nameOverride ? nameOverride : graphqlName('list' + plurality(toUpper(type)));
const defaultPageLimit = 10;
const requestVariable = 'ListRequest';
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(type), 'Name'),
FieldName: fieldName,
TypeName: queryTypeName,
RequestMappingTemplate: print(
compoundExpression([
set(ref('limit'), ref(`util.defaultIfNull($context.args.limit, ${defaultPageLimit})`)),
set(
ref(requestVariable),
obj({
version: isSyncEnabled ? str('2018-05-29') : str('2017-02-28'),
limit: ref('limit'),
})
),
iff(ref('context.args.nextToken'), set(ref(`${requestVariable}.nextToken`), str('$context.args.nextToken'))),
iff(
resolver = (type: string, field: string, name: string, region?: string): any => {
return new AppSync.Resolver({
ApiId: Fn.Ref(ResourceConstants.PARAMETERS.AppSyncApiId),
TypeName: type,
FieldName: field,
Kind: 'PIPELINE',
PipelineConfig: {
Functions: [Fn.GetAtt(FunctionResourceIDs.FunctionAppSyncFunctionConfigurationID(name, region), 'FunctionId')],
},
RequestMappingTemplate: printBlock('Stash resolver specific context.')(
compoundExpression([qref(`$ctx.stash.put("typeName", "${type}")`), qref(`$ctx.stash.put("fieldName", "${field}")`), obj({})])
),
ResponseMappingTemplate: '$util.toJson($ctx.prev.result)',
}).dependsOn(FunctionResourceIDs.FunctionAppSyncFunctionConfigurationID(name, region));
};
function makeQueryResolver(definition: ObjectTypeDefinitionNode, directive: DirectiveNode, ctx: TransformerContext) {
const type = definition.name.value;
const directiveArgs: KeyArguments = getDirectiveArguments(directive);
const index = directiveArgs.name;
const fieldName = directiveArgs.queryField;
const queryTypeName = ctx.getQueryTypeName();
const defaultPageLimit = 10
const requestVariable = 'QueryRequest';
return new AppSync.Resolver({
ApiId: Fn.GetAtt(ResourceConstants.RESOURCES.GraphQLAPILogicalID, 'ApiId'),
DataSourceName: Fn.GetAtt(ModelResourceIDs.ModelTableDataSourceID(type), 'Name'),
FieldName: fieldName,
TypeName: queryTypeName,
RequestMappingTemplate: print(
compoundExpression([
setQuerySnippet(definition, directive, ctx),
set(ref('limit'),
ref(`util.defaultIfNull($context.args.limit, ${defaultPageLimit})`)),
set(
ref(requestVariable),
obj({
version: str('2017-02-28'),
operation: str('Query'),
limit: ref('limit'),
query: ref(ResourceConstants.SNIPPETS.ModelQueryExpression),