Skip to content

Fix monaco-graphql hover crashing on the first line#4225

Merged
trevor-scheer merged 4 commits into
graphql:mainfrom
trevor-scheer:trevor/fix-monaco-hover-line-zero
May 6, 2026
Merged

Fix monaco-graphql hover crashing on the first line#4225
trevor-scheer merged 4 commits into
graphql:mainfrom
trevor-scheer:trevor/fix-monaco-hover-line-zero

Conversation

@trevor-scheer

Copy link
Copy Markdown
Contributor

Hover is broken in monaco-graphql. On the first line it throws and renders nothing; on later lines it doesn't crash but highlights the wrong token.

GraphQLWorker.doHover passes a 0-indexed position to getRange, which actually wants a 1-indexed SourceLocation (per the GraphQL spec, and what its other callers pass via error.locations[0]). On line 0 the loop never runs and Expected Parser stream to be available fires. On later lines getRange parses to end-of-line and ignores column, so the range comes back pointing at the previous line's last token.

getRange was the wrong tool here anyway. It was built for diagnostics, not for finding the token under a cursor. getTokenAtPosition is what we want, and it's what the rest of the language service already uses.

Fixes #4157
Fixes #3168

Supersedes #4162, which tried to special-case line === 0 inside getRange itself. That left getRange returning a Position with line: -1 (which becomes 0 after toMonacoRange's +1, invalid in Monaco's 1-indexed API), and didn't address the bigger problem that getRange doesn't compute a token range to begin with.

Validation

yarn install
yarn build
yarn workspace example-monaco-graphql-react-vite dev

Open the URL Vite prints, with browser devtools open.

  1. Hover the word query on the first line. Before: console logs Error: Expected Parser stream to be available and no popup appears. After: popup renders and query is highlighted.
  2. Press Enter to push the query down a line, then hover a field name. Before: popup appears but the highlight is on the previous line's last token. After: the field under the cursor is highlighted.

`GraphQLWorker.doHover` was passing 0-indexed positions to `getRange`,
which wants a 1-indexed `SourceLocation` per the GraphQL spec. On line
0 this hit the invariant; on other lines it returned the wrong range
(`getRange` parses to end-of-line and ignores `column`, so you get the
previous line's last token). Use `getTokenAtPosition` instead.
@changeset-bot

changeset-bot Bot commented May 6, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: f8cfc88

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
monaco-graphql Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@trevor-scheer trevor-scheer requested a review from dimaMachina May 6, 2026 20:19
@trevor-scheer trevor-scheer marked this pull request as ready for review May 6, 2026 20:19
Stand up a minimal `IWorkerContext` so the worker can be constructed
without a Monaco runtime, then exercise `doHover` at a position on
line 0. Asserts that the result is non-null and that the range covers
the token under the cursor.

@dimaMachina dimaMachina left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Nice, did you create failling test first?

@trevor-scheer

Copy link
Copy Markdown
Contributor Author

@dimaMachina test was after but I did test it against main for failure!

@trevor-scheer trevor-scheer enabled auto-merge (squash) May 6, 2026 20:37
@trevor-scheer trevor-scheer merged commit 4bb7909 into graphql:main May 6, 2026
13 checks passed
This was referenced May 6, 2026
@acao acao mentioned this pull request May 8, 2026
This was referenced May 9, 2026
benjie pushed a commit that referenced this pull request May 11, 2026
This PR was opened by the [Changesets
release](https://github.com/changesets/action) GitHub action. When
you're ready to do a release, you can merge this and the packages will
be published to npm automatically. If you're not ready to do a release
yet, that's fine, whenever you add more changesets to main, this PR will
be updated.


# Releases
## @graphiql/toolkit@0.12.0

### Minor Changes

- [#4199](#4199)
[`463df14`](463df14)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Send
spec-compliant `Accept` header (`application/graphql-response+json`) in
`createSimpleFetcher`

## codemirror-graphql@2.2.5

### Patch Changes

- Updated dependencies
\[[`914a547`](914a547),
[`10f66d5`](10f66d5)]:
    -   graphql-language-service@5.5.1

## graphiql@5.2.3

### Patch Changes

- [#4181](#4181)
[`f1a210e`](f1a210e)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Fix schema
prop to skip introspection when IntrospectionQuery data is provided

Previously, passing an `IntrospectionQuery` result as the `schema` prop
would still trigger a network introspection request. The
`shouldIntrospect` check only recognized `GraphQLSchema` instances (via
`isSchema`), not raw introspection data. Now, when an
`IntrospectionQuery` is passed, a schema is built from it directly using
`buildClientSchema` and introspection is skipped.

- [#4211](#4211)
[`e7b30c1`](e7b30c1)
Thanks [@davidjb](https://github.com/davidjb)! - Add \*.css to
sideEffects to allow import of CSS in Webpack Javascript

- Updated dependencies
\[[`f1a210e`](f1a210e),
[`6f5d5d2`](6f5d5d2),
[`40359eb`](40359eb),
[`e7b30c1`](e7b30c1)]:
    -   @graphiql/react@0.37.4
    -   @graphiql/plugin-doc-explorer@0.4.2
    -   @graphiql/plugin-history@0.4.2

## @graphiql/plugin-code-exporter@5.1.2

### Patch Changes

- [#4211](#4211)
[`e7b30c1`](e7b30c1)
Thanks [@davidjb](https://github.com/davidjb)! - Add \*.css to
sideEffects to allow import of CSS in Webpack Javascript

## @graphiql/plugin-doc-explorer@0.4.2

### Patch Changes

- [#4231](#4231)
[`6f5d5d2`](6f5d5d2)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Fix
degraded type declarations in published packages

Both packages import from `@graphiql/react` at build time but only
declared it as a peer dependency. Yarn workspaces topologically orders
builds via `dependencies`/`devDependencies`, not `peerDependencies`, so
on a clean checkout these plugins built before `@graphiql/react` had
emitted its `dist/*.d.ts`. `vite-plugin-dts` then ran `tsc` against
unresolved `@graphiql/react` imports, fell back to `any` for any return
type that flowed through `useGraphiQL`, and published `.d.ts` artifacts
where hooks like `useDocExplorer` and `useDocExplorerActions` resolved
to `() => any` instead of their real shapes.

Adding `@graphiql/react` as a `devDependency` matches the pattern
already in `@graphiql/plugin-explorer` and
`@graphiql/plugin-code-exporter` and lets the build run in topological
order.

- [#4140](#4140)
[`40359eb`](40359eb)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Remove
`react-compiler-runtime` peer dependency

- [#4211](#4211)
[`e7b30c1`](e7b30c1)
Thanks [@davidjb](https://github.com/davidjb)! - Add \*.css to
sideEffects to allow import of CSS in Webpack Javascript

## @graphiql/plugin-explorer@5.1.2

### Patch Changes

- [#4211](#4211)
[`e7b30c1`](e7b30c1)
Thanks [@davidjb](https://github.com/davidjb)! - Add \*.css to
sideEffects to allow import of CSS in Webpack Javascript

## @graphiql/plugin-history@0.4.2

### Patch Changes

- [#4231](#4231)
[`6f5d5d2`](6f5d5d2)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Fix
degraded type declarations in published packages

Both packages import from `@graphiql/react` at build time but only
declared it as a peer dependency. Yarn workspaces topologically orders
builds via `dependencies`/`devDependencies`, not `peerDependencies`, so
on a clean checkout these plugins built before `@graphiql/react` had
emitted its `dist/*.d.ts`. `vite-plugin-dts` then ran `tsc` against
unresolved `@graphiql/react` imports, fell back to `any` for any return
type that flowed through `useGraphiQL`, and published `.d.ts` artifacts
where hooks like `useDocExplorer` and `useDocExplorerActions` resolved
to `() => any` instead of their real shapes.

Adding `@graphiql/react` as a `devDependency` matches the pattern
already in `@graphiql/plugin-explorer` and
`@graphiql/plugin-code-exporter` and lets the build run in topological
order.

- [#4140](#4140)
[`40359eb`](40359eb)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Remove
`react-compiler-runtime` peer dependency

- [#4211](#4211)
[`e7b30c1`](e7b30c1)
Thanks [@davidjb](https://github.com/davidjb)! - Add \*.css to
sideEffects to allow import of CSS in Webpack Javascript

- Updated dependencies
\[[`463df14`](463df14)]:
    -   @graphiql/toolkit@0.12.0

## @graphiql/react@0.37.4

### Patch Changes

- [#4181](#4181)
[`f1a210e`](f1a210e)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Fix schema
prop to skip introspection when IntrospectionQuery data is provided

Previously, passing an `IntrospectionQuery` result as the `schema` prop
would still trigger a network introspection request. The
`shouldIntrospect` check only recognized `GraphQLSchema` instances (via
`isSchema`), not raw introspection data. Now, when an
`IntrospectionQuery` is passed, a schema is built from it directly using
`buildClientSchema` and introspection is skipped.

- [#4140](#4140)
[`40359eb`](40359eb)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Remove
`react-compiler-runtime` peer dependency

- [#4211](#4211)
[`e7b30c1`](e7b30c1)
Thanks [@davidjb](https://github.com/davidjb)! - Add \*.css to
sideEffects to allow import of CSS in Webpack Javascript

- Updated dependencies
\[[`914a547`](914a547),
[`463df14`](463df14),
[`4bb7909`](4bb7909),
[`10f66d5`](10f66d5)]:
    -   graphql-language-service@5.5.1
    -   @graphiql/toolkit@0.12.0
    -   monaco-graphql@1.7.4

## graphql-language-service@5.5.1

### Patch Changes

- [#3882](#3882)
[`914a547`](914a547)
Thanks [@bensengupta](https://github.com/bensengupta)! - Fix off-by-one
when hovering over token

- [#4222](#4222)
[`10f66d5`](10f66d5)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Unpin and
update graphql-config dependency

## graphql-language-service-server@2.14.9

### Patch Changes

- [#4187](#4187)
[`ca83879`](ca83879)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Bump
required TypeScript runtime dependency from `^5.3.3` to `^5.8.0`. This
is preparatory work for adopting the TypeScript Native Preview (tsgo)
compiler in a follow-up change, which tracks TypeScript 5.8 semantics.
In practice `^5.3.3` already resolved to TS 5.8+ for most consumers; the
new floor only affects consumers who pin TypeScript to 5.3–5.7 via
resolutions or overrides.

- [#4222](#4222)
[`10f66d5`](10f66d5)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Unpin and
update graphql-config dependency

- Updated dependencies
\[[`914a547`](914a547),
[`10f66d5`](10f66d5)]:
    -   graphql-language-service@5.5.1

## monaco-graphql@1.7.4

### Patch Changes

- [#4225](#4225)
[`4bb7909`](4bb7909)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Fix hover
crashing on the first line of a query

`GraphQLWorker.doHover` was passing 0-indexed positions to `getRange`,
which expects a 1-indexed `SourceLocation` (per the GraphQL spec). On
the first line this caused `Expected Parser stream to be available` to
be logged and hover to return `null`. On other lines it returned the
range of the previous line's last token rather than the token under the
cursor. Use `getTokenAtPosition` to compute the actual token range
instead.

- Updated dependencies
\[[`914a547`](914a547),
[`10f66d5`](10f66d5)]:
    -   graphql-language-service@5.5.1

## vscode-graphql@0.13.3

### Patch Changes

- [#4183](#4183)
[`2ef9389`](2ef9389)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Fix VS Code
extension publishing scripts

- Updated dependencies
\[[`ca83879`](ca83879),
[`10f66d5`](10f66d5)]:
    -   graphql-language-service-server@2.14.9

## vscode-graphql-execution@0.3.3

### Patch Changes

- [#4183](#4183)
[`2ef9389`](2ef9389)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Fix VS Code
extension publishing scripts

- [#4222](#4222)
[`10f66d5`](10f66d5)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Unpin and
update graphql-config dependency

## vscode-graphql-syntax@1.3.9

### Patch Changes

- [#4183](#4183)
[`2ef9389`](2ef9389)
Thanks [@trevor-scheer](https://github.com/trevor-scheer)! - Fix VS Code
extension publishing scripts

- [#4143](#4143)
[`7979bf5`](7979bf5)
Thanks [@Netail](https://github.com/Netail)! - Add syntax highlighting
support for subscription operations.

- [#4144](#4144)
[`f7e2a56`](f7e2a56)
Thanks [@jsmnbom](https://github.com/jsmnbom)! - Add `text.html.vue` as
inline injection target.

[This PR](vuejs/language-tools#5856) broke
tooling by changing the vue grammar scope from `source.vue` to
`text.html.vue`. This adds `text.html.vue` as an additional injection
target for GraphQL syntax highlighting so that it works in both cases.

Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
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.

[graphql-language-service] hover broken [language-service] getRange does not work for line 0

2 participants