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

Allow accessing enum value names via methods #5206

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

DmitryTsepelev
Copy link
Contributor

This is a pretty minor change, but might be helpful: now you can grab GraphQL name of the enum value using the method instead of a complex spell.

Before: Jazz::Family.values["STRING"].graphql_name
Now: Jazz::Family.string

@DmitryTsepelev DmitryTsepelev marked this pull request as ready for review January 12, 2025 17:24
Copy link
Owner

@rmosolgo rmosolgo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey, thanks so much for this contribution and sorry I didn't get back to you til now. I'm definitely open to this change. I think the next few things to work out are:

  • What if the value's graphql_name.downcase conflicts with an existing method on Schema::Enum, like value, kind, or inherited? I think we should emit some kind of warning that the method generation was skipped and provide an override option like value_method:, whose value will be used instead of graphql_name.downcase.

  • In case this causes some problems for people who are already using the gem, I'd like to have some way for them to turn it off. For example, if a value_method: option was added, value_method: false could disable creating these methods, and they could set that option by default in their base enum value classes.

Thanks, and let me know what you think!

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

```ruby
Types::MediaCategory.audio # => "Audio"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would this be "AUDIO"?

@@ -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 }
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Implementation-wise, calling define_method with a block will result in the binding of this method staying in memory for the lifetime of the application. I'd like to avoid that memory "bloat", and we could probably do it with something like

instance_eval("def #{value.graphql_name.downcase}; #{value.graphql_name.inspect}; end;")

It would create the method that returns the string, but without retaining a reference of the binding.

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 this pull request may close these issues.

2 participants