[Gen2] Cannot return null for non-nullable type: xxx within parent
Description
In any one-to-many relationship, if making a child instance creating request, the repsponse will has error Cannot return null for non-nullable type: xxx within parent due to parent object is null.
Categories
- [ ] Analytics
- [ ] API (REST)
- [X] API (GraphQL)
- [ ] Auth
- [ ] Authenticator
- [ ] DataStore
- [ ] Notifications (Push)
- [ ] Storage
Steps to Reproduce
- create data schema like this, with a 1-many relationship
Room: a
.model({
name: a.string().required(),
messages: a.hasMany('Message', 'roomId'),
}).authorization(allow => [
allow.owner(),
]),
Message: a
.model({
roomId: a.id().required(),
room: a.belongsTo('Room', 'roomId'),
content: a.string().required(),
})
- Create a room first
- Create a message like this
final message = Message(
room: room,
content: text,
);
final request = ModelMutations.create(message);
final response = await Amplify.API.mutate(request: request).response;
- Check
response.hasErrorswhich now hasCannot return null for non-nullable type: xxx within parent room
I noticed the room field in response has value null which should be a room object. Below is the default graphql generated by cli, which should return a room objecti:
export const createMessage = /* GraphQL */ `mutation CreateMessage(
$condition: ModelMessageConditionInput
$input: CreateMessageInput!
) {
createMessage(condition: $condition, input: $input) {
createdAt
id
owner
room {
createdAt
id
name
owner
updatedAt
__typename
}
roomId
updatedAt
__typename
}
}
` as GeneratedMutation<
APITypes.CreateMessageMutationVariables,
APITypes.CreateMessageMutation
>;
Screenshots
No response
Platforms
- [ ] iOS
- [ ] Android
- [ ] Web
- [ ] macOS
- [x] Windows
- [ ] Linux
Flutter Version
3.22.2
Amplify Flutter Version
2.1.0
Deployment Method
Amplify CLI
Schema
No response
@hangoocn Sorry that you are facing this issue and thanks for reporting it. We will look into this and get back to you when we have updates.
@hangoocn Following the reproduction steps I've been able to replicate the behavior described. Specifically, when attempting to create a message for a room the response has errors: Cannot return null for non-nullable type.
However, despite this error, the message is successfully created for the specified room. while we investigate this issue furthure could you please confirm whether the message is being created successfully on your end?
Hi @NikaHsn I can confirm message is created sucessfully.
We have been developing a web application over a year and just resently we have been facing the same problem when Amplify builds the application after a change. We can temporarily fix the environment by making "some change" to the schema and pushing it from local machine. But this is not a solution.
@hangoocn @tommi-h-taigoa
Hi all, the observed behavior is expected when deploying backends with newer versions of Amplify CLI and Gen 2. For more context please review the discussion on this issue
I'm also encountering this issue with a Node.js Lambda function. I'm not able to ignore it though as I'm using the data downstream in my Lambda function.
As a workaround, I've added a catch to return the remaining values of the mutation by way of the error response data field.
const variant = await client
.graphql({
query: createMediaVariantAssociation,
variables: {
input: {
mediaId,
variantId: mediaCreate.id,
ownerId: media.ownerId,
},
},
})
.then((res) => res.data.createMediaVariantAssociation)
.catch((err) => {
logger.error(err)
return err.data.createMediaVariantAssociation
})
So for who uses Gen2, what should we do for this problem? issue I looked this issue but I couldn't find what to do
@mertkarahan955
For Gen 2, you should query the additional Model individually when your response contains redacted relationships. You can reference the callout in the amber box on this docs page for more details.
For example, when your GraphQL request on the parent model returns null for the child due to a redaction. Then you should perform an additional query for the child model, filtering by the Parent ID, to retrieve the rest of the data.
I am experiencing this behavior with two Models that have the same authorization rule...