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

Providing function to descriptions no longer works #935

Closed
bjunc opened this issue May 26, 2020 · 3 comments · Fixed by #1005
Closed

Providing function to descriptions no longer works #935

bjunc opened this issue May 26, 2020 · 3 comments · Fixed by #1005

Comments

@bjunc
Copy link

bjunc commented May 26, 2020

Environment

  • Elixir version (elixir -v): Elixir 1.9.1
  • Absinthe version (mix deps | grep absinthe): 1.5.1
  • Client Framework and version (Relay, Apollo, etc): n/a

Expected behavior

Previously (eg. 1.4.16), I could pass a function to a field's description. We do this since many fields share the same description, and it keeps things dry (eg. objects and input objects often have the same description). It also cuts down on the schema noise.

Actual behavior

I'm not a macro wizard, but it seems 1.5 changed the way the macro works for descriptions (and maybe others?). The String trim logic is now attempting to trim a quoted function; which throws an error.

Maybe related to #928 ?

Relevant Schema/Middleware Code

n/a

@binaryseed
Copy link
Contributor

binaryseed commented Nov 27, 2020

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 description:

{:description, dynamic_description()}

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

@dylan-chong
Copy link
Contributor

Duplicate of #946

@bjunc
Copy link
Author

bjunc commented Dec 2, 2020

That works, thanks.

Still experimenting with the "best" way to do this, but I'm currently doing something like this (for someone who stumbles onto this):

# AppWeb.Schema

def hydrate(
    %Absinthe.Blueprint.Schema.FieldDefinition{identifier: field_name},
    [%Absinthe.Blueprint.Schema.ObjectTypeDefinition{identifier: parent_object} | _]
  ),
    do:
      {:description,
        __MODULE__.Descriptions.description("field:#{parent_object}.#{field_name}")}

def hydrate(%Absinthe.Blueprint.Schema.ObjectTypeDefinition{identifier: object}, _),
  do: {:description, __MODULE__.Descriptions.description("type:#{object}")}

def hydrate(_node, _ancestors), do: []
# AppWeb.Schema.Descriptions

def description(identifier) when is_binary(identifier) do
  case identifier do
    identifier
      when identifier in [
        "field:foo.bar",
        "field:create_foo_input.bar",
        "field:update_foo_input.bar"
      ] ->
        "The quick brown fox"

    _unsupported_description ->
      nil
  end
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants