Search⌘ K

Resolver Function

Explore how to create resolver functions in GraphQL mutations using Elixir. Understand handling input arguments, persisting data with Ecto changesets, and managing success or error responses for your API clients.

Building the resolver

Let’s build the resolver function. It will grab the :input argument for us, and then call a general-purpose PlateSlate.Menu.create_item/1 unction that will handle persisting the record:

def create_item(_, %{input: params}, _) do
case Menu.create_item(params) do
{:error, _} ->
{:error, "Could not create menu item"}
{:ok, _} = success ->
success
end
end

Here’s how we’ve implemented PlateSlate.Menu.create_item/1:

def create_item(attrs \\ %{}) do
%Item{}
|> Item.changeset(attrs)
|> Repo.insert()
end

The actual persistence of the menu items is straightforward, but requires us to know a little about how Ecto works. The important thing to know here is that an Ecto changeset ...