Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unexpected behavior of @default with non-string types #76

Closed
5 tasks done
josefaidt opened this issue Mar 22, 2022 · 7 comments
Closed
5 tasks done

unexpected behavior of @default with non-string types #76

josefaidt opened this issue Mar 22, 2022 · 7 comments
Assignees
Labels
bug Something isn't working documentation Improvements or additions to documentation graphql-transformer-v2 p2

Comments

@josefaidt
Copy link
Contributor

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.
  • I have removed any sensitive information from my code snippets and submission.

How did you install the Amplify CLI?

pnpm

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

v16.13.2

Amplify CLI Version

7.6.25

What operating system are you using?

mac

Did you make any manual changes to the cloud resources managed by Amplify? Please describe the changes made.

no

Amplify Categories

api

Amplify Commands

Not applicable

Describe the bug

@default does not work for non-string types unless supplied default value is passed as a string. In the example below we are attempting to set default values based on the field type.

type Todo @model {
  someString: String @default(value: "my-default-string")
  someInt: Int @default(value: 42)
  someFloat: Float @default(value: 3.14)
  someBoolean: Boolean @default(value: true)
}

However this results in an error upon running amplify api gql-compile:

🛑 Schema validation failed.

Expected type String!, found 42.

GraphQL request:12:32
11 |   someString: String @default(value: "my-default-string")
12 |   someInt: Int @default(value: 42)
   |                                ^
13 |   someFloat: Float @default(value: 3.14)

Expected type String!, found 3.14.

GraphQL request:13:36
12 |   someInt: Int @default(value: 42)
13 |   someFloat: Float @default(value: 3.14)
   |                                    ^
14 |   someBoolean: Boolean @default(value: true)

Expected type String!, found true.

GraphQL request:14:40
13 |   someFloat: Float @default(value: 3.14)
14 |   someBoolean: Boolean @default(value: true)
   |                                        ^
15 | } 

After modifying the schema to pass default values as strings, we are then able to successfully compile the GraphQL schema and observe VTL templates are generated with the correct default values:

type Todo @model {
  someString: String @default(value: "my-default-string")
  someInt: Int @default(value: "42")
  someFloat: Float @default(value: "3.14")
  someBoolean: Boolean @default(value: "true")
}

and the resulting Mutation.createTodo.init.2.req.vtl for example

## [Start] Setting "someString" to default value of "my-default-string". **
$util.qr($context.args.input.put("someString", $util.defaultIfNull($ctx.args.input.someString, "my-default-string")))
## [End] Setting "someString" to default value of "my-default-string". **
## [Start] Setting "someInt" to default value of "42". **
$util.qr($context.args.input.put("someInt", $util.defaultIfNull($ctx.args.input.someInt, 42)))
## [End] Setting "someInt" to default value of "42". **
## [Start] Setting "someFloat" to default value of "3.14". **
$util.qr($context.args.input.put("someFloat", $util.defaultIfNull($ctx.args.input.someFloat, 3.14)))
## [End] Setting "someFloat" to default value of "3.14". **
## [Start] Setting "someBoolean" to default value of "true". **
$util.qr($context.args.input.put("someBoolean", $util.defaultIfNull($ctx.args.input.someBoolean, true)))
## [End] Setting "someBoolean" to default value of "true". **
{}

Expected behavior

Default value adheres to field type

type Todo @model {
  someString: String @default(value: "my-default-string")
  someInt: Int @default(value: 42)
  someFloat: Float @default(value: 3.14)
  someBoolean: Boolean @default(value: true)
}

Reproduction steps

  1. amplify init -y
  2. amplify add api > GraphQL > Blank schema
  3. paste provided schema
  4. amplify api gql-compile
  5. observe error

GraphQL schema(s)

# Put schemas below this line

Log output

# Put your logs below this line


Additional information

https://docs.amplify.aws/cli/graphql/data-modeling/#assign-default-values-for-fields

@josefaidt josefaidt added bug Something isn't working graphql-transformer-v2 p2 labels Mar 22, 2022
@josefaidt josefaidt added the documentation Improvements or additions to documentation label Apr 5, 2022
@josefaidt
Copy link
Contributor Author

NOTE: this is working as expected. add more examples to the docs for non-string types

@alharris-at alharris-at transferred this issue from aws-amplify/amplify-cli May 17, 2022
@naingaungphyo
Copy link

So does it mean we always need to use string value whatever the type of the field is?

@alharris-at alharris-at mentioned this issue Jun 4, 2022
5 tasks
@naingaungphyo
Copy link

naingaungphyo commented Jun 22, 2022

add more examples to the docs for non-string types

I can't see any docs update yet but I think we can't use non-string as default value regardless of the corresponding field type.

@naingaungphyo
Copy link

> The value parameter must be formatted as a string regardless of the corresponding field type.

I can confirm that it works for Int type with default value parameter as string.

@davaya
Copy link

davaya commented Aug 12, 2022

Is there documentation for what string equivalents are allowed? (e.g., "nil" or JSON "null" or Python "None", IEEE 754 "NaN", "6.022E23", etc)

@hisham
Copy link

hisham commented Jan 24, 2024

+1. Better documentation would have saved me some time here. Thanks!

Even Amazon Q gave me wrong info:
CleanShot 2024-01-23 at 17 56 24@2x

Copy link

github-actions bot commented Jul 2, 2024

This issue is now closed. Comments on closed issues are hard for our team to see.
If you need more assistance, please open a new issue that references this one.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working documentation Improvements or additions to documentation graphql-transformer-v2 p2
Projects
None yet
Development

No branches or pull requests

5 participants