Skip to content

Fix inline fragments without a space before "on"#84

Open
dpep wants to merge 1 commit into
github-community-projects:masterfrom
dpep:fix-inline-fragment-on-keyword
Open

Fix inline fragments without a space before "on"#84
dpep wants to merge 1 commit into
github-community-projects:masterfrom
dpep:fix-inline-fragment-on-keyword

Conversation

@dpep

@dpep dpep commented Jul 10, 2026

Copy link
Copy Markdown

What

Client#parse's fragment-spread regex (/\.\.\.([a-zA-Z0-9_]+(::[a-zA-Z0-9_]+)*)/) matches ...on Type (no space) as a spread referencing a Ruby constant named On, since it only requires the identifier to immediately follow the three dots — it doesn't distinguish that from an inline fragment written without a space before on.

Both ...on Type { ... } and ... on Type { ... } are valid, equivalent GraphQL — whitespace between ... and on is optional per the spec. But only the spaced form parses correctly today; the unspaced form raises:

GraphQL::Client::ValidationError: uninitialized constant on

Why this is safe to fix with a lookahead

The GraphQL spec explicitly forbids naming a fragment on:

FragmentName : Name but not on

So ...on immediately followed by a word boundary can never be a legitimate fragment-spread reference — it's unambiguously an inline fragment. This PR adds a negative lookahead ((?!on\b)) to the regex to skip exactly that case, leaving every other fragment name (including ones that start with on, like onFragment, or literally named On with a capital O) unaffected.

Testing

Added test_client_parse_inline_fragment_without_space_before_on, which fails against master with the ValidationError above and passes with this fix. Ran the full test/test_client.rb suite locally — 17 runs, 0 failures, 0 errors (3 pre-existing skips, unrelated).

`...on Type` (no space) is valid GraphQL for an inline fragment, but
Client#parse's fragment-spread regex matched it as a spread
referencing a Ruby constant named `On`, since it only excluded
whitespace between the dots and the name, not the specific "on"
keyword. Adds a negative lookahead: GraphQL forbids naming a fragment
"on" (FragmentName : Name but not "on"), so "...on" followed by a
word boundary is always an inline fragment, never ambiguous with a
constant reference.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant