absinthe-graphql/absinthe

Providing functions to meta no longer works

Closed this issue · 7 comments

Environment

  • Elixir version (elixir -v): 1.10.2
  • Absinthe version (mix deps | grep absinthe): 1.5.1
  • Client Framework and version (Relay, Apollo, etc): Not relevant

Expected behavior

I expected the following to work (works in 1.4.16):

object :user do
  field :email, :string do
    meta([private: & !&1.is_email_public])
  end
end

When inspecting the meta[:private] field in 1.4.16, in a middleware:

def call(resolution, [object: %Type.Object{fields: fields} = object, field: field]) do
  fields[field] |> Type.meta(:private) |> IO.inspect() 
end

I get the function: #Function<5.7636899/1 in Rajska.FieldAuthorizationTest.Schema.__absinthe_type__/1>

Actual behavior

In 1.5.1, however, with the same code I'm getting the AST instead of the function:

{:&, [line: 84],
 [
   {:case, [optimize_boolean: true],
    [
      {{:., [line: 84], [{:&, [line: 84], [1]}, :is_email_public]},
       [no_parens: true, line: 84], []},
      [
        do: [
          {:->, [],
           [
             [
               {:when, [],
                [
                  {:x, [counter: {Rajska.FieldAuthorizationTest.Schema, 61}],
                   Kernel},
                  {{:., [], [:erlang, :orelse]}, [],
                   [
                     {{:., [], [:erlang, :"=:="]}, [],
                      [
                        {:x,
                         [counter: {Rajska.FieldAuthorizationTest.Schema, 61}],
                         Kernel},
                        false
                      ]},
                     {{:., [], [:erlang, :"=:="]}, [],
                      [
                        {:x,
                         [counter: {Rajska.FieldAuthorizationTest.Schema, 61}],
                         Kernel},
                        nil
                      ]}
                   ]}
                ]}
             ],
             true
           ]},
          {:->, [], [[{:_, [], Kernel}], false]}
        ]
      ]
    ]}
 ]}

Was this change intentional? By providing the AST instead of the actual value, it's now hard to use more complex structures in the meta, such as functions and maps, which is something some libraries depend upon.

any news about this?

Hi, I am wondering if the work @Hermanverschooten did to fix meta on fields would not actually fix this issue as well. Can you try to reproduce on master ? Thanks.

@tlvenn do functions work if you use the persistent term backend?

@tlvenn I tried it again with the master version and still getting the same error, both when using the shortcut syntax and the meta/1 syntax 😢

I'm still not sure if this is how it's supposed to work, but I fixed my problem by evaluating the AST at runtime before trying to execute the function: jungsoft/rajska@01f716a.

@benwilson512 Should we evaluate the AST inside Absinthe or is it expected that the library clients do that on their own, like I did in that commit?

Duplicate issue that was already closed for this: #737

I still think it's a valuable property to keep, but apparently the complexity wasn't worth it, from @benwilson512 .

Hey folks. I believe as we prep to switch to using :persistent_term as the backend in the next release this will be come possible.

This can be accomplished in 1.5 using hydrate. Here's an example of dynamic Enum value hydration in the absinthe tests:

https://github.com/absinthe-graphql/absinthe/blob/master/test/absinthe/schema/hydrate_dynamic_values_test.exs

In your case, you can set the meta:

{:meta, dynamic_meta()}

Documented here: https://hexdocs.pm/absinthe/Absinthe.Schema.html#c:hydrate/2