Swift and JS DataStore subscriptions are inconsistent, if @belongsTo field is marked as required #626
Labels
bug
Something isn't working
DataStore
p2
transferred
Issue has been transferred from another Amplify repository
How did you install the Amplify CLI?
pnpm
If applicable, what version of Node.js are you using?
v19.2.0
Amplify CLI Version
12.0.2
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
Describe the bug
I've been struggling with this for a while in a big project. So I created a small, simple project with only two models:
I set the relationship field
channelMessagesId
explicitly, because in the big project I need it for sorting purposes.Then I created two different small clients, Swift and JavaScript, that only use the DataStore (no API).
In my
cli.json
the flaggenerateModelsForLazyLoadAndCustomSelectionSet
was set tofalse
.So I had to change it to
true
, push the backend again (using--force
because there was nothing to update) and pull it back from my clients.I wrote two simple functions to create a message:
createMessage (Swift)
createMessage (JavaScript)
Finally, I set both clients in listening mode, by observing the Message type:
subscribeToMessages (Swift)
subscribeToMessages (JavaScript)
The mutation that is executed when creating a message on the Swift client generates a warning on the JavaScript client, preventing the subscription to succeed:
The subscription on the Swift client receives the mutation without issues:
The message is created correctly on the backend.
Next, I tried the other way around: creating a message on the JavaScript client.
Both the JavaScript and the Swift clients receive the mutation without issues.
Expected behavior
As I discovered, the problem lies in the schema (see below).
What I expected, though, is that the DataStore would have behaved consistently between the Amplify implementations.
Reproduction steps
generateModelsForLazyLoadAndCustomSelectionSet
is set totrue
incli.json
Project Identifier
No response
Log output
No response
Additional information
And now for the best part: I managed to spot the root of the problem.
Remember: it's crucial that the flag
generateModelsForLazyLoadAndCustomSelectionSet
is set totrue
incli.json
!In the GraphQL schema, I switched the required flag (ie the exclamation mark) from
channelMessagesId
tochannel
:and now (after pushing, pulling and rebuilding, of course) both clients behave as expected.
I created this issue to highlight that maybe the documentation should be explicit on all this.
The examples provided for the
@belongsTo
directive, with the usage of thefields
argument (https://docs.amplify.aws/cli/graphql/data-modeling/#belongs-to-relationship), can lead to the anomaly that I described.It took me a lot of time to figure out the solution; I hope this helps anyone facing the same problem!
Before submitting, please confirm:
The text was updated successfully, but these errors were encountered: