Coder Social home page Coder Social logo

graphql-assert-transformer's Introduction

graphql-assert-transformer

A custom transformer of the amplify-cli. It can validate fields.

directive @assert(condition: String!, message: String, type: String) on FIELD_DEFINITION

Usage

1. Install package

npm install graphql-assert-transformer -D

or

yarn add graphql-assert-transformer -D

2. Setup custom transformer

Edit amplify/backend/api/<YOUR_API>/transform.conf.json and append "./graphql-assert-transformer" to transformers field.

    "transformers": [
      "graphql-assert-transformer"
    ]

3. Use @assert directive

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.

3.1 How it works

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

4. Export NODE_PATH

This step isn't necessary once aws-amplify/amplify-cli#3236 merged.

export NODE_PATH=./node_modules

License

Fork of https://github.com/amazon-archives/aws-reinvent-2019-mobile-workshops/tree/master/MOB405/01%20-%20Finished

ISC

Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. Copyright 2020 Hiroshi Ioka. All Rights Reserved.

graphql-assert-transformer's People

Contributors

hirochachacha avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.