-
Notifications
You must be signed in to change notification settings - Fork 172
Add helper to insert values into context #230
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
Conversation
lib/absinthe/plug.ex
Outdated
context = | ||
absinthe | ||
|> Map.get(:context, %{}) | ||
|> Map.merge(Enum.into(assigns, %{})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The merge order here is probably counter to what you want. assigns
should be merged into the current context not the other way around, so that if you do want to override an existing assign it works as expected.
context = | |
absinthe | |
|> Map.get(:context, %{}) | |
|> Map.merge(Enum.into(assigns, %{})) | |
context = | |
assigns | |
|> Map.new | |
|> Map.merge(Map.get(absinthe, :assigns, %{})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My understanding is that right wins.
iex> Map.merge(%{foo: "bar"}, %{foo: "baz"})
%{foo: "baz"}
So the current code un-pipelined
context = Map.get(absinthe, :context, %{})
context = Map.merge(context, Map.new(assigns))
looks correct to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct! I got Map.merge and Enum.into backwards, my bad!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is a great idea, just a couple of changes and we'll have it merged in, thank you!
lib/absinthe/plug.ex
Outdated
end | ||
|
||
def assign_context(conn, assigns) do | ||
put_options(conn, context: Enum.into(assigns, %{})) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
put_options(conn, context: Enum.into(assigns, %{})) | |
put_options(conn, context: Map.new(assigns)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done! put_options
itself needs the same change. Let me know if you want those included here.
assert conn.private.absinthe.context.current_user.id == 1 | ||
assert conn.private.absinthe.context.foo == "bar" | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you add a test case that tests assigning a value that is already assigned?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.
@benwilson512 are you planning to released this in 1.5.1(-rc)? We'd love to use this :) |
Absinthe.Plug.put_options(conn, context: context)
overwrites any previously set context.This pull request adds
assign_context
which merges values instead. This allows multiple plugs that configure context to be composed.I considered naming it
put_in_context
but conventionallyput
andput_in
functions do not merge. Phoenix’sassign
appears closest in behaviour.