fix: anchor inline fragment-definition detection in Client#parse#82
Open
rellampec wants to merge 2 commits into
Open
fix: anchor inline fragment-definition detection in Client#parse#82rellampec wants to merge 2 commits into
rellampec wants to merge 2 commits into
Conversation
Pin current behavior of locally-defined named fragments (a 'fragment Name on Type' spread via '...Name' within the same document), transitive local fragments, and the source_document vs sliced document distinction. These were only exercised incidentally alongside constant-path spreads; isolating them guards a future refactor of the fragment-resolution step in parse.
The spread/definition disambiguation in #parse used an unanchored match,
str.match(/fragment\s*#{const_name}/), to decide whether a ...Name spread
referred to a named fragment defined in the same document. It matched too
eagerly: \s* allowed zero whitespace, and there was no boundary after the
name, so a ...UserFields spread was mistaken for a fragment UserFieldsExtended
on User definition and left unresolved (later crashing in sliced_definitions
with a TypeError, instead of resolving as a constant spread).
Anchor the match to /fragment\s+<Name>\s+on\b/ (mandatory surrounding
whitespace, escaped name, 'on' word boundary) so only a real
'fragment <Name> on Type' definition is treated as local; prefix-sharing
names no longer collide.
Adds test/test_parse_fragment_anchoring.rb (3 tests):
- a prefix spread is not swallowed by a longer fragment definition;
- prefix-sharing *inline* fragments both still resolve;
- a *constant* fragment spread coexists with a prefix-sharing inline
fragment (the realistic migration/interop case).
Behaviour pinned by test_parse_fragment_characterization is unchanged.
c43de27 to
19b8c22
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The bug
When
Client#parsewalks...Namespreads, it decides for each whetherNameis a named fragment defined in the same document (leave the spread alone) or a Ruby constant to resolve. That decision used an unanchored match:which matches too eagerly:
\s*allows zero whitespace (fragmentNamematches), and...UserFieldsspread matches afragment UserFieldsExtended on Userdefinition.When that false positive happens, the spread is left unresolved and is never registered as a constant dependency.
Downstream this doesn't just produce a wrong query — it crashes in
sliced_definitionswithTypeError: no implicit conversion of nil into Array. An earlier attempt to tighten this matching was reverted long ago (b5a4612), so the eager form has been in place since.The fix
Anchor the match to a real fragment definition:
Tests
Adds
test/test_parse_fragment_anchoring.rb:...UserFieldsalongsidefragment UserFieldsExtended on Usernow surfaces a cleanuninitialized constant UserFieldsValidationError(previously: theTypeErrorcrash above).UserFields/UserFieldsExtended), both spread, still resolve locally —anchoring isn't over-strict.
No behaviour change to anything pinned by the characterization tests (#81) — they remain green.
Relationship to other PRs
lib/graphql/client.rb+test/test_parse_fragment_anchoring.rbonce test: characterize Client#parse handling of inline named fragments #81 lands.