amplify-flutter icon indicating copy to clipboard operation
amplify-flutter copied to clipboard

[Gen2] Cannot return null for non-nullable type: xxx within parent

Open hangoocn opened this issue 1 year ago • 1 comments

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

  1. 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(),
    })
  1. Create a room first
  2. 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;
  1. Check response.hasErrors which now has Cannot 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 avatar Jun 30 '24 21:06 hangoocn

@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.

NikaHsn avatar Jul 01 '24 16:07 NikaHsn

@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?

NikaHsn avatar Jul 06 '24 00:07 NikaHsn

Hi @NikaHsn I can confirm message is created sucessfully.

hangoocn avatar Jul 06 '24 00:07 hangoocn

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.

tommi-h-taigoa avatar Jul 10 '24 05:07 tommi-h-taigoa

@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

Equartey avatar Jul 11 '24 19:07 Equartey

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
      })

stewartmoreland avatar Aug 22 '24 02:08 stewartmoreland

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 avatar Aug 25 '24 08:08 mertkarahan955

@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.

Equartey avatar Aug 28 '24 15:08 Equartey

I am experiencing this behavior with two Models that have the same authorization rule...

charlieforward9 avatar Feb 21 '25 04:02 charlieforward9