Skip to content

How to implement readonly access to fields depending on the users Role #4341

Answered by tobias-tengler
causiz asked this question in Q&A
Discussion options

You must be logged in to vote

If you take a look at the Authorization documentation, you can annotate your properties or mutation methods using the [Authorize] attribute.
There is no such thing as "hiding" fields or getters/setters.
You could for example have the following setup:

type User {
  name: String
  ssn: SocialSecurityNumber @authorize(roles: "Administrator)
}

type Query {
 user(id: ID): User
}

Now only an administrator can query for the ssn field on the user type.

For modification you would have a mutation:

type Mutation {
  setUserSsn(userId: ID, ssn: SocialSecurityNumber): Payload @authorize(policy: "IsOwner")
}

Now only users that fulfill the IsOwner policy can change the ssn of a user.

Hope this helps :)

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@causiz
Comment options

@causiz
Comment options

@tobias-tengler
Comment options

Answer selected by causiz
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants