Skip to content

[api] Model helpers should not pass null fields #2492

@dnys1

Description

@dnys1

Description

AppSync interprets explicit null values different than implicit ones in the variables map of a request. Model helpers explicitly setting these values can lead to unexpected errors when these fields are read-only or otherwise protected.

For example, consider the schema:

type Todo @model @auth(rules: [{ allow: public }, { allow: owner, ownerField: "owners" }]) {
  id: ID!
  name: String!
  owners: [String]
  private: String @auth(rules: [{ allow: owner, ownerField: "owners" }])
}

And a mutation to create a Todo:

mutation CreateTodo($input: CreateTodoInput!) {
  createTodo(input: $input) {
    id
    name
    owners
    private
    createdAt
    updatedAt
  }
}

The following input allows creation with no issues:

{
  "input": {
    "name": "test",
    "private": "secret"
  }
}

However, model helpers generates the following input (notice the inclusion of owners which cannot be written to):

{
  "input": {
    "name": "test",
    "owners": null,
    "private": "secret"
  }
}

This leads to the following error:

{
  "data": {
    "createTodo": null
  },
  "errors": [
    {
      "path": [
        "createTodo"
      ],
      "data": null,
      "errorType": "Unauthorized",
      "errorInfo": null,
      "locations": [
        {
          "line": 2,
          "column": 3,
          "sourceName": null
        }
      ],
      "message": "Not Authorized to access createTodo on type Todo"
    }
  ]
}

Categories

  • Analytics
  • API (REST)
  • API (GraphQL)
  • Auth
  • Authenticator
  • DataStore
  • Storage

Steps to Reproduce

No response

Screenshots

No response

Platforms

  • iOS
  • Android
  • Web
  • macOS
  • Windows
  • Linux

Android Device/Emulator API Level

No response

Environment

N/A

Dependencies

vNext

Device

N/A

OS

N/A

Deployment Method

Amplify CLI

CLI Version

No response

Additional Context

No response

Amplify Config

N/A

Metadata

Metadata

Labels

GraphQL APIIssues related to the API (GraphQL) CategorybugSomething is not working; the issue has reproducible steps and has been reproducedfixed-in-release-candidateIssues that have been addressed in the current release-candidate branch

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions