Skip to main content

Class: Parser

Resolver and function parser.

import { Parser } from 'aws-appsync-butler';

const parser = new Parser();
parser.readTypes(); // ['Query', 'Mutation', 'Post']
parser.readFields('Query'); // ['getPost', 'getAuthor']
const resolver = parser.parseResolver('Query', 'getPost');

parser.readFunctions(); // ['getPostById', 'getAuthorByPostId']
const func = reader.parseFunction('getPostById');

Hierarchy

Constructors

constructor

โ€ข new Parser(optionsOrRoot?)

Parameters

NameTypeDescription
optionsOrRootstring | ParserOptionsPath to VTL directory or parsing directives.

Overrides

Reader.constructor

Methods

setVariable

โ–ธ setVariable(key, value): Parser

Sets an in-memory variable for VTL file parsing.

vtl/resolvers/Mutation/addPost/request.vtl
#set($postId = $util.autoId())

{
"version" : "2018-05-29",
"operation" : "BatchPutItem",
"tables": {
"{{ tableName }}": [
{
"pk": $util.dynamodb.toDynamoDBJson("p-$postId"),
"sk": $util.dynamodb.toDynamoDBJson("A"),
"postTitle": $util.dynamodb.toDynamoDBJson($ctx.args.postInput.title),
"postContent": $util.dynamodb.toDynamoDBJson($ctx.args.postInput.content)
},
{
"pk": $util.dynamodb.toDynamoDBJson("u-$ctx.identity.sub"),
"sk": $util.dynamodb.toDynamoDBJson("p-$postId")
}
]
}
}
parser.setVariable("tableName", dynamodbTable.tableName);
const resolver = parser.parseResolver('Mutation', 'addPost');

Parameters

NameTypeDescription
keystringVariable identifier
valuestringValue

Returns

Parser


getVariable

โ–ธ getVariable(key): undefined | string

Retrieves the defined variable value.

parser.setVariable('name', 'Ali');
parser.getVariable('name'); // 'Ali'

Parameters

NameTypeDescription
keystringVariable identifier

Returns

undefined | string

Variable value if set, undefined otherwise.


parseResolver

โ–ธ parseResolver(typeName, fieldName): ParsedResolverInfo

Parses Resolver files including variable substitution in mapping templates, pipeline sequence defintion parsing, and data source binding.

Parameters

NameTypeDescription
typeNamestringGraphQL type name
fieldNamestringGraphQL field name

Returns

ParsedResolverInfo

Parsed resolver information


parseFunction

โ–ธ parseFunction(functionName): ParsedFunctionInfo

Parses function files including variable substitution in mapping templates and data source binding.

Parameters

NameTypeDescription
functionNamestringFunction name

Returns

ParsedFunctionInfo

Parsed function information


readFunctions

โ–ธ readFunctions(): string[]

Reads the on-disk function directory names. Essentially, the first-level subdirectories of functions are read.

vtl
โ”œโ”€โ”€ functions
โ”‚ โ””โ”€โ”€ GetPostById
โ”‚ โ”œโ”€โ”€ request.vtl
โ”‚ โ””โ”€โ”€ response.vtl
โ””โ”€โ”€ resolvers
reader.readFunctions(); // ['GetPostById']

Returns

string[]

A list of function names

Inherited from

Reader.readFunctions


readTypes

โ–ธ readTypes(): string[]

Reads the on-disk GraphQL type names. Essentially, the first-level subdirectories of resolvers are read.

vtl
โ”œโ”€โ”€ functions
โ””โ”€โ”€ resolvers
โ”œโ”€โ”€ Mutation
โ””โ”€โ”€ Query
โ””โ”€โ”€ Post
reader.readTypes(); // ['Mutation', 'Query', 'Post']

Returns

string[]

A list of GraphQL types

Inherited from

Reader.readTypes


readFields

โ–ธ readFields(typeName): string[]

Reads the on-disk GraphQL field names. Essentially, the second-level subdirectories of resolvers are read.

vtl
โ”œโ”€โ”€ functions
โ””โ”€โ”€ resolvers
โ”œโ”€โ”€ Mutation
โ””โ”€โ”€ Query
โ””โ”€โ”€ getPost
โ”œโ”€โ”€ request.vtl
โ””โ”€โ”€ response.vtl
reader.readFields('Query'); // ['getPost']

Parameters

NameTypeDescription
typeNamestringGraphQL type name

Returns

string[]

A list of GraphQL fields

Inherited from

Reader.readFields


readResolver

โ–ธ readResolver(typeName, fieldName): ResolverInfo

Reads a GraphQL field resolver from disk.

Parameters

NameTypeDescription
typeNamestringGraphQL type name
fieldNamestringGraphQL field name

Returns

ResolverInfo

Unit or Pipeline Resolver information

Inherited from

Reader.readResolver


readFunction

โ–ธ readFunction(functionName): FunctionInfo

Reads an AWS AppSync Function from disk.

Parameters

NameTypeDescription
functionNamestringFunction name

Returns

FunctionInfo

Function information

Inherited from

Reader.readFunction