absinthe-graphql / absinthe

The GraphQL toolkit for Elixir
http://absinthe-graphql.org
Other
4.28k stars 525 forks source link

Unable to conditionally define an enum value #1100

Closed Awlexus closed 3 years ago

Awlexus commented 3 years ago

Environment

Elixir 1.12.2 (compiled with Erlang/OTP 22)

* Absinthe version: 1.6.4

### Expected behavior

I tried to define an enum value, that is only available during testing, but I was also able to use it during development.

### Actual behavior

The enum value was available during development

### Relevant Schema/Middleware Code

```elixir
enum :social_media do
  value :google
  value :facebook
  value :twitter
  value :linkedin

  if Mix.env() == :test do
    value :mock
  end
end
benwilson512 commented 3 years ago

This isn't really possible for us to support as written. Your best bet is to do:

if Mix.env() == :test do
  enum :social_media do
    value :google
    value :facebook
    value :twitter
    value :linkedin
    value :mock
  end
else
  enum :social_media do
    value :google
    value :facebook
    value :twitter
    value :linkedin
  end
end

Or you can use a pipeline modifier: https://hexdocs.pm/absinthe/Absinthe.Schema.html#module-custom-schema-manipulation-in-progress

Awlexus commented 3 years ago

Interestingly, I tried wrapping the whole definition as well and got an error because the enum was defined multiple times 😅

But understandable, I will look into pipelines, thanks for the suggestion