I'm using Laravel Lighthouse.
Following situation: I have users, and those users should be allowed to access different datasets and run different mutations.
My solution: The users are assigned roles, and those roles define what datasets the user can access and what mutations can be run.
I'm stuck at the implementation. What I could do is to write down all my Queries and Mutations in my schema, and have policies to restrict access to them. What I would prefer is to have a way to see from the schema which role has access to what.
My idea: Have a type for each role, and in that type associate what data can be accessed and what mutations can be run
Here's some example code that might explain what I'm going for, even though the syntax is probably invalid:
type Query {
me: User @auth
}
type User {
id: ID
username: String
first_name: String
wage: Float
password: String
roles: [Role]
role(name: String! @eq): Role @find
}
type Role {
id: ID
name: String
}
type AdminRole {
#set of users whose data the admin has access to
#also directly restrict the amount of attributes that are accessible (e.g. password is not accessible)
#this is invalid syntax, I know
users: [Users] @all {
id
first_name
wage
}
#a mutation the admin has access to
updateUser(id: ID!, wage: Float): User @update
}
What query I'd like to run for the admin to get all wages:
query {
me {
role(name: "AdminRole") {
users {
wage
}
}
}
}
What mutation I'd like to run for the admin to update a user's wage:
mutation {
me {
role(name: "AdminRole") {
updateUser(id: 7, wage: 10.00) {
id
wage
}
}
}
}
So instead of writing policies that restrict access to things, I'd rather just have everything defined implicitly in the schema. This would make defining and answering "What can an admin do?" more intuitive and easier to comprehend, because it's written down in a single spot, rather than several policies.
I assume this is not possible in the way I described above. What's the closest thing to it? Or are there issues with this approach?