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

Added Dataloader.has_source? so registered sources can be checked #28

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/dataloader.ex
Expand Up @@ -139,6 +139,11 @@ defmodule Dataloader do
Enum.any?(loader.sources, fn {_name, source} -> Source.pending_batches?(source) end)
end

@spec has_source?(t, atom()) :: boolean
def has_source?(%{sources: sources}, source) do
Map.has_key?(sources, source)
end

defp get_source(loader, source_name) do
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe instead we should just make get_source not private here? Then you could still do if !Dataloader.get_source etc.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reasoning behind using a separate function was that get_source will currently raise, and it feels a bit unfriendly to make people wrap their checks in a catch. That said, is there any reason that it needs to raise?

loader.sources[source_name] || raise "Source does not exist: #{inspect(source_name)}"
end
Expand Down
10 changes: 10 additions & 0 deletions test/dataloader_test.exs
Expand Up @@ -18,4 +18,14 @@ defmodule DataloaderTest do

assert log =~ "boom"
end

test "that already added sources can be identified" do
source = Dataloader.KV.new(fn _, ids -> Enum.with_index(ids) end)

loader =
Dataloader.new
|> Dataloader.add_source(:foo, source)

assert Dataloader.has_source?(loader, :foo)
end
end