79298298

Date: 2024-12-20 20:08:28
Score: 0.5
Natty:
Report link

You cannot directly use a field's value (isPrivate) to conditionally apply authorization rules within the schema alone. The @auth directive operates at the type and field level but does not support dynamic rules based on field values.

To achieve this

  1. Use Custom Lambda Resolver.

This allows you to read the isPrivate field in the request, check the user's ownership or group membership, and allow or deny access accordingly.

  1. Or Implement Field-Level Authorization in the schema. (If only some of the fields should protected)

Split SomeEntity into fields with separate rules, e.g., privateField for owners and publicField for everyone.

Example :

type SomeEntity
  @model
  @auth(
    rules: [
      { allow: groups, groups: ["user"], operations: [create] }
      { allow: owner, operations: [read] }
    ]
  ) {
  ....
  .....
  privateField: String @auth(rules: [{ allow: owner }])
  publicField: String
}
Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @auth
  • Low reputation (0.5):
Posted by: Shahi Papon