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

Making field property accessible after definition #87

Closed
kliuless opened this issue Jan 19, 2016 · 3 comments
Closed

Making field property accessible after definition #87

kliuless opened this issue Jan 19, 2016 · 3 comments

Comments

@kliuless
Copy link
Contributor

When defining a field, you can specify a property name, which gets called on the backing object during resolve:

field :createdBy, types.String, property: :created_by

However, it seems like the property isn't accessible on the GraphQL::Field object; it's wrapped inside the resolver lambda in GraphQL::DefinitionHelpers::DefinedByConfig::DefinitionConfig:

property && field.resolve = -> (t,a,c) { t.public_send(property)}

Is there interest in storing this to an instance variable and exposing a getter?

My use case is to access the mappings in a mutation which needs to do a reverse mapping, to set attributes on an ActiveRecord model. E.g. the mutation receives a createdBy argument and has to call record.created_by = input_value. If there's a better way to do this, let me know.

@rmosolgo
Copy link
Owner

Is there interest in storing this to an instance variable and exposing a getter?

👍 that'd be great! Maybe you could update the default resolve proc to check for a property? (or maybe that's a waste of time at runtime?)

@kliuless
Copy link
Contributor Author

At first I didn't want to have the proc do the check, but then property would make more sense as a attr_reader only (since the original property would be hardwired to the proc). But that complicates other things, like when DefinitionConfig#to_instance using public_send to set the attributes on the Field.

So I'm going to have the proc do the check. I don't think performance would be affected much, especially on ruby 2.x.

@kliuless
Copy link
Contributor Author

Hmm, after making the changes, I realized I could've re-created the resolver proc when property is reassigned, instead of checking inside the proc. Not sure it's worth the effort to change it though.

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

No branches or pull requests

2 participants