Git Product home page Git Product logo

graphql-auto-transformer's Introduction

graphql-auto-transformer

A custom transformer of the amplify-cli. It can control accessibility of auto generated fields.

directive @auto(creatable: Boolean = false, updatable: Boolean = false) on FIELD_DEFINITION

Motivation

If you are familiar with amplify-cli, you know several fields of object are generated by some directives. For example, @model generates createdAt and updatedAt, @versioned generates version, @auth generates owner and so on. Currently, the official way to access these fields from GraphQL API is to define these types as nullable types.

type Post @model {
  id: ID!
  text: String
  createdAt: AWSDateTime
}

However above solution isn't ideal because users can modify createdAt at any time.

input CreatePostInput {
  id: ID
  text: String
  createdAt: AWSDateTime
}

input UpdatePostInput {
  id: ID!
  text: String
  createdAt: AWSDateTime
}

We want to strip createdAt field from CreatePostInput and UpdatePostInput.

What is worse, we cannot use nullable type as a part of a database index. So, we should write:

type Post
  @model
  @key(name: "byUser", fields: ["userId", "createdAt"]) {
    id: ID!
    userId: ID!
    text: String
    createdAt: AWSDateTime!
}

Predictabley, the result is bad. aws-amplify/amplify-cli#1657

Usage

1. Install package

npm install graphql-auto-transformer -D

or

yarn add graphql-auto-transformer -D

2. Setup custom transformer

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

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

3. Use @auto directive

Append @auto to target fields.

type Post @model {
  id: ID!
  text: String
  createdAt: AWSDateTime! @auto
}

generates:

input CreatePostInput {
  id: ID
  text: String
}

input UpdatePostInput {
  id: ID!
  text: String
}

If you want to allow optional creation/update like id field, use creatable and updatable parameter.

type Post @model {
  id: ID!
  text: String
  createdAt: AWSDateTime! @auto(creatable: true)
}

generates:

input CreatePostInput {
  id: ID
  text: String
  createdAt: AWSDateTime
}

input UpdatePostInput {
  id: ID!
  text: 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 graphql-versioned-transformer

Apache-2.0

Copyright 2017 - 2020 Amazon.com, Inc. or its affiliates. All Rights Reserved.

Copyright 2020 - 2020 Hiroshi Ioka. All Rights Reserved.

graphql-auto-transformer's People

Contributors

hirochachacha avatar dependabot[bot] 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.