Skip to content

Commit

Permalink
general updates and formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
benwilson512 committed Nov 29, 2018
1 parent afdaf8b commit 5791b7a
Show file tree
Hide file tree
Showing 20 changed files with 385 additions and 200 deletions.
7 changes: 7 additions & 0 deletions .formatter.exs
@@ -0,0 +1,7 @@
[
inputs: [
"{config,lib,priv,test}/**/*.{ex,exs}",
"{mix,.formatter}.exs"
],
import_deps: [:absinthe]
]
2 changes: 2 additions & 0 deletions .gitignore
@@ -1,4 +1,6 @@
# The directory Mix will write compiled artifacts to.
.elixir_ls

/_build

# If you run "mix test --cover", coverage assets end up here.
Expand Down
4 changes: 3 additions & 1 deletion config/config.exs
@@ -1,3 +1,5 @@
use Mix.Config

import_config "#{Mix.env}.exs"
config :phoenix, :json_library, Jason

import_config "#{Mix.env()}.exs"
56 changes: 32 additions & 24 deletions lib/absinthe/phoenix/channel.ex
Expand Up @@ -29,9 +29,10 @@ defmodule Absinthe.Phoenix.Channel do

absinthe_config =
put_in(absinthe_config[:opts], opts)
|> Map.update(:schema, schema, &(&1))
|> Map.update(:schema, schema, & &1)

absinthe_config = Map.put(absinthe_config, :pipeline, pipeline || {__MODULE__, :default_pipeline})
absinthe_config =
Map.put(absinthe_config, :pipeline, pipeline || {__MODULE__, :default_pipeline})

socket = socket |> assign(:absinthe, absinthe_config)
{:ok, socket}
Expand All @@ -51,36 +52,41 @@ defmodule Absinthe.Phoenix.Channel do
query,
config.schema,
[],
opts,
opts
})

pipeline = Map.get(config, :pipeline)

{reply, socket} = case run(query, config.schema, pipeline, opts) do
{:ok, %{"subscribed" => topic}, context} ->
:ok = Phoenix.PubSub.subscribe(socket.pubsub_server, topic, [
fastlane: {socket.transport_pid, socket.serializer, []},
link: true,
])
socket = Absinthe.Phoenix.Socket.put_options(socket, context: context)
{{:ok, %{subscriptionId: topic}}, socket}

{:ok, %{data: _} = reply, context} ->
socket = Absinthe.Phoenix.Socket.put_options(socket, context: context)
{{:ok, reply}, socket}

{:ok, %{errors: _} = reply, context} ->
socket = Absinthe.Phoenix.Socket.put_options(socket, context: context)
{{:error, reply}, socket}

{:error, reply} ->
{reply, socket}
end
{reply, socket} =
case run(query, config.schema, pipeline, opts) do
{:ok, %{"subscribed" => topic}, context} ->
:ok =
Phoenix.PubSub.subscribe(
socket.pubsub_server,
topic,
fastlane: {socket.transport_pid, socket.serializer, []},
link: true
)

socket = Absinthe.Phoenix.Socket.put_options(socket, context: context)
{{:ok, %{subscriptionId: topic}}, socket}

{:ok, %{data: _} = reply, context} ->
socket = Absinthe.Phoenix.Socket.put_options(socket, context: context)
{{:ok, reply}, socket}

{:ok, %{errors: _} = reply, context} ->
socket = Absinthe.Phoenix.Socket.put_options(socket, context: context)
{{:error, reply}, socket}

{:error, reply} ->
{reply, socket}
end

Logger.debug(fn ->
"""
-- Absinthe Phoenix Reply --
#{inspect reply}
#{inspect(reply)}
----------------------------
"""
end)
Expand All @@ -103,9 +109,11 @@ defmodule Absinthe.Phoenix.Channel do

defp run(document, schema, pipeline, options) do
{module, fun} = pipeline

case Absinthe.Pipeline.run(document, apply(module, fun, [schema, options])) do
{:ok, %{result: result, execution: res}, _phases} ->
{:ok, result, res.context}

{:error, msg, _phases} ->
{:error, msg}
end
Expand Down
11 changes: 6 additions & 5 deletions lib/absinthe/phoenix/channel_test.ex
Expand Up @@ -3,7 +3,7 @@ defmodule Absinthe.Phoenix.SubscriptionTest do
Convenience functions for subscription tests
"""

defmacro __using__([schema: schema]) do
defmacro __using__(schema: schema) do
quote do
setup_all do
Absinthe.Test.prime(unquote(schema))
Expand All @@ -14,15 +14,16 @@ defmodule Absinthe.Phoenix.SubscriptionTest do
end

def join_absinthe(socket) do
with {:ok, _, socket} <- Phoenix.ChannelTest.subscribe_and_join(socket, "__absinthe__:control", %{}) do
with {:ok, _, socket} <-
Phoenix.ChannelTest.subscribe_and_join(socket, "__absinthe__:control", %{}) do
{:ok, socket}
end
end

def push_doc(socket, doc, opts \\ []) do
Phoenix.ChannelTest.push socket, "doc", %{
Phoenix.ChannelTest.push(socket, "doc", %{
"query" => doc,
"variables" => opts[:variables],
}
"variables" => opts[:variables]
})
end
end
66 changes: 49 additions & 17 deletions lib/absinthe/phoenix/controller.ex
Expand Up @@ -172,6 +172,7 @@ defmodule Absinthe.Phoenix.Controller do

defmacro __using__(opts \\ []) do
schema = Keyword.fetch!(opts, :schema)

quote do
@behaviour unquote(__MODULE__)
@before_compile unquote(__MODULE__)
Expand All @@ -181,24 +182,32 @@ defmodule Absinthe.Phoenix.Controller do

@absinthe_schema unquote(schema)

plug unquote(__MODULE__).Action, unquote(opts)
plug(unquote(__MODULE__).Action, unquote(opts))

@impl unquote(__MODULE__)
@spec cast_param(value :: any, target_type :: Absinthe.Type.t, schema :: Absinthe.Schema.t) :: any
@spec cast_param(
value :: any,
target_type :: Absinthe.Type.t(),
schema :: Absinthe.Schema.t()
) :: any
def cast_param(value, %Absinthe.Type.NonNull{of_type: inner_target_type}, schema) do
cast_param(value, inner_target_type, schema)
end
def cast_param(values, %Absinthe.Type.List{of_type: inner_target_type}, schema) when is_list(values) do

def cast_param(values, %Absinthe.Type.List{of_type: inner_target_type}, schema)
when is_list(values) do
for value <- values do
cast_param(value, inner_target_type, schema)
end
end

def cast_param(value, %Absinthe.Type.InputObject{} = target_type, schema) when is_map(value) do
for {name, field_value} <- value, into: %{} do
case Map.values(target_type.fields) |> Enum.find(&(to_string(&1.identifier) == name)) do
nil ->
# Pass through value for error reporting by validations
{name, field_value}

field ->
{
name,
Expand All @@ -207,36 +216,48 @@ defmodule Absinthe.Phoenix.Controller do
end
end
end
def cast_param(value, %Absinthe.Type.Scalar{__reference__: %{identifier: :integer}}, _schema) when is_binary(value) do

def cast_param(
value,
%Absinthe.Type.Scalar{__reference__: %{identifier: :integer}},
_schema
)
when is_binary(value) do
case Integer.parse(value) do
{result, _} ->
result

:error ->
# Pass through value for error reporting by validations
value
end
end
def cast_param(value, %Absinthe.Type.Scalar{__reference__: %{identifier: :float}}, _schema) when is_binary(value) do

def cast_param(value, %Absinthe.Type.Scalar{__reference__: %{identifier: :float}}, _schema)
when is_binary(value) do
case Float.parse(value) do
{result, _} ->
result

:error ->
# Pass through value for error reporting by validations
value
end
end

def cast_param(value, target_type, schema) do
value
end
defoverridable [cast_param: 3]

defoverridable cast_param: 3

@impl unquote(__MODULE__)
@spec absinthe_pipeline(schema :: Absinthe.Schema.t, Keyword.t) :: Absinthe.Pipeline.t
@spec absinthe_pipeline(schema :: Absinthe.Schema.t(), Keyword.t()) :: Absinthe.Pipeline.t()
def absinthe_pipeline(schema, opts) do
unquote(__MODULE__).default_pipeline(schema, opts)
end
defoverridable [absinthe_pipeline: 2]

defoverridable absinthe_pipeline: 2
end
end

Expand All @@ -251,45 +272,53 @@ defmodule Absinthe.Phoenix.Controller do
schema
|> Pipeline.for_document(options)
|> Pipeline.from(Phase.Document.Variables)
|> Pipeline.insert_before(Phase.Document.Variables, {Absinthe.Phoenix.Controller.Blueprint, options})
|> Pipeline.insert_before(
Phase.Document.Variables,
{Absinthe.Phoenix.Controller.Blueprint, options}
)
|> Pipeline.without(Phase.Document.Validation.ScalarLeafs)
|> Pipeline.insert_after(Phase.Document.Directives, {Absinthe.Phoenix.Controller.Action, options})
|> Pipeline.insert_after(
Phase.Document.Directives,
{Absinthe.Phoenix.Controller.Action, options}
)
end

defmacro __before_compile__(env) do
actions = Module.get_attribute(env.module, :graphql_actions)
provides = for {name, doc, _} <- actions, do: {name, doc}
schemas = for {name, _, schema} <- actions, do: {to_string(name), schema}
quote do

quote do
defmodule GraphQL do
use Absinthe.Plug.DocumentProvider.Compiled
provide unquote(provides)
provide(unquote(provides))

@absinthe_schemas %{unquote_splicing(schemas)}
def lookup_schema(name) do
@absinthe_schemas[name]
end

end

end
end

@doc false
def register_graphql_action(env, :def, name, _args, _guards, _body) do
default_schema = Module.get_attribute(env.module, :absinthe_schema)

case Module.get_attribute(env.module, :graphql) do
nil ->
:ok

{document, schema} ->
Module.delete_attribute(env.module, :graphql)
Module.put_attribute(env.module, :graphql_actions, {name, document, schema})

document ->
Module.delete_attribute(env.module, :graphql)
Module.put_attribute(env.module, :graphql_actions, {name, document, default_schema})
end
end

def register_graphql_action(_env, _kind, _name, _args, _guards, _body) do
:ok
end
Expand Down Expand Up @@ -317,14 +346,17 @@ defmodule Absinthe.Phoenix.Controller do
unchanged so that Absinthe's usual validation logic can report it as
invalid.
"""
@callback cast_param(value :: any, target_type :: Absinthe.Type.t, schema :: Absinthe.Schema.t) :: any
@callback cast_param(
value :: any,
target_type :: Absinthe.Type.t(),
schema :: Absinthe.Schema.t()
) :: any

@doc """
Customize the Absinthe processing pipeline.
Only implement this function if you need to change the pipeline used
to process documents.
"""
@callback absinthe_pipeline(schema :: Absinthe.Schema.t, Keyword.t) :: Absinthe.Pipeline.t

@callback absinthe_pipeline(schema :: Absinthe.Schema.t(), Keyword.t()) :: Absinthe.Pipeline.t()
end

0 comments on commit 5791b7a

Please sign in to comment.