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 function that will handle attempting to persist the record:

1: def​ create_item(_, %{​input:​ params}, _) ​do
2: case​ Menu.create_item(params) ​do
3:  {​:error​, _} ->
4:  {​:error​, ​"​​Could not create menu item"​}
5:  {​:ok​, _} = success ->
6:  success
7: end
8: 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 ...

Get Craft GraphQL APIs in Elixir with Absinthe now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.