Login API
Learn about how to define the mutation for your API.
We'll cover the following...
Mutation for API
With the underlying database work all set, the next task is to define the mutation for our API. We head over to our Absinthe schema and add a :login
mutation field to the root mutation type:
Press + to interact
mutation dofield :login, :session doarg :email, non_null(:string)arg :password, non_null(:string)arg :role, non_null(:role)resolve &Resolvers.Accounts.login/3end# «Other mutation fields»end
The mutation requires an email address, password, and role, and it returns a :session
type. We will create a new type module to house this type and others like it:
Press + to interact
defmodule PlateSlateWeb.Schema.AccountsTypes douse Absinthe.Schema.Notationobject :session dofield :token, :stringfield :user, :userendenum :role dovalue :employeevalue :customerendinterface :user dofield :email, :stringfield :name, :stringresolve_type fn%{role: "employee"}, _ -> :employee%{role: "customer"}, _ -> :customerendendobject :employee dointerface :userfield :email, :stringfield :name, :stringendobject :customer dointerface :userfield :email, :stringfield :name, :stringfield :orders, list_of(:order)endend
There are a couple of interesting types here. At the top, we have the :session
object that returns from the :login
mutation, which contains an API token ...