Skip to content

Relation ship when authorization is owner cannot access a parent upon creation #2859

Description

@ipodishima

How did you install the Amplify CLI?

npx

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

v20.6.1

Amplify CLI Version

npx ampx --version 1.2.5

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 took the example given in the documentation and just changed the authorization to owner

const schema = a.schema({
  Member: a.model({
    name: a.string().required(),
    // 1. Create a reference field
    teamId: a.id(),
    // 2. Create a belongsTo relationship with the reference field
    team: a.belongsTo('Team', 'teamId'),
  })
  .authorization(allow => [allow.owner()]),

  Team: a.model({
    mantra: a.string().required(),
    // 3. Create a hasMany relationship with the reference field
    //    from the `Member`s model.
    members: a.hasMany('Member', 'teamId'),
  })
  .authorization(allow => [allow.owner()])
});

I can run a mutation for creating a team with success.

mutation CreateTestTeam {
  createTeam(input: {mantra: "Test"}) {
    id
  }
}

But when I run this mutation

mutation CreateJohn {
  createMember(input: {name: "John", teamId: "c0da8ec0-5fe9-4125-b62c-1b0dbcb4a557"}) {
    id
    name
    team {
      id
      mantra
    }
  }
}

This is what I get

{
  "data": {
    "createMember": {
      "id": "16827537-b102-40f5-83e9-252a65872cda",
      "name": "John",
      "team": null
    }
  },
  "errors": [
    {
      "path": [
        "createMember",
        "team",
        "id"
      ],
      "locations": null,
      "message": "Cannot return null for non-nullable type: 'ID' within parent 'Team' (/createMember/team/id)"
    },
    {
      "path": [
        "createMember",
        "team",
        "mantra"
      ],
      "locations": null,
      "message": "Cannot return null for non-nullable type: 'String' within parent 'Team' (/createMember/team/mantra)"
    }
  ]
}

Note:
Running the query just after

query GetTeam {
  getTeam(id: "c0da8ec0-5fe9-4125-b62c-1b0dbcb4a557") {
    id
    mantra
    members {
      items {
        id
        name
      }
    }
  }
}

works (aka John was created, but the response had an error)

{
  "data": {
    "getTeam": {
      "id": "c0da8ec0-5fe9-4125-b62c-1b0dbcb4a557",
      "mantra": "Test",
      "members": {
        "items": [
          {
            "id": "16827537-b102-40f5-83e9-252a65872cda",
            "name": "John"
          }
        ]
      }
    }
  }
}

Also, when I run

query GetJohn {
  getMember(id: "16827537-b102-40f5-83e9-252a65872cda") {
    id
    name
    team {
      id
      mantra
    }
  }
}

There are no issues

{
  "data": {
    "getMember": {
      "id": "16827537-b102-40f5-83e9-252a65872cda",
      "name": "John",
      "team": {
        "id": "c0da8ec0-5fe9-4125-b62c-1b0dbcb4a557",
        "mantra": "Test"
      }
    }
  }
}

Expected behavior

When using allow.authenticated() I do not have this issue. I expect the same behavior when auth is owner()

Reproduction steps

Simply creating a new model with team and member like in the documentation, but with owner for the auth for both team and member

Project Identifier

No response

Log output

Details
# Put your logs below this line


Additional information

No response

Before submitting, please confirm:

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions