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

Accessing values from the context and passing in as argument #117

Closed
morgz opened this issue Mar 4, 2021 · 1 comment
Closed

Accessing values from the context and passing in as argument #117

morgz opened this issue Mar 4, 2021 · 1 comment

Comments

@morgz
Copy link

morgz commented Mar 4, 2021

I'm trying to filter an objects association with the id of my current user which I put in the context via middleware.

I am trying to find, and failing, a way to pass this to the dataloader method I have setup in the context.

  def query(UserExerciseFavourite, args) do
    # I want the user id in args
    #...
  end

I can pass arguments this way:

object :exercise do
    # I can pass arguments this way
    field :user_exercise_favourites, list_of(:user_exercise_favourite) do
      resolve(dataloader(Exercises, :user_exercise_favourites, args: %{foo: "bar"}))
    end
end

And I can access the id this way, but I can't find the way to then pass it to the method like I can above!

    field :user_exercise_favourites, list_of(:user_exercise_favourite) do
      resolve fn exercise, _, %{context: %{loader: loader} = context} ->
        user_id = context |> Map.get(:current_user)
        Logger.debug("\n\n User: #{inspect user_id}\n\n")
        # How can I take my user out of the context and pass to the Dataloader?
        loader
        |> on_load(fn loader ->
          {:ok, Dataloader.load(loader, Exercises, :user_exercise_favourites, exercise)}
        end)
      end
    end

Any tips please!

@benwilson512
Copy link
Contributor

My @morgz, the best way to do this is actually at the point where you initialize the dataloader instance itself. See default_params in https://hexdocs.pm/dataloader/Dataloader.Ecto.html#content.

However you could also pass them in by doing load(loader, Exercises, {:user_excercise_favorites, %{current_user: user}}, exercise)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants