-
Notifications
You must be signed in to change notification settings - Fork 529
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
Fix description function calls #1005
Changes from all commits
20e463a
fb08870
792f1da
a8c4f1a
d0240ad
f390aeb
a1bb493
cc8a053
44fe0ae
4bd51be
a9bc536
f962b93
c29c262
65618af
bccf70a
a28730b
ec144b4
4f82158
e39ec48
95480bc
0aaf957
477b789
85fb8e7
f8917a5
4e0adf8
b3e0cbe
c192ec8
435246b
34f72ce
aac0a7e
ccb6cf3
dbcb7de
99ee813
46d5b66
aa538d8
de73de0
be8e3b8
ce742ef
7df5c50
25b347e
ef90531
1c8ac87
043040d
bac3f1b
4baef8f
9780fa0
e9403e8
c8feac5
a235e43
93c21a6
fba1663
3e7f316
2e193de
5ef5b90
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -212,7 +212,12 @@ defmodule Absinthe.Schema.Notation do | |
|
||
__CALLER__ | ||
|> recordable!(:object, @placement[:object]) | ||
|> record!(Schema.ObjectTypeDefinition, identifier, attrs, block) | ||
|> record!( | ||
Schema.ObjectTypeDefinition, | ||
identifier, | ||
attrs |> Keyword.update(:description, nil, &wrap_in_unquote/1), | ||
block | ||
) | ||
end | ||
|
||
@placement {:interfaces, [under: [:object]]} | ||
|
@@ -407,6 +412,7 @@ defmodule Absinthe.Schema.Notation do | |
|> expand_ast(caller) | ||
|> Keyword.delete(:args) | ||
|> Keyword.delete(:meta) | ||
|> Keyword.update(:description, nil, &wrap_in_unquote/1) | ||
|> handle_deprecate | ||
|
||
{attrs, block} | ||
|
@@ -719,7 +725,7 @@ defmodule Absinthe.Schema.Notation do | |
defmacro scalar(identifier, attrs, do: block) do | ||
__CALLER__ | ||
|> recordable!(:scalar, @placement[:scalar]) | ||
|> record!(Schema.ScalarTypeDefinition, identifier, attrs, block) | ||
|> record_scalar!(identifier, attrs, block) | ||
end | ||
|
||
@doc """ | ||
|
@@ -730,13 +736,13 @@ defmodule Absinthe.Schema.Notation do | |
defmacro scalar(identifier, do: block) do | ||
__CALLER__ | ||
|> recordable!(:scalar, @placement[:scalar]) | ||
|> record!(Schema.ScalarTypeDefinition, identifier, [], block) | ||
|> record_scalar!(identifier, [], block) | ||
end | ||
|
||
defmacro scalar(identifier, attrs) do | ||
__CALLER__ | ||
|> recordable!(:scalar, @placement[:scalar]) | ||
|> record!(Schema.ScalarTypeDefinition, identifier, attrs, nil) | ||
|> record_scalar!(identifier, attrs, nil) | ||
end | ||
|
||
@placement {:serialize, [under: [:scalar]]} | ||
|
@@ -934,7 +940,12 @@ defmodule Absinthe.Schema.Notation do | |
defmacro input_object(identifier, attrs \\ [], do: block) do | ||
__CALLER__ | ||
|> recordable!(:input_object, @placement[:input_object]) | ||
|> record!(Schema.InputObjectTypeDefinition, identifier, attrs, block) | ||
|> record!( | ||
Schema.InputObjectTypeDefinition, | ||
identifier, | ||
attrs |> Keyword.update(:description, nil, &wrap_in_unquote/1), | ||
block | ||
) | ||
end | ||
|
||
# UNIONS | ||
|
@@ -965,7 +976,12 @@ defmodule Absinthe.Schema.Notation do | |
defmacro union(identifier, attrs \\ [], do: block) do | ||
__CALLER__ | ||
|> recordable!(:union, @placement[:union]) | ||
|> record!(Schema.UnionTypeDefinition, identifier, attrs, block) | ||
|> record!( | ||
Schema.UnionTypeDefinition, | ||
identifier, | ||
attrs |> Keyword.update(:description, nil, &wrap_in_unquote/1), | ||
block | ||
) | ||
end | ||
|
||
@placement {:types, [under: [:union]]} | ||
|
@@ -1069,10 +1085,11 @@ defmodule Absinthe.Schema.Notation do | |
|> expand_ast(env) | ||
|> Keyword.update(:values, [], fn values -> | ||
Enum.map(values, fn ident -> | ||
value_attrs = handle_enum_value_attrs(ident, module: env.module) | ||
value_attrs = handle_enum_value_attrs(ident, [], env) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. bug fix: |
||
struct!(Schema.EnumValueDefinition, value_attrs) | ||
end) | ||
end) | ||
|> Keyword.update(:description, nil, &wrap_in_unquote/1) | ||
end | ||
|
||
@placement {:value, [under: [:enum]]} | ||
|
@@ -1296,6 +1313,7 @@ defmodule Absinthe.Schema.Notation do | |
raw_attrs | ||
|> Keyword.put_new(:name, to_string(identifier)) | ||
|> Keyword.put_new(:type, type) | ||
|> Keyword.update(:description, nil, &wrap_in_unquote/1) | ||
|> handle_deprecate | ||
end | ||
|
||
|
@@ -1319,6 +1337,7 @@ defmodule Absinthe.Schema.Notation do | |
attrs | ||
|> Keyword.put(:identifier, identifier) | ||
|> Keyword.put_new(:name, to_string(identifier)) | ||
|> Keyword.update(:description, nil, &wrap_in_unquote/1) | ||
|
||
scoped_def(env, Schema.DirectiveDefinition, identifier, attrs, block) | ||
end | ||
|
@@ -1401,16 +1420,27 @@ defmodule Absinthe.Schema.Notation do | |
scoped_def(env, :enum, identifier, attrs, block) | ||
end | ||
|
||
defp reformat_description(text), do: String.trim(text) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like we lost the |
||
|
||
@doc false | ||
# Record a description in the current scope | ||
def record_description!(env, text_block) do | ||
text = reformat_description(text_block) | ||
text = wrap_in_unquote(text_block) | ||
|
||
put_attr(env.module, {:desc, text}) | ||
end | ||
|
||
def handle_enum_value_attrs(identifier, raw_attrs) do | ||
@doc false | ||
# Record a scalar | ||
def record_scalar!(env, identifier, attrs, block_or_nil) do | ||
record!( | ||
env, | ||
Schema.ScalarTypeDefinition, | ||
identifier, | ||
attrs |> Keyword.update(:description, nil, &wrap_in_unquote/1), | ||
block_or_nil | ||
) | ||
end | ||
|
||
def handle_enum_value_attrs(identifier, raw_attrs, env) do | ||
value = | ||
case Keyword.get(raw_attrs, :as, identifier) do | ||
value when is_tuple(value) -> | ||
|
@@ -1423,18 +1453,19 @@ defmodule Absinthe.Schema.Notation do | |
end | ||
|
||
raw_attrs | ||
|> expand_ast(raw_attrs) | ||
|> expand_ast(env) | ||
|> Keyword.put(:identifier, identifier) | ||
|> Keyword.put(:value, value) | ||
|> Keyword.put_new(:name, String.upcase(to_string(identifier))) | ||
|> Keyword.delete(:as) | ||
|> Keyword.update(:description, nil, &wrap_in_unquote/1) | ||
|> handle_deprecate | ||
end | ||
|
||
@doc false | ||
# Record an enum value in the current scope | ||
def record_value!(env, identifier, raw_attrs) do | ||
attrs = handle_enum_value_attrs(identifier, raw_attrs) | ||
attrs = handle_enum_value_attrs(identifier, raw_attrs, env) | ||
record!(env, Schema.EnumValueDefinition, identifier, attrs, []) | ||
end | ||
|
||
|
@@ -1445,7 +1476,7 @@ defmodule Absinthe.Schema.Notation do | |
values | ||
|> expand_ast(env) | ||
|> Enum.map(fn ident -> | ||
value_attrs = handle_enum_value_attrs(ident, module: env.module) | ||
value_attrs = handle_enum_value_attrs(ident, [], env) | ||
struct!(Schema.EnumValueDefinition, value_attrs) | ||
end) | ||
|
||
|
@@ -1484,6 +1515,14 @@ defmodule Absinthe.Schema.Notation do | |
put_attr(env.module, {:middleware, [new_middleware]}) | ||
end | ||
|
||
# We wrap the value (from the user) in an `unquote` call, so that when the schema `blueprint` is | ||
# placed into `__absinthe_blueprint__` via `unquote(Macro.escape(blueprint, unquote: true))` the | ||
# value get's unquoted. This allows us to evaluate function calls in the scope of the schema | ||
# module. | ||
defp wrap_in_unquote(value) do | ||
{:unquote, [], [value]} | ||
end | ||
|
||
# ------------------------------ | ||
|
||
@doc false | ||
|
@@ -1866,6 +1905,18 @@ defmodule Absinthe.Schema.Notation do | |
|
||
defp expand_ast(ast, env) do | ||
Macro.prewalk(ast, fn | ||
# We don't want to expand `@bla` into `Module.get_attribute(module, @bla)` because this | ||
# function call will fail if the module is already compiled. Remember that the ast gets put | ||
# into a generated `__absinthe_blueprint__` function which is called at "__after_compile__" | ||
# time. This will be after a module has been compiled if there are multiple modules in the | ||
# schema (in the case of an `import_types`). | ||
# | ||
# Also see test "test/absinthe/type/import_types_test.exs" | ||
# "__absinthe_blueprint__ is callable at runtime even if there is a module attribute" | ||
# and it's comment for more information | ||
{:@, _, _} = node -> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I haven't quite worked out why this work, because I was sure this would fail with:
However based on my local testing:
Works great! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The value assigned to |
||
node | ||
|
||
{_, _, _} = node -> | ||
Macro.expand(node, env) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see function
wrap_in_unquote
for a description.this
Keyword.update(:description, nil, &wrap_in_unquote/1)
is the fix pasted in all the places necessary. basically is the only change tonotation.ex