When Schema with max_complexity set, multiplex queries with an abstract type get `NoMethodError` using AST analyzers
Background
When a schema has max_complexity set and to a multiplex query is called, MaxQueryComplexity analyzers is added to the multiplex_analyzers for a given execution. When the analyzing phase happens in QueryComplexity#on_leave_field checks to see if the parent node is an abstract type. When it is an abstract it calls possible_types on the query that was passed to the analyzer.
The problem is multiplex_analyzers are initialized with GraphQL::Execution::Multiplex, which doesn't have possible_types defined.
Code Example
backtrace and code example are in this gist
Purposed Solutions
It would be nice if there some way to check wether or not your analyzer is being used at a multiplex analyzer or just being used as a plan query analyzer. Would it make sense to define multiplex? method on GraphQL::Analysis::AST::Analyzer that checks the class of the query variable?
If GraphQL::Analysis::AST::Analyzer is not the right place for this fix, we could just add something to QueryComplexity that would run a different code path when working with GraphQL::Execution::Multiplex. Or GraphQL::Execution::Multiplex could implement possible_types.
Let me know what you think the best approach is, I would be happy to put up a patch.
Thanks for such a detailed bug report! That's really helpful. What a tricky issue. Here are some thoughts:
- I like having a single Analyzer class that works on either a multiplex or a query. (That is, it runs on multiplexes of size
1or greater.) - But, we have to keep the lines clear about which objects go where -- for example, clear lifecycle of initialize, visit each query, check result.
I'm not exactly sure the best solution, but I want to prevent confusion over whether query is a Query or a Multiplex -- that could come back to bite us!
Thanks for getting back so fast!
I also like that analyzer can be used in both normal Queries and Multiplex queries > 1. Based on the comments in the class around multiplex analyzers maintaining state across all queries visited, I think keeping the single query object makes the sense but the query instance variable in the class should represent current query that is being visited. We can change the query variable to the new currently being visited query per iteration in the multiplex analyzers.
This should have little impact on current analyzer and allow us to iterate over queries as they are being visited in the multiplex analyzer. The multiplex analyzer can still maintain state across all the queries in the multiplex, and the individual query analyzer can still operate on one query. Also is seems that GraphQL::Execute::Multiplex has configuration, like max_complexity that should be accessible to the analyzer, having access to that information should still be possible for analyzers like GraphQL::Analysis::AST::QueryComplexity. Do you think this solution would do the trick?
I have also been read the GraphQL::Analyzer::AST implementation and it also seems like multiplex analyzers do not check the analyze? method. I think this is a potential problem, specially if we want analyzers classes to be interoperable between query analyzers and multiplex analyzers.
What do you think about calling the analyze? method after initializing the object, and not running the analyzers when it returns false, the same way the query analyzer functions?