question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DataStore - Omitting an optional field from a model object results in error on save

See original GitHub issue

Describe the bug Given a GraphQL schema which defines a model type with an optional (string) value, if a model instance is saved without defining that value (it’s optional after all), it will fail; definging a null value for the optional value succeeds.

To Reproduce This was recently demonstrated to us when we had a graphql schema like the following:

type Movie @model {
  id: ID!
  name: String!
  description: String
}

When calling something like:

await DataStore.save(
  new Movie({
    name: "Dumb and Dumber",
  })
);

The local DataStore appears to be updated but I see the following error logged as a warning:

[WARN] 06:03.591 DataStore - failed to execute errorHandler, [Error: Field description should be of type string, undefined received. undefined]

Expected behavior All optional fields should be able to be omitted when saved and it should succeed without populating that value. Alternatively, it can also be explicitly specified as undefined or null which is particularly useful on updates to clear out a field.

Investigation A quick search of the codebase seems to show the culprit of this error here.

Perhaps the following line can simply be changed from:

- } else if (typeof v !== jsType && v !== null) {
+ } else if (typeof v !== jsType && v != null) {

(e.g. it avoid strict equality on the null check to also include undefined and avoid erroring in that case?)

Earlier up in validateModelFields() it is already checking if the field is required:

if (isRequired && (v === null || v === undefined)) {
  throw new Error(`Field ${name} is required`);
}

So my suggested change should be sufficient? I don’t know if there are other side-effects of this change that are undesired, or if the original exclusion of undefined was intentional, but hopefully not?

Amplify Version 2.2.8

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
danrivettcommented, Oct 23, 2020

Brilliant, thanks @wei I look forward to checking it out once it’s released on a stable version. Thanks for fixing this!

1reaction
weicommented, Oct 23, 2020

For the record, I believe #7044 fixes this issue which has been merged and accessible in aws-amplify@unstable for now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[DataStore] Optional fields related to another table using ...
Describe the bug This is the schema that I am using: GraphQL Schema type Blog @model @auth(rules: [{allow: public}]) { id: ID! name:...
Read more >
How to save an optional value to a 1:1 relationship in Amplify ...
I am saving a car model with several fields. One field is the cityID of Type Relationship Source in Amplify UI(The city has...
Read more >
Basic Operations - Google Cloud
If the options include "omitempty" and the value of the field is an empty value, then the field will be omitted on Save....
Read more >
How to more gracefully handle non-optional Core Data ...
In Core Data, a non-optional property must not be nil when saving changes to your model but there is otherwise no enforcement of ......
Read more >
DataStore - Manipulating data - JavaScript - AWS Amplify Docs
Model instances which store values, such as those from the results of a DataStore.Query operation, can become stale and outdated when their properties...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found