Skip to content

Polymorphic associations and GraphQL UnionType in query #8

Open
@geneeblack

Description

@geneeblack

First off, great job. I struggled in my own code traversing the ast_node to determine the include/preload models to use in the resultant query. What you've done has made my life much easier.

I ran into an issue when using a UnionType to express a polymorphic association and then attempting to traverse the ast_node to build the references. The issue is that the UnionType is expressing a polymorphic association. The polymorphic association does not have a direct class reference. So in the following line an empty result is returned and the code errs on the next line.

https://github.com/nettofarah/graphql-query-resolver/blob/master/lib/graphql/query_resolver.rb#L66

I don't think there is a way to 'fix' this since the only way that I know of to determine the models that a polymorphic association references is to do a table scan. I am simply punting at this point by checking if !selection_name.nil? and hitting next if it is. Maybe you can solve this in another way. It's not a high priority for me at this point since we only use polymorphic associations in two cases in our app.

selection_name = class_name.reflections.with_indifferent_access[selection.name].options[:class_name]
begin   
    current_class_name = selection_name.singularize.classify.constantize
    dependencies[selection.name.to_sym] = get_implied_includes(current_class_name, selection)
rescue
    Rails.logger "polymorphic associations do not have a resolvable class_name"
end

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions