Skip to content

A field with @default directive is required on the generated create entity mutation input #390

@alex-vladut

Description

@alex-vladut

Before opening, please confirm:

  • I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
  • I have searched for duplicate or closed issues.
  • I have read the guide for submitting bug reports.
  • I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

How did you install the Amplify CLI?

npm install -g @aws-amplify/cli

If applicable, what version of Node.js are you using?

v14.16.0

Amplify CLI Version

7.6.5

What operating system are you using?

MacOS

Amplify Categories

api

Amplify Commands

Not applicable

Describe the bug

When a required field also has a @default directive attached to it, the mutation for creating a new entity will still require this field to be provided and will output an error otherwise, even though a default value should be assigned to it. If, on the other hand, the field is set as not required (i.e. removing the exclamation mark in the GraphQL schema) then its value could be updated to null, which is not the expected behavior. e.g.

type Organisation @model {
  id: ID! @primaryKey
  name: String!
  status: OrganisationStatus! @default(value: "CREATED")
}

here I have a field status with a default value provided, but the generated createOrganisation mutation will still require a value to be provided for this field.

Expected behavior

I would expect to be able to set a field in the GraphQL schema as both required and attach a default value to it, in which case the corresponding createX mutation generated for that entity to have this field as optional. The type CreateOrganisationInput looks something like that, so I think it should take into consideration if there is a default value assigned to a field or not (see that status is required):

input CreateOrganisationInput {
  id: ID
  name: String!
  status: OrganisationStatus!
  createdAt: AWSDateTime
  updatedAt: AWSDateTime
  _version: Int
}

Reproduction steps

As an example could try this simple project I put together to reproduce this issue https://github.com/alex-vladut/aws-amplify-auth, but any simple schema with a default value could do it.

GraphQL schema(s)

# Put schemas below this line

enum OrganisationStatus {
  CREATED
  DISABLED
  INACTIVE
}

type Organisation
  @model
  @auth(rules: [{ allow: owner }]) {
  id: ID! @primaryKey
  name: String!
  status: OrganisationStatus! @default(value: "CREATED")
  createdAt: AWSDateTime!
  updatedAt: AWSDateTime!
}

Log output

# Put your logs below this line


Additional information

No response

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingpending-releasetransferredIssue has been transferred from another Amplify repository

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions