aws-amplify / aws-amplify/amplify-codegen

Model Fields without read access should be marked 'isReadOnly'

Open
#419 0 comments 1 reaction 0 assignees View on GitHub
bug p3
Dominant language
TypeScript
Stars
59
Forks
64
PR merge metrics
No merged PRs in 30d

Description

### Before opening, please confirm:

- [X] I have installed the latest version of the Amplify CLI (see above), and confirmed that the issue still persists.
- [X] I have [searched for duplicate or closed issues](https://github.com/aws-amplify/amplify-codegen/issues?q=is%3Aissue+).
- [X] I have read the guide for [submitting bug reports](https://github.com/aws-amplify/amplify-codegen/blob/master/CONTRIBUTING.md#bugs).
- [X] I have done my best to include a minimal, self-contained set of instructions for consistently reproducing the issue.

### How did you install the Amplify CLI?

_No response_

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

_No response_

### Amplify CLI Version

8.0.2

### What operating system are you using?

Mac

### Amplify Codegen Command

Not applicable

### Describe the bug

When a graphql schema model and fields have `@auth` rules on it indicating that some of the model fields are not writable (don't have an `update` operation in the `@auth` rules) the generated fields are not marked with `isReadOnly`. Right now, this is the cause of aws-amplify/amplify-category-api#230 . Currently, any model that has read-only fields in it is completely incompatible with Datastore, as it will generate update statements that include the non-writable fields, causing AppSync to reject the update. (Now, whether AppSync should just ignore read-only fields that are exactly the same as the current values rather than rejecting it is another argument).

### Expected behavior

I would expect that when there are `@auth` statements on a model that indicate that some fields are read-only, those fields should be marked with the `isReadOnly` flag in the code-gen.

### Reproduction steps

This is trivially verified by looking at the `getWritableFields` function in `appsync-modelgen-plugin/src/visitors/appsync-java-visitor.ts` as the only fields it excludes as being writable are connected fields.

The schema below shows an example of a model object that is, by default, read-only, but has a field called `readWriteField` which has an `@auth` rule indicating that the field can be modified.

Please note that the id or other key field should not be marked as `isReadOnly` as that will prevent the graphql query generators in the client libraries from including them in the input variable of the query.

### GraphQL schema(s)

```graphql
@auth(rules: [{allow: owner, operations: [read], provider: userPools, ownerField: "userId"}])
{
id: ID! @auth(rules: [{ allow: owner, operations: [read, update], provider: userPools, ownerField: "userId"}])
userId: ID!
readOnlyField: String!
readWriteField: String! @auth(rules: [{ allow: owner, operations: [read, update], provider: userPools, ownerField: "userId"}])
}
```

### Log output

_No response_

### Additional information

Right now the work-around I'm using is to manually modify the generated client model files to mark the appropriate fields as `isReadOnly`.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.