From b8958efe81c5b62bb8b92a2caf603ba2df156bb8 Mon Sep 17 00:00:00 2001 From: Eli Date: Mon, 11 Oct 2021 18:47:37 -0500 Subject: [PATCH] Adds get_option/2 --- lib/absinthe/plug.ex | 18 ++++++++++++++++++ test/lib/absinthe/plug_test.exs | 26 ++++++++++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/lib/absinthe/plug.ex b/lib/absinthe/plug.ex index e1e6eb7..abb214d 100644 --- a/lib/absinthe/plug.ex +++ b/lib/absinthe/plug.ex @@ -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}) diff --git a/test/lib/absinthe/plug_test.exs b/test/lib/absinthe/plug_test.exs index 975ebc6..6c2bf45 100644 --- a/test/lib/absinthe/plug_test.exs +++ b/test/lib/absinthe/plug_test.exs @@ -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 =