Skip to content

Commit

Permalink
Merge pull request #1171 from kdawgwilk/kaden/directive_ergonomics
Browse files Browse the repository at this point in the history
Allow single atom for macro directives
  • Loading branch information
benwilson512 authored Jun 7, 2022
2 parents 2768d10 + 573283e commit 9539806
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 27 deletions.
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

0 comments on commit 9539806

Please sign in to comment.