Rivet.Graphql (rivet v2.7.1)

View Source

Helper functions for Absinthe resolvers.

Summary

Functions

current_hostname(arg1)

iex> current_hostname(%{context: %{hostname: "narf"}})
{:ok, "narf"}
iex> current_hostname(:nope)
{:error, "No Host on session"}

error_string(errs)

iex> error_string([{:error, :authz}])
"Unauthorized"
iex> error_string("narf")
"narf"
iex> error_string(:authn)
"Unauthenticated"
iex> error_string(:authn)
"Unauthenticated"
iex> error_string(:narf)
"narf"
iex> error_string({:nobody, :expects})
"unexpected error, see logs"
iex> error_string({:error, "narf"})
"narf"

iex> err = RivetTestLib.Yoink.build(%{name: :nope})
iex> error_string(err)
"name is invalid"
iex> error_string({:error, err})
"name is invalid"

graphql_error(method, err, logargs \\ [])

graphql_log(method, args \\ [])

iex> graphql_log("narf")
:ok
iex> graphql_log(nil)
:ok

graphql_result(x, method \\ nil)

iex> graphql_result({:ok, :narf})
{:ok, :narf}
iex> graphql_result({:error, :args}, :narf)
{:error, "Invalid Arguments"}

graphql_status_result(state, key \\ nil)

iex> graphql_status_result({:error, "Unauthenticated"})
{:error, "Unauthenticated"}
iex> graphql_status_result({:ok, %{success: true, result: :narf}})
{:ok, %{success: true, result: :narf}}
iex> graphql_status_result({:ok, %{success: true, result: :narf}}, :narf)
{:ok, %{success: true, narf: :narf}}
iex> graphql_status_result({:ok, %{success: false, reason: :narf}})
{:ok, %{success: false, reason: :narf}}
iex> graphql_status_result({:ok, 100})
{:ok, %{success: true, result: 100}}
iex> graphql_status_result({:ok, 100}, :narf)
{:ok, %{narf: 100, success: true}}
iex> graphql_status_result({:error, :authz})
{:ok, %{reason: "Unauthorized", success: false}}
iex> graphql_status_result({:error, "narf", a: :b})
{:ok, %{success: false, reason: "narf"}}

graphql_status_results(x, key \\ :results)

Handle multi-results with total/matching tallys

iex> graphql_status_results({:ok, %{success: true, results: :narf}})
{:ok, %{success: true, results: :narf}}
iex> graphql_status_results({:ok, %{success: true, results: :narf}, :narf}, :key)
{:ok, %{success: true, results: :narf, key: :narf}}
iex> graphql_status_results({:ok, %{success: true, results: :narf}}, :results)
{:ok, %{success: true, results: :narf}}
iex> graphql_status_results({:ok, %{success: true, results: :narf}}, :narf)
{:ok, %{success: true, narf: :narf}}
iex> graphql_status_results({:ok, [:narf]}, :narf)
{:ok, %{success: true, narf: [:narf], total: 1, matching: 1}}
iex> graphql_status_results({:ok, :narf}, :narf)
{:ok, %{success: true, narf: [:narf], total: 1, matching: 1}}
iex> graphql_status_results({:error, :authn})
{:ok, %{success: false, reason: "Unauthenticated"}}

ok_as_list(pass)

iex> ok_as_list({:error, "Nothing found"})
{:ok, []}
iex> ok_as_list({:error, :not_found})
{:ok, []}
iex> ok_as_list({:ok, :narf})
{:ok, [:narf]}
iex> ok_as_list({:error, :narf})
{:error, :narf}

optional_arg(map, arg)

iex> optional_arg(%{}, :narf)
[]
iex> optional_arg(%{narf: :something}, :narf)
[narf: :something]

parse_atom(arg1)

iex> parse_atom(%{value: "narf"})
{:ok, :narf}
iex> parse_atom(%{value: 10})
:error
iex> parse_atom(:narf)
:error

parse_enum(arg1, enum)

Easy parser to connect Absinthe scalers with EctoEnum.