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 do
field :login, :session do
arg :email, non_null(:string)
arg :password, non_null(:string)
arg :role, non_null(:role)
resolve &Resolvers.Accounts.login/3
end
# «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 do
use Absinthe.Schema.Notation
object :session do
field :token, :string
field :user, :user
end
enum :role do
value :employee
value :customer
end
interface :user do
field :email, :string
field :name, :string
resolve_type fn
%{role: "employee"}, _ -> :employee
%{role: "customer"}, _ -> :customer
end
end
object :employee do
interface :user
field :email, :string
field :name, :string
end
object :customer do
interface :user
field :email, :string
field :name, :string
field :orders, list_of(:order)
end
end

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 ...