Skip to content

Commit

Permalink
Rework how entities field is resolved to:
Browse files Browse the repository at this point in the history
- Support Async resolution with 0-arity function
- Support Dataloader.Ecto
- Support function capture resolution
  • Loading branch information
maciej-szlosarczyk committed Dec 18, 2024
1 parent 3e9eb92 commit c1c24ec
Show file tree
Hide file tree
Showing 11 changed files with 420 additions and 163 deletions.
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]}
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
13 changes: 13 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import Config

config :absinthe_federation, ecto_repos: [ExampleRepo]

config :absinthe_federation, ExampleRepo,
adapter: Ecto.Adapters.Postgres,
username: "postgres",
password: "postgres",
database: "example_test",
hostname: "localhost",
pool_size: 20,
pool: Ecto.Adapters.SQL.Sandbox,
log: :info
Loading

0 comments on commit c1c24ec

Please sign in to comment.