itopiz

itopiz

How to extend the payload output macro in absinthe_relay 1.5?

Since I migrated from absinthe_relay version 1.4 to 1.5, my code does not compile anymore.

Indeed, I was using a custom macro to extend the payload output/1 macro:

defmodule MyApp.Schema.Notation do
  @moduledoc false

  defmacro mutation({:output, _, _}, do: block) do
    do_output(__CALLER__, block)
  end

  defmacro mutation({:output, _, _}) do
    do_output(__CALLER__, nil)
  end

  defp do_output(_env, block) do
    record_output(block)
  end

  defp record_output(block) do
    body = mutation_output_body()

    quote do
      output do
        unquote(body)
        unquote(block)
      end
    end
  end

  # success and error fields are automatically added
  defp mutation_output_body do
    quote do
      @desc "Indicates if the command succeeded."
      field(:success, non_null(:boolean))
      @desc "Error details."
      field(:error, :error)
    end
  end
end

The custom macro was used this way to add the 2 fields :success and :error on my mutations outputs:

payload field(:register_user) do
  input do
    field(:username, non_null(:string))
    field(:display_name, non_null(:string))
    field(:password, non_null(:string))
    field(:role, non_null(:user_role))
    field(:blocked, non_null(:boolean))
  end

  mutation output do
    field(:user, :user)
  end

  resolve(&Accounts.RegisterUser.resolve/3)
end

payload field(:change_user_username) do
  input do
    field(:user_id, non_null(:id))
    field(:username, non_null(:string))
  end

  mutation(output)

  middleware(Absinthe.Relay.Node.ParseIDs, user_id: :user)
  resolve(&Accounts.ChangeUserUsername.resolve/3)
end

Since absinthe_relay 1.5, output/1 was replaced by output/2 and my code does not compile anymore:

$ mix compile 
Compiling 6 files (.ex)

== Compilation error in file lib/myapp_web/schema/accounts.ex ==
** (CompileError) lib/myapp_web/schema/accounts.ex:28: undefined function output/1
    expanding macro: MyAppWeb.Schema.Notation.mutation/2
    lib/myapp_web/schema/accounts.ex:28: MyAppWeb.Schema.Accounts (module)
    expanding macro: Absinthe.Schema.Notation.field/3
    lib/myapp_web/schema/accounts.ex:12: MyAppWeb.Schema.Accounts (module)
    expanding macro: Absinthe.Relay.Mutation.Notation.Modern.payload/2
    lib/myapp_web/schema/accounts.ex:12: MyAppWeb.Schema.Accounts (module)
    expanding macro: Absinthe.Schema.Notation.object/2
    lib/myapp_web/schema/accounts.ex:10: MyAppWeb.Schema.Accounts (module)
    (elixir 1.10.4) lib/kernel/parallel_compiler.ex:304: anonymous fn/4 in Kernel.ParallelCompiler.spawn_workers/7

I understand the compilation error: my custom macro is expanded after the new Absinthe.Relay.Mutation.Notation.Modern.payload/2 which rewrites the input and output blocks to add an identifier.

Is there a way to customize/extend the payload output macro to add generic fields with absinthe_relay 1.5?

First Post!

itopiz

itopiz

@benwilson512: any idea?

Where Next?

Popular in Questions Top

Werner
Hi, I’m using Ubuntu 18.04 and after updating to OTP-24.0 yesterday i have this warning when I run “mix local.hex”: 14:57:30.512 [warn] ...
New
logicmason
Hi there, I'm working through my first release with elixir/phoenix. I've built a release with distillery and found that it crashes when I...
New
lk-geimfari
What is most correct way to open, read and parse JSON file with poison? For example if we have example.json file in root of some projec...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
Phillipp
Hey, I have a NanoPi-M3 and try to install Elixir on their Ubuntu image. I followed the Raspberry Pi installation instructions from the ...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New
Exadra37
Sometimes I want to check if the input into a function is not a blank string. My first approach: defmodule Example do def do_stuff(s...
New
wernerlaude
In AR this is so simple @articles = current_user.articles How to do in Ecto? def index(conn, _params) do current_user = conn.assig...
New
jc00ke
Expanding on this topic: https://forum.elixirforum.net/t/map-typespec-question/19217 Let’s say I have a map with required and optional k...
New

Other popular topics Top

srinivasu
How to handle excepions in elixir? Suppose i have A, B, C ,D, E modules. and each module has get() function. A.get() method will call th...
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
AstonJ
You’re a programmer, so you don’t need spoon feeding with the conventional drivel about “this is an integer.” No. You need to know what’s...
New
KronicDeth
Elixir plugin for JetBrain’s IntelliJ Platform (including Rubymine) This is a plugin that adds support for Elixir to JetBrains IntelliJ...
289 35421 110
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
rms.mrcs
Hi, I need to transform a list of numbers into a map where the keys are the indexes and the values are the original values of the list....
New
chrisalley
ExUnit now has describe blocks which is a welcome addition coming from RSpec. In the docs, it states that nested hierarchies of describe ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
josevalim
Hi everyone, One of the features added to Elixir early on to help integration with Erlang code was the idea of overridable function defi...
New
Nvim
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New

We're in Beta

About us Mission Statement