Skip to content

fix: #2199 GraphQL 500 when fragment definitions precede the operation - #2203

Merged
lbroudoux merged 1 commit into
microcks:masterfrom
DKey96:fix/graphql-fragment-first-operation
Jul 2, 2026
Merged

lbroudoux merged 1 commit into
microcks:masterfrom
DKey96:fix/graphql-fragment-first-operation

Conversation

@DKey96

@DKey96 DKey96 commented Jul 1, 2026

Copy link
Copy Markdown
Contributor

Fixes #2199

Problem

GraphQL clients (Apollo, urql, Relay) emit fragment definitions before the operation in the query document. GraphQLController assumed the operation is the first definition and blindly cast getDefinitions().get(0) to OperationDefinition, so any real client query using named fragment spreads throws:

java.lang.ClassCastException: class graphql.language.FragmentDefinition
cannot be cast to class graphql.language.OperationDefinition
    at io.github.microcks.web.GraphQLController.execute(...)

This is a different code path from the one fixed in #2201 (ab184ed) — that change hardened field retention in filterFieldSelection, but execute() throws before ever reaching it, so the 500 remains for real-world queries.

Reproduction

Same query, only the definition order differs:

Variant Result
Named fragment spreads, fragments defined first (Apollo's native output) HTTP 500
Same query, operation moved before the fragments HTTP 200
Same query, fragments inlined (... on Type {}) HTTP 200

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

  • Select the operation via getDefinitionsOfType(OperationDefinition.class), honoring the request operationName when present (mirrors the existing getDefinitionsOfType(FragmentDefinition.class) call just below).
  • Harden two sibling casts that assumed a top-level selection is always a Field: guard the introspection check with instanceof, and flatten top-level fragment spreads via collectFields() before processing each queried field.

Test

Adds a GraphQLControllerIT case asserting a query whose fragment definition precedes the operation returns 200 (verified failing before the change, passing after).

…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]>
@github-actions

github-actions Bot commented Jul 1, 2026

Copy link
Copy Markdown

👋 @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 lbroudoux left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome! This looks great. Thank you.

@lbroudoux lbroudoux added this to the 1.15.0 milestone Jul 2, 2026
@lbroudoux
lbroudoux merged commit 8091b13 into microcks:master Jul 2, 2026
15 checks passed
@github-actions

github-actions Bot commented Jul 2, 2026

Copy link
Copy Markdown

🎉 @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 🙌

lbroudoux pushed a commit that referenced this pull request Aug 5, 2026
#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

GraphQL: named fragment spreads cause HTTP 500; inlining them exposes a second bug where Microcks synthesises null for unrecorded fields

2 participants