A custom transformer of the amplify-cli. It can validate fields.
directive @assert(condition: String!, message: String, type: String) on FIELD_DEFINITION
npm install graphql-assert-transformer -D
or
yarn add graphql-assert-transformer -D
Edit amplify/backend/api/<YOUR_API>/transform.conf.json
and append "./graphql-assert-transformer"
to transformers
field.
"transformers": [
"graphql-assert-transformer"
]
Append @assert
to target fields.
type Post @model {
id: ID!
title: String @assert(condition: ".length() > 3 && .matches(\"[a-zA-Z0-9]+\")")
text: String @assert(condition: ".length() > 10")
episode: Int @assert(condition: ". % 2 == 0")
}
You can refer the field itself by .
.
This transformer doesn't check type soundness, writing correct boolean expressions are up to you.
AppSync uses Apache Velocity for resolver mapping. It supports basic operators like logical operators. Besides, It supports Java methods. Thus, you can do whatever you want. But I'm not sure which Java version is used inside AppSync, you may find missing methods.
GraphQL Type | Java Class |
---|---|
ID | java.lang.String |
String | java.lang.String |
Int | java.lang.Integer |
Float | java.lang.Double |
Boolean | java.lang.Boolean |
Array | java.util.ArrayList |
Enum | java.lang.String |
This step isn't necessary once aws-amplify/amplify-cli#3236 merged.
export NODE_PATH=./node_modules
ISC
Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. Copyright 2020 Hiroshi Ioka. All Rights Reserved.