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

Add dataloader/2 function to helpers #898

Merged
Merged
Changes from all 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
40 changes: 39 additions & 1 deletion lib/absinthe/resolution/helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,47 @@ defmodule Absinthe.Resolution.Helpers do
"""
@spec dataloader(Dataloader.source_name()) :: dataloader_key_fun()
def dataloader(source) do
dataloader(source, [])
end

@doc """
Resolve a field with a dataloader source.

This function is not imported by default. To make it available in your module do

```
import Absinthe.Resolution.Helpers
```

Same as `dataloader/3`, but it infers the resource name from the field name. For `opts` see
`dataloader/3` on what options can be passed in.

## Examples

```
object :user do
field :posts, list_of(:post),
resolve: dataloader(Blog, args: %{deleted: false})

field :organization, :organization do
resolve dataloader(Accounts, use_parent: false)
end

field(:account_active, non_null(:boolean), resolve: dataloader(
Accounts, callback: fn account, _parent, _args ->
{:ok, account.active}
end
)
)
end
```

"""
@spec dataloader(Dataloader.source_name(), [dataloader_opt]) :: dataloader_key_fun()
def dataloader(source, opts) when is_list(opts) do
fn parent, args, %{context: %{loader: loader}} = res ->
resource = res.definition.schema_node.identifier
do_dataloader(loader, source, resource, args, parent, [])
do_dataloader(loader, source, resource, args, parent, opts)
end
end

Expand Down