-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Namespaced model support #176
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
Conversation
…ted ReduceResolvedClassNames
This reverts commit 5dbc884.
PS - Sorry for the spacing change, you might add a |
@rmosolgo - regarding the rbx failure, I have no idea why it isn't loading the test model |
I really like this idea -- matching GraphQL object types to "underlying" Ruby classes -- I just need to get a chance to think it through! |
# Conflicts: # lib/graphql/object_type.rb # lib/graphql/schema.rb
@rmosolgo - I just re-merged this to resolve conflicts with updated Local tests succeed, travis seems hung up at this time (builds aren't starting). Absent other ideas, I'd like to see this get merged before it falls behind again. |
Thanks! Sorry I haven't looked at it yet. I want to keep an eye out for extensibility for this mechanism, for example, what if a type can be matched to an object by a hash key? or what if it exposes several classes? I haven't thought through these questions yet but I'd like to get them all at once, if possible! |
All in all, I like the idea of adding some awareness of "is this an object that belongs to this type?" There are some dubious "defaults" in graphql-ruby like Given my own experience and things I've heard from maintaining this gem, here are the questions we'll have to answer next:
Can this approach be expanded to handle these different cases? I think this can be accomplished without library changes, too. Instead of storing the map of class names to types on # Store a map of application object class names => graphql types
CLASS_NAME_TO_GRAPHQL_TYPE = {}
module AssignResolvedClassName
# Add an entry to the global map of classname => type
def self.call(type, resolved_class_name)
CLASS_NAME_TO_GRAPHQL_TYPE[resolved_class_name] = type
end
end
GraphQL::BaseType.accepts_definitions(resolved_class_name: AssignResolvedClassName)
PostType = GraphQL::ObjectType.define do
# ...
resolved_class_name "Acme::Post"
end
NodeIdentification = GraphQL::Relay::GlobalNodeIdentification.define do
object_from_id -> (id, ctx) do
type_name, id = NodeIdentification.from_global_id(id)
# Get the resolved_class_name for the given type_name
class_name = CLASS_NAME_TO_GRAPHQL_TYPE[type_name]
# "Post" -> Post.find(id)
class_name.constantize.find(id)
end
end Would that work the same as this library change? |
I think this will work for our case. I don't like the idea of global variables in general; it would be nice if weren't adding to the global namespace, but that's not a show-stopper. While the case for us seems clear now, I don't quite see how you implement different resolvers for the other cases you mention, but again I don't yet have those same issues. |
Thanks for working through this with me! I pushed a new feature in 0.18.1 that allows types to store arbitrary metadata: https://github.com/rmosolgo/graphql-ruby/blob/master/guides/defining_your_schema.md#extending-type-and-field-definitions Do you think that would be a good fit for this use case? |
hope that works, let me know if it doesn't! |
Thanks @rmosolgo, this pattern works great and has good use for any specific ruby fields you might need. I've tested the change with our code and it was successful. |
🎉 glad to hear it, and thanks for following up!! |
Solves #175. Provides support through
resolved_class_name
.type_name => class_name
Schema.resolved_class_names
An accompanying PR from graphql-relay-ruby will be forthcoming that uses
Schema.resolved_class_names