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

Allow single atom for macro directives #1171

Merged
Merged
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
46 changes: 22 additions & 24 deletions lib/absinthe/schema/notation.ex
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,7 @@ defmodule Absinthe.Schema.Notation do
end

defmacro object(identifier, attrs, do: block) do
block =
for {identifier, args} <- build_directives(attrs) do
quote do
directive(unquote(identifier), unquote(args))
end
end ++ block
block = block_from_directive_attrs(attrs, block)

{attrs, block} =
case Keyword.pop(attrs, :meta) do
Expand Down Expand Up @@ -439,12 +434,7 @@ defmodule Absinthe.Schema.Notation do
end
end

block =
for {identifier, args} <- build_directives(attrs) do
quote do
directive(unquote(identifier), unquote(args))
end
end ++ block
block = block_from_directive_attrs(attrs, block)

block =
case Keyword.get(attrs, :meta) do
Expand Down Expand Up @@ -1539,12 +1529,7 @@ defmodule Absinthe.Schema.Notation do
defp reason(msg), do: raise(ArgumentError, "Invalid reason: #{msg}")

def handle_arg_attrs(identifier, type, raw_attrs) do
block =
for {identifier, args} <- build_directives(raw_attrs) do
quote do
directive(unquote(identifier), unquote(args))
end
end
block = block_from_directive_attrs(raw_attrs)

attrs =
raw_attrs
Expand Down Expand Up @@ -1731,12 +1716,7 @@ defmodule Absinthe.Schema.Notation do
def handle_enum_value_attrs(identifier, raw_attrs, env) do
value = Keyword.get(raw_attrs, :as, identifier)

block =
for {identifier, args} <- build_directives(raw_attrs) do
quote do
directive(unquote(identifier), unquote(args))
end
end
block = block_from_directive_attrs(raw_attrs)

attrs =
raw_attrs
Expand Down Expand Up @@ -2244,6 +2224,24 @@ defmodule Absinthe.Schema.Notation do
{node, ast ++ acc}
end

defp block_from_directive_attrs(attrs, block \\ []) do
block =
for {identifier, args} <- build_directives(attrs) do
quote do
directive(unquote(identifier), unquote(args))
end
end ++ block

block =
for directive_name <- build_directives(attrs), is_atom(directive_name) do
quote do
directive(unquote(directive_name), [])
end
end ++ block

block
end

defp split_definitions(definitions) do
Enum.reduce(definitions, {[], [], []}, fn definition,
{directive_definitions, type_definitions,
Expand Down
10 changes: 7 additions & 3 deletions test/absinthe/schema/type_system_directive_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ defmodule Absinthe.Schema.TypeSystemDirectiveTest do
field :str, :string
end

directive :external do
on [:field_definition]
end

directive :feature do
arg :name, non_null(:string)
arg :number, :integer
Expand Down Expand Up @@ -53,7 +57,7 @@ defmodule Absinthe.Schema.TypeSystemDirectiveTest do
}

type Post @feature(name: ":object", number: 3, complex: {str: "foo"}) {
name: String @deprecated(reason: "Bye")
name: String @deprecated(reason: "Bye") @external
}

scalar SweetScalar @feature(name: ":scalar")
Expand Down Expand Up @@ -148,7 +152,7 @@ defmodule Absinthe.Schema.TypeSystemDirectiveTest do
is_type_of fn _ -> true end
interface :animal
field :leg_count, non_null(:integer)
field :name, non_null(:string)
field :name, non_null(:string), directives: [:external]
end

input_object :search_filter do
Expand Down Expand Up @@ -207,7 +211,7 @@ defmodule Absinthe.Schema.TypeSystemDirectiveTest do

type Dog implements Animal {
legCount: Int!
name: String!
name: String! @external
}

enum Category @feature(name: ":enum") {
Expand Down