Not sure how to ask this other than show an example…
Consider a GraphQL query like this:
{
posts {
comment_authors
comments {
author
}
}
}
And we have a comment.author
resolver that uses a helper function:
field :author, :string, resolve: fn comment, _args, res ->
res.context.loader
|> load_author(comment, fn _loader, author ->
{:ok, author}
end)
end
def load_author(loader, comment, f) do
loader
|> load(Repo, :author, comment)
|> on_load(fn loader ->
author = get(Repo, :author, comment)
f.(loader, author)
end)
end
How can I reuse that helper function for posts.comment_authors
?
field :comment_authors, resolve: fn post, _args, res ->
res.context.loader
|> load(Repo, :comments, post)
|> on_load(fn loader ->
comments = get(Repo, :comments, post)
authors = Enum.map(comments, fn comment ->
load_author(loader, comment, fn loader, author ->
author
end)
end)
{:ok, authors}
end)
end
Obviously doesn’t work; authors
will be a list of functions.
What is the best way to accomplish this goal? Thanks for the help.