What is the correct way to add roles to a user schema in absinthe?

Hi everyone,

My code can be found here https://github.com/wolfiton/phx_api/tree/role_not_working

Here is my user_type:

So i was wondering if I want two types of users admin and user for now would the following enum work?

@desc "Role enum"
enum :role do
  value :admin, description: "Admin role"
  value :user,  description: "User role"
end

If it works are the fields in the user types treated as atoms for example in my case does the enum role correspond to the role field in my user schema?

Also this example I created using the docs here https://hexdocs.pm/absinthe/Absinthe.Type.Enum.html

Thanks in advance

Update

My registration mutation works tested using

mutation{
  registerUser(input:{email:"a@a.com", password:"a@a.com", role:"user",passwordConfirmation:"a@a.com"}){
    email
    
  }
}

But my login mutation doesn’t work

Tried this

mutation{
  loginUser(input:{email:"a@a.com", password:"a@a.com", role:"user"}){
    token
    user{
      email
    }
  }
}

I get this error

"# KeyError at POST /api/graphiql\n\nException:\n\n    
** (KeyError) key :role not found in: %{email: \"a@a.com\", password: \"a@a.com\"}\n 

Also if you have any questions regarding my code please ask them, because i really wnat to make this example work and understand what I am doing wrong.

This code is used for learning.

The types in the field for :role in this file https://github.com/wolfiton/phx_api/blob/role_not_working/lib/phx_api_web/schema/session_type.ex#L13 all seem wrong. Your role fields are referencing various input types instead of strings.

2 Likes

Thank you for pointing out me error so i can’t use fields from the user schema and connect them like this?

field(:role, :user_input)

Is there a better way then using:

field(:role, non_null(:string))

Because i feel like i am repeating my user schema.
Or this is the path i should take?