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

Adds get_option/2 #260

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
18 changes: 18 additions & 0 deletions lib/absinthe/plug.ex
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,24 @@ defmodule Absinthe.Plug do
@doc """
Sets the options for a given GraphQL document execution.

## Examples

iex> Absinthe.Plug.put_options(conn, context: :my_context)
iex> Absinthe.Plug.get_option(conn, :context)
:my_context
"""
@spec get_option(Plug.Conn.t(), atom()) :: any()
def get_option(%Plug.Conn{private: %{absinthe: absinthe}}, key) do
Map.get(absinthe, key)
end

def get_option(_, _) do
nil
end

@doc """
Sets the options for a given GraphQL document execution.

## Examples

iex> Absinthe.Plug.put_options(conn, context: %{current_user: user})
Expand Down
26 changes: 26 additions & 0 deletions test/lib/absinthe/plug_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -619,6 +619,32 @@ defmodule Absinthe.PlugTest do
end
end

describe "get_option/2" do
test "It gets the current options for the given key" do
context = %{current_user: %{id: 1}}

conn =
conn(:post, "/")
|> Absinthe.Plug.put_options(context: context)

assert Absinthe.Plug.get_option(conn, :context) == context
end

test "It returns nil if the key isn't found" do
context = %{current_user: %{id: 1}}

conn =
conn(:post, "/")
|> Absinthe.Plug.put_options(context: context)

assert Absinthe.Plug.get_option(conn, :doesnt_exist) == nil
end

test "It returns nil if there isn't aren't any options yet " do
assert Absinthe.Plug.get_option(conn(:post, "/"), :context) == nil
end
end

describe "assign_context/2" do
test "with a pristine connection it sets the values as provided" do
conn =
Expand Down