Closed nitingupta910 closed 6 years ago
Try moving all of your import_types to schema.ex; type resolution doesn't happen until compilation and imports aren't done inside type (non schema) modules. We're going to work on making this more clear in a future release.
I now moved all imports in schema.ex:
import_types Shared
import_types Foo
import_types Bar
and removed import of shared in foo and bar. Now it compiles fine. However, graphiql cannot see any shared field from either foo or bar:
query Foo {
foo(id: "123") {
fshared <---- field defined on shared object but not visible in foo which import_fields :shared
ffoo
}
}
Can you supply your object :foo
definition?
object :foo do
import_fields :shared
field :ffoo, :string do
resolve fn _, _, _ -> {:ok, "ffoo value"} end
end
end
just for reference, here is object :shared
:
object :shared do
field :id, :id
field :fshared, :string do
resolve fn _, _, _ -> {:ok, "fshared value"} end
end
end
You're going to need to provide the actual code the causes the issue, or at least a simplified example that actually demonstrates the issue. Using the code you've provided doesn't exhibit any problem at all:
defmodule Shared do
use Absinthe.Schema.Notation
object :shared do
field :id, :id
field :fshared, :string do
resolve fn _, _, _ -> {:ok, "fshared value"} end
end
end
end
defmodule Schema do
use Absinthe.Schema
import_types Shared
object :foo do
import_fields :shared
field :ffoo, :string do
resolve fn _, _, _ -> {:ok, "ffoo value"} end
end
end
query do
field :foo, :foo, resolve: fn _, _, _ -> {:ok, %{}} end
end
end
Absinthe.run("{foo { fshared }}", Schema)
#=> {:ok, %{data: %{"foo" => %{"fshared" => "fshared value"}}}}
I created git repo for a minimal running absinthe project which reproduces this issue: absinthe_385
Here is some iex output:
iex(9)> Absinthe.run("{foo { ffoo }}", Schema)
{:ok, %{data: %{"foo" => %{"ffoo" => "ffoo value"}}}}
iex(10)> Absinthe.run("{foo { ffshared }}", Schema)
{:ok,
%{errors: [%{locations: [%{column: 0, line: 1}],
message: "Cannot query field \"ffshared\" on type \"Foo\"."}]}}
iex(11)>
Awesome, I'll take a look here as soon as I can.
I just removed some more code to make the example simpler.
Hey! Confirmed that this is indeed an issue. Basically, import_fields
doesn't work unless the object you're importing from is defined in the module, or imported into the module. Obviously that won't work if you want to have it imported in several places.
Will fix.
@benwilson512 Any update on this? I’m running into this issue as well.
I’d be happy to put together a PR if you can point me in the right direction for the fix. :)
@benwilson512 I feel like we've ran into this before. Is this something @xtian can pick up (if still interested), or do you need to do a deeper dive?
Any update on this? How can I use shared fields? I would like to avoid duplicate fields.
Hey. I think in the current implementation of the schema this is not possible right now. The Absinthe 1.5 release will include a full re-work of the schema internals, and I think this will be possible then.
This works now in 1.5 / master.
I have a set of shared fields defined in
shared.ex
:and two object types, defined in
foo.ex
andbar.ex
which want to import fields in this shared object:foo.ex
bar.ex
and finally in
schema.ex
:With this setup, I'm getting this build error:
So, the question is, how do I keep fields shared between foo and bar in a separate file?
Setup details: