Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions guides/type_definitions/enums.md
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,9 @@ value "AUDIO", value: :audio
Then, GraphQL inputs of `AUDIO` will be converted to `:audio` and Ruby values of `:audio` will be converted to `"AUDIO"` in GraphQL responses.

Enum classes are never instantiated and their methods are never called.

You can get the GraphQL name of the enum value using the method matching its downcased name:

```ruby
Types::MediaCategory.audio # => "Audio"
```
1 change: 1 addition & 0 deletions lib/graphql/schema/enum.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ class << self
def value(*args, **kwargs, &block)
kwargs[:owner] = self
value = enum_value_class.new(*args, **kwargs, &block)
singleton_class.define_method(value.graphql_name.downcase) { value.graphql_name }
key = value.graphql_name
prev_value = own_values[key]
case prev_value
Expand Down
12 changes: 12 additions & 0 deletions spec/graphql/schema/enum_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,18 @@
end
end

describe "value methods" do
it "defines methods to fetch graphql names" do
assert_equal enum.string, "STRING"
assert_equal enum.woodwind, "WOODWIND"
assert_equal enum.brass, "BRASS"
assert_equal enum.percussion, "PERCUSSION"
assert_equal enum.didgeridoo, "DIDGERIDOO"
assert_equal enum.keys, "KEYS"
assert_equal enum.silence, "SILENCE"
end
end

describe "type info" do
it "tells about the definition" do
assert_equal "Family", enum.graphql_name
Expand Down
Loading