Skip to content

🐞 Issue: API.mutate(create/update/delete) succeeds in DynamoDB but throws GraphQL error when using belongsTo LazyReference (Room β†’ Space)Β #4127

Description

@bokute2010

Describe the bug

When creating a model with a belongsTo relationship using Amplify Gen2 + API.mutate (GraphQL/AppSync), the item is successfully written to DynamoDB, but the mutation throws a GraphQL error during response resolution.

This happens when the generated Swift model constructor accepts a related object (Space) via LazyReference, but no explicit foreign key (spaceId) exists in the schema.

This behavior is confusing because:

The code is generated by Amplify

The mutation appears to fail

But the data is actually persisted

Image Image

Steps To Reproduce

1. Create schema (Backend Amplify Gen2)

Room: a.model({
  name: a.string().required(),
  owner: a.string(),
  spaceId: a.id().required(),
  space: a.belongsTo('Space', 'spaceId'),
})

Space: a.model({
  name: a.string().required(),
  owner: a.string(),
})

2. Generated Swift Model (excerpt)

public struct Room: Model {
  public let id: String
  public var name: String
  internal var _space: LazyReference<Space>

  public var space: Space? {
    get async throws {
      try await _space.get()
    }
  }

  public init(
    id: String = UUID().uuidString,
    name: String,
    owner: String? = nil,
    space: Space? = nil
  ) {
    self.id = id
    self.name = name
    self.owner = owner
    self._space = LazyReference(space)
  }
}

3. Create a Space using API.mutate(.create)
- Create a Room and pass the Space object into the generated initializer:
let room = Room(
  name: "Living Room",
  owner: owner,
  space: space   // passed as LazyReference
)

try await Amplify.API.mutate(
  request: .create(room)
)

Expected behavior

One of the following would be expected:

The generated Swift model should not expose an initializer that accepts a related object when using API-only GraphQL mutations
OR

The documentation should clearly state that:

Passing related objects only works with DataStore

For API.mutate, foreign keys must be set explicitly
OR

Amplify Gen2 should automatically map LazyReference β†’ foreign key (spaceId) when performing create mutations

Amplify Framework Version

2.53.2

Amplify Categories

DataStore, API

Dependency manager

Swift PM

Swift version

6

CLI version

14.2.3

Xcode version

Version 16.2

Relevant log output

<details>
<summary>Log Messages</summary>


INSERT LOG MESSAGES HERE

</details>

Is this a regression?

Yes

Regression additional context

Root Cause Analysis (Suspected)

LazyReference does not serialize into the GraphQL CreateRoomInput

No spaceId is written to DynamoDB

During response resolution, AppSync attempts to resolve:

createRoom {
    space {
      id
    }
}

Since spaceId is null, resolver returns null

GraphQL throws because Space.id is non-nullable

This results in a successful write + failed response.

Platforms

No response

OS Version

ios 18.6.2 (ipad), 18.7.3 (iphone XR), 18.2 (Simulator)

Device

iphone XR, Ipad (9th generation), Simulator iphone 16 pro

Specific to simulators

No response

Additional context

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    apiIssues related to the API categorybugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions