1.3.5 • Published 6 months ago

@amplifiers/amplify-graphql-create-model-transformer v1.3.5

Weekly downloads
-
License
MIT
Repository
github
Last release
6 months ago

amplify-graphql-create-model-transformer

Description

This directive is intended to be used for creating a new model once a Cognito Event is fired, significantly speeding up the development of your AWS Amplify projects.

@createModel

The @createModel GraphQL directive creates a new model on cognito trigger event (ex. Cognito Post Confirmation) initializing the model from the event's fields.

Definition

directive @createModel( trigger:String="postConfirmation", fieldMap: [FieldMap]) on OBJECT
input FieldMap {
    cognitoField: String!
    modelField: String!
}

Installation

npm install --save @amplifiers/amplify-graphql-create-model-transformer

Import

/amplify/backend/api/<API_NAME>/transform.conf.json

{
  "transformers": [
    "@amplifiers/amplify-graphql-create-model-transformer"
  ]
}

Usage

Make sure your project has authorization added.

Append directive to target model and specify the trigger event and the field mapping. Trigger is optional and the default value is postConfirmation. The values are mapped from the cognito event field to the model fields. Custom fields are also supported ex: {modelField:"age", cognitoField:'custom:age'}.

type User
  @model
  @auth(
    rules: [
      { allow: public, operations: [read] }
      { allow: owner, operations: [read, update], ownerField: "id" }
    ]
  )
  @createModel(
    fieldMap: [
      { modelField: "id", cognitoField: "sub" }
      { modelField: "firstName", cognitoField: "given_name" }
      { modelField: "lastName", cognitoField: "family_name" }
      { modelField: "email", cognitoField: "email" }
    ]
  ) {
  id: ID!
  firstName: String
  lastName: String
  email: AWSEmail
  todos: [Todo] @hasMany
}

type Todo
  @model
  @auth(rules: [{ allow: owner, ownerField: "owner" }])
  @createModel(
    fieldMap: [
      { modelField: "owner", cognitoField: "sub" }
      { modelField: "userTodosId", cognitoField: "sub" }
    ]
  ) {
  id: ID!
  name: String! @default(value: "My first Todo")
  description: String! @default(value: "Enter text here...")

  owner: String
  user: User @belongsTo
}

In the example above we create a new User and Todo models after user finishes the sign up flow in the front end. An important thing to note is that the User id field is also the record owner, and the value is set from the cognito sub field. In this example our sign up page has extra attributes signUpAttributes={["family_name", "given_name"]} :

alt text

All possible cognito fields: "username", "email", "phone_number", "birthdate", "email", "family_name", "given_name", "middle_name", "name", "nickname", "phone_number", "preferred_username", "profile", "website", "confirmation_code", "password", "confirm_password", "address", "gender", "locale", "picture", "updated_at", "zoneinfo"

Deploy the project and test it with a new user.

NOTE: Right now you will need to manually link the lambda function to the cognito trigger event, by going to Cognito -> Manage User Pools -> Select the pool -> Triggers -> Post confirmation -> select amplify-{shortpojectname}-CreateModelTransformer-{id}

In the future we are looking to make this automatic.

alt text

Development and Contributions

Contributions are more than welcome! Please feel free to open an issue or a pull request. Developer docs are here

License

MIT License

1.3.5

6 months ago

1.3.4

6 months ago

1.2.0

6 months ago

1.3.3

6 months ago

1.3.2

6 months ago

1.3.1

6 months ago

1.3.0

6 months ago

1.2.1

6 months ago

1.1.10

6 months ago

1.1.16

6 months ago

1.1.15

6 months ago

1.1.14

6 months ago

1.1.13

6 months ago

1.1.18

6 months ago

1.1.17

6 months ago

1.1.23

6 months ago

1.1.22

6 months ago

1.1.21

6 months ago

1.1.20

6 months ago

1.1.1

2 years ago

1.1.0

2 years ago

1.1.5

1 year ago

1.1.4

2 years ago

1.1.3

2 years ago

1.1.2

2 years ago

1.0.9

2 years ago

1.0.8

2 years ago

1.0.7

2 years ago

1.0.6

2 years ago

1.0.5

2 years ago

1.0.4

2 years ago