Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework how entities_field works #104

Merged
merged 6 commits into from
Dec 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 55 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,8 @@ defmodule Products.Schema do
directive :key, fields: "id"

# Any subgraph contributing fields MUST define a _resolve_reference field.
# Note that implementing the reference resolver with function capture does not work at the moment. Hence, the examples below use an anonymous function.
field :_resolve_reference, :product do
resolve(fn %{__typename: "Product", id: id} = entity, _info ->
{:ok, Map.merge(entity, %{name: "ACME Anvil", price: 10000})}
end)
resolve &Products.find_by_id/2
end

field :id, non_null(:id)
Expand Down Expand Up @@ -238,6 +235,60 @@ defmodule Example.Schema do
end
```

### Using Dataloader in \_resolve_reference queries

You can use Dataloader in to resolve references to specific objects, but it requires manually setting up the batch and item key, as the field has no parent. Resolution for both \_resolve\_reference fields are functionally equivalent.

```elixir
defmodule Example.Schema do
use Absinthe.Schema
use Absinthe.Federation.Schema

import Absinthe.Resolution.Helpers, only: [on_load: 2, dataloader: 2]

def context(ctx) do
loader =
Dataloader.new()
|> Dataloader.add_source(Example.Loader, Dataloader.Ecto.new(Example.Repo))

Map.put(ctx, :loader, loader)
end

def plugins do
[Absinthe.Middleware.Dataloader] ++ Absinthe.Plugin.defaults()
end

object :item do
key_fields("item_id")

# Using the dataloader/2 resolution helper
field :_resolve_reference, :item do
resolve dataloader(Example.Loader, fn _parent, args, _res ->
%{batch: {{:one, Example.Item}, %{}}, item: [item_id: args.item_id]}
kzlsakal marked this conversation as resolved.
Show resolved Hide resolved
end)
end
end

object :verbose_item do
key_fields("item_id")

# Using the on_load/2 resolution helper
field :_resolve_reference, :verbose_item do
resolve fn %{item_id: id}, %{context: %{loader: loader}} ->
batch_key = {:one, Example.Item, %{}}
item_key = [item_id: id]

loader
|> Dataloader.load(Example.Loader, batch_key, item_key)
|> on_load(fn loader ->
result = Dataloader.get(loader, Example.Loader, batch_key, item_key)
{:ok, result}
end)
end
end
end
```

### Resolving structs in \_entities queries

If you need to resolve your struct to a specific type in your schema you can implement the `Absinthe.Federation.Schema.EntityUnion.Resolver` protocol like this:
Expand Down
Loading
Loading