Repository navigation
fix: #2199 GraphQL 500 when fragment definitions precede the operation - #2203
Conversation
…operation GraphQL clients (Apollo, urql, Relay) emit fragment definitions before the operation in the query document. GraphQLController assumed the operation was the first definition and blindly cast getDefinitions().get(0) to OperationDefinition, throwing a ClassCastException (HTTP 500) for any real client query using named fragment spreads. Select the operation via getDefinitionsOfType(OperationDefinition.class), honoring the request operationName when present. Also harden two sibling casts that assumed a top-level selection is always a Field: guard the introspection check with an instanceof, and flatten top-level fragment spreads via collectFields() before processing each queried field. Add a GraphQLControllerIT case asserting a query whose fragment definition precedes the operation returns 200. Signed-off-by: Daniel Klíč <[email protected]>
|
👋 @DKey96 Welcome to the Microcks community! 💖 Thanks and congrats 🎉 for opening your first pull request here! Be sure to follow the pull request template or please update it accordingly. Hope you have a great time there! |
lbroudoux
left a comment
There was a problem hiding this comment.
Awesome! This looks great. Thank you.
|
🎉 @DKey96 You are now a Microcks community contributor! 💖 Thanks and congrats 🚀 on merging your first pull request! We are delighted and very proud of you! 👏 📢 If you're using Microcks in your organization, please add your company name to this list. 🙏 It really helps the project to gain momentum and credibility. It's a small contribution back to the project with a big impact. If you need to know why and how to add yourself to the list, please read the blog post "Join the Microcks Adopters list and Empower the vibrant open source Community 🙌" Kudos and please keep going, we need you 🙌 |
#2203) GraphQL clients (Apollo, urql, Relay) emit fragment definitions before the operation in the query document. GraphQLController assumed the operation was the first definition and blindly cast getDefinitions().get(0) to OperationDefinition, throwing a ClassCastException (HTTP 500) for any real client query using named fragment spreads. Select the operation via getDefinitionsOfType(OperationDefinition.class), honoring the request operationName when present. Also harden two sibling casts that assumed a top-level selection is always a Field: guard the introspection check with an instanceof, and flatten top-level fragment spreads via collectFields() before processing each queried field. Add a GraphQLControllerIT case asserting a query whose fragment definition precedes the operation returns 200. Signed-off-by: Daniel Klíč <[email protected]> (cherry picked from commit 8091b13)
Fixes #2199
Problem
GraphQL clients (Apollo, urql, Relay) emit fragment definitions before the operation in the query document.
GraphQLControllerassumed the operation is the first definition and blindly castgetDefinitions().get(0)toOperationDefinition, so any real client query using named fragment spreads throws:This is a different code path from the one fixed in #2201 (ab184ed) — that change hardened field retention in
filterFieldSelection, butexecute()throws before ever reaching it, so the 500 remains for real-world queries.Reproduction
Same query, only the definition order differs:
... on Type {})Reordering the operation to the front makes the identical named-fragment query succeed, confirming the failure is the
get(0)cast rather than the spreads themselves.Fix
getDefinitionsOfType(OperationDefinition.class), honoring the requestoperationNamewhen present (mirrors the existinggetDefinitionsOfType(FragmentDefinition.class)call just below).Field: guard the introspection check withinstanceof, and flatten top-level fragment spreads viacollectFields()before processing each queried field.Test
Adds a
GraphQLControllerITcase asserting a query whosefragmentdefinition precedes the operation returns 200 (verified failing before the change, passing after).