From 573283e8a6affea4bccd42f3e667e257917517e7 Mon Sep 17 00:00:00 2001 From: Kaden Wilkinson <7799267+kdawgwilk@users.noreply.github.com> Date: Tue, 17 May 2022 00:24:32 -0500 Subject: [PATCH] Allow single atom for macro directives --- lib/absinthe/schema/notation.ex | 46 +++++++++---------- .../schema/type_system_directive_test.exs | 10 ++-- 2 files changed, 29 insertions(+), 27 deletions(-) diff --git a/lib/absinthe/schema/notation.ex b/lib/absinthe/schema/notation.ex index 84efc78611..5c8c48826a 100644 --- a/lib/absinthe/schema/notation.ex +++ b/lib/absinthe/schema/notation.ex @@ -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 @@ -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 @@ -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 @@ -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 @@ -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, diff --git a/test/absinthe/schema/type_system_directive_test.exs b/test/absinthe/schema/type_system_directive_test.exs index 6251d6a25a..b244038bcd 100644 --- a/test/absinthe/schema/type_system_directive_test.exs +++ b/test/absinthe/schema/type_system_directive_test.exs @@ -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 @@ -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") @@ -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 @@ -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") {