Hi,
I've stumbled into that problem on current beta. Basically, given following schema (please ignore usefulness of it, I just wanted demonstrate failing case):
defmodule Schema do
use Absinthe.Schema
query do
#Query type must exist
end
object :thing do
field :name, :string
end
object :mutations do
field :update_thing,
type: :thing,
args: [],
resolve: fn _, _ ->
{:ok, %{name: "thing"}}
end
end
mutation do
import_fields :mutations
end
subscription do
field :thing, :string do
arg :client_id, non_null(:id)
resolve fn
%{client_id: id}, _ ->
{:ok, "subscribed-#{id}"}
end
end
end
end
This will fail with following stack trace:
[absinthe] mix test test/lib/absinthe_failing_fields_import_test.exs master ✱
Excluding tags: [pending: true]
** (Protocol.UndefinedError) protocol Enumerable not implemented for nil. This protocol is implemented for: Date.Range, File.Stream, Function, GenEvent.Stream, HashDict, HashSet, IO.Stream, List, Map, MapSet, Range, Stream
(elixir) /home/ubuntu/bob/tmp/3251921048f5d8e3ff92bd4156013d87/elixir/lib/elixir/lib/enum.ex:1: Enumerable.impl_for!/1
(elixir) /home/ubuntu/bob/tmp/3251921048f5d8e3ff92bd4156013d87/elixir/lib/elixir/lib/enum.ex:116: Enumerable.reduce/3
(elixir) lib/enum.ex:1832: Enum.map/2
(absinthe) lib/absinthe/schema/notation/writer.ex:101: Absinthe.Schema.Notation.Writer.add_mutation_triggers/1
(absinthe) lib/absinthe/schema/notation/writer.ex:75: Absinthe.Schema.Notation.Writer.build_info/1
(absinthe) expanding macro: Absinthe.Schema.Notation.Writer.__before_compile__/1
test/lib/absinthe_failing_fields_import_test.exs:9: Absinthe.AbsintheFailingFieldsImportTest.Schema (module)
(elixir) lib/code.ex:376: Code.require_file/2
(elixir) lib/kernel/parallel_require.ex:59: anonymous fn/2 in Kernel.ParallelRequire.spawn_requires/5
It looks like mutation.attrs[:fields](https://github.com/absinthe-graphql/absinthe/blob/master/lib/absinthe/schema/notation/writer.ex#L100) returns nil in __before_compile__ phase(?). This doesn't happen if there are any fields in the mutation block but in this case, it still looks like a bug because triggers wouldn't be added to some of imported mutations.
I'd love to try to submit a PR for that but I'm not quite sure how to approach this. Should the import_fields be implemented in a different way or maybe mutation.attrs[:fields] should be changed to something else that doesn't ignore the imported fields?
Some additional details:
Hey! Yea this is unfortunately a known issue, but I'm glad you made an issue so others can see it.
This is definitely an issue with how triggers are currently implemented. Mutation fields defined outside of the root module can't be modified, so I'm gonna have to come up with a different approach for storing this information.
This is a known issue with the macro apporach we discussed briefly with @benwilson512 over slack as I got bitten by that issue as well. @benwilson512, have you been able to think of a way to solve this ? Could you wire / resolve the triggers at runtime instead ?
For the specific issue you have, you can create a dummy mutation as a workaround but something else need to happen for those triggers to play nicely with import_fields.
Yeah I think it has to happen at runtime. The performance cost won't be very significant. Let me see if I can get a fix out tonight, I know this has been a show stopper for a lot of people.
Fixed in master!
Most helpful comment
Fixed in master!