When using Absinthe Controller, what is the correct way to massage data before it goes into the mutation

Hey,

I’ve created a Absinthe schema that is working. Just for fun I want to try to integrate and learn Absinthe.Phoenix.Controller. So that I can reuse the same schema in a “classic” controller solution.

I’ve started from the auto-generate code that I get from this

mix phx.gen.html Todos TodoList todo_lists name:string

index as easy to implement by just following the documentation.
However, from create I got a little stuck.
Here is my stripped controller.

defmodule BackendWeb.TodoListController do
  use BackendWeb, :controller
  use Absinthe.Phoenix.Controller, schema: BackendWeb.Schema, action: [mode: :internal]

  @graphql """
  mutation CreateTodoList($name: String!) {
    create_todo_list(name: $name)
  }
  """
  def create(conn, result) do
    # ...
  end
end

When I press the button from http://localhost:4000/todo_lists/new I get

Parameters: %{"_csrf_token" => "..", "todo_list" => %{"name" => "Have fun"}}

I think the mutation needs a %{name: "Have fun"}. Which means that I need to unpack the incoming request body.

Which is the correct way to massage the incoming data from the form so it fits with the GraphQL query. I want to ask before I try to hack my own solution.

Any help is greatly appreciated.

I think the original idea is to use the Absinthe controller with GraphQL queries. I don’t see why you’d want to use it the way you tried. Nevertheless, the docs show an example usage and the data for the query comes from the request’s query parameters - Absinthe.Phoenix.Controller — absinthe_phoenix v2.0.2.

1 Like

Thank you @krasenyp for the help. I agree that it might not be the best idea. However, I notice that there is a mutation example in the book (page 207).

Great suggestion about the query parameters example. I didn’t make the connection when I read the documentation. I was so focused on keeping the GraphQL statement the same (as if I called it externally over the API).

Once again, thank you for your reply, I will try it out.

2 Likes