Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Built-in utilities

Focus mode
Built-in utilities - AWS AppSync GraphQL

The util variable contains general utility methods to help you work with data. Unless otherwise specified, all utilities use the UTF-8 character set.

Encoding utils

util.urlEncode(String)

Returns the input string as an application/x-www-form-urlencoded encoded string.

util.urlDecode(String)

Decodes an application/x-www-form-urlencoded encoded string back to its non-encoded form.

util.base64Encode(string) : string

Encodes the input into a base64-encoded string.

util.base64Decode(string) : string

Decodes the data from a base64-encoded string.

util.urlEncode(String)

Returns the input string as an application/x-www-form-urlencoded encoded string.

util.urlDecode(String)

Decodes an application/x-www-form-urlencoded encoded string back to its non-encoded form.

util.base64Encode(string) : string

Encodes the input into a base64-encoded string.

util.base64Decode(string) : string

Decodes the data from a base64-encoded string.

ID generation utils

util.autoId()

Returns a 128-bit randomly generated UUID.

util.autoUlid()

Returns a 128-bit randomly generated ULID (Universally Unique Lexicographically Sortable Identifier).

util.autoKsuid()

Returns a 128-bit randomly generated KSUID (K-Sortable Unique Identifier) base62 encoded as a String with a length of 27.

util.autoId()

Returns a 128-bit randomly generated UUID.

util.autoUlid()

Returns a 128-bit randomly generated ULID (Universally Unique Lexicographically Sortable Identifier).

util.autoKsuid()

Returns a 128-bit randomly generated KSUID (K-Sortable Unique Identifier) base62 encoded as a String with a length of 27.

Error utils

util.error(String, String?, Object?, Object?)

Throws a custom error. This can be used in request or response mapping templates if the template detects an error with the request or with the invocation result. Additionally, an errorType field, a data field, and an errorInfo field can be specified. The data value will be added to the corresponding error block inside errors in the GraphQL response.

Note

data will be filtered based on the query selection set. The errorInfo value will be added to the corresponding error block inside errors in the GraphQL response.

errorInfo will not be filtered based on the query selection set.

util.appendError(String, String?, Object?, Object?)

Appends a custom error. This can be used in request or response mapping templates if the template detects an error with the request or with the invocation result. Additionally, an errorType field, a data field, and an errorInfo field can be specified. Unlike util.error(String, String?, Object?, Object?), the template evaluation will not be interrupted, so that data can be returned to the caller. The data value will be added to the corresponding error block inside errors in the GraphQL response.

Note

data will be filtered based on the query selection set. The errorInfo value will be added to the corresponding error block inside errors in the GraphQL response.

errorInfo will not be filtered based on the query selection set.

util.error(String, String?, Object?, Object?)

Throws a custom error. This can be used in request or response mapping templates if the template detects an error with the request or with the invocation result. Additionally, an errorType field, a data field, and an errorInfo field can be specified. The data value will be added to the corresponding error block inside errors in the GraphQL response.

Note

data will be filtered based on the query selection set. The errorInfo value will be added to the corresponding error block inside errors in the GraphQL response.

errorInfo will not be filtered based on the query selection set.

util.appendError(String, String?, Object?, Object?)

Appends a custom error. This can be used in request or response mapping templates if the template detects an error with the request or with the invocation result. Additionally, an errorType field, a data field, and an errorInfo field can be specified. Unlike util.error(String, String?, Object?, Object?), the template evaluation will not be interrupted, so that data can be returned to the caller. The data value will be added to the corresponding error block inside errors in the GraphQL response.

Note

data will be filtered based on the query selection set. The errorInfo value will be added to the corresponding error block inside errors in the GraphQL response.

errorInfo will not be filtered based on the query selection set.

Type and pattern matching utils

util.matches(String, String) : Boolean

Returns true if the specified pattern in the first argument matches the supplied data in the second argument. The pattern must be a regular expression such as util.matches("a*b", "aaaaab"). The functionality is based on Pattern, which you can reference for further documentation.

util.authType()

Returns a String describing the multi-auth type being used by a request, returning back either "IAM Authorization", "User Pool Authorization", "Open ID Connect Authorization", or "API Key Authorization".

util.matches(String, String) : Boolean

Returns true if the specified pattern in the first argument matches the supplied data in the second argument. The pattern must be a regular expression such as util.matches("a*b", "aaaaab"). The functionality is based on Pattern, which you can reference for further documentation.

util.authType()

Returns a String describing the multi-auth type being used by a request, returning back either "IAM Authorization", "User Pool Authorization", "Open ID Connect Authorization", or "API Key Authorization".

Return value behavior utils

util.escapeJavaScript(String)

Returns the input string as a JavaScript escaped string.

util.escapeJavaScript(String)

Returns the input string as a JavaScript escaped string.

Resolver authorization utils

util.unauthorized()

Throws Unauthorized for the field being resolved. Use this in request or response mapping templates to determine whether to allow the caller to resolve the field.

util.unauthorized()

Throws Unauthorized for the field being resolved. Use this in request or response mapping templates to determine whether to allow the caller to resolve the field.

PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.