feat: find-references and rename for GraphQL fragments#409
Open
JoviDeCroock wants to merge 1 commit into
Open
Conversation
Proxy findReferences/getReferencesAtPosition and getRenameInfo/findRenameLocations so that a cursor on a fragment name - at its definition or at any spread - lists and renames the definition plus every spread across the project's source files. Files are cheaply prefiltered with a plain-text scan before their GraphQL documents are parsed, and gql.tada multi-schema setups only match documents belonging to the same schema. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
🦋 Changeset detectedLatest commit: c69ceed The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
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 |
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.
What/Why
GraphQL fragments are the unit of composition (gql.tada pushes fragment co-location), but until now you couldn't see where a fragment is spread, nor rename it. This PR adds:
fragment PokemonFields on Pokemon) or at a spread (...PokemonFields) — the editor lists the definition and every spread of that fragment, across all of the project's source files....prefix), including fragments defined in anothergraphql()call in the same file and fragments imported from other files.Implementation notes
packages/graphqlsp/src/references.tsresolves the cursor to a fragment name token the same waydefinition.tsdoes (findNode → bubble up to thegraphql()/gql()call or tagged template → parse the document → hit-testFragmentDefinition/FragmentSpreadname locations), then scans the program's source files for matching definitions/spreads.node_modules, and prefilters with a plain-textsource.getText().includes(fragmentName)scan before any GraphQL parsing, so the cost is bounded on explicit user actions only.documentNode.getStart() + 1 + loc.start, using the raw (unescaped) literal text so offsets map 1:1 (same approach asresolveTemplate).findReferences(used by the tsserverreferencescommand),getReferencesAtPosition,getRenameInfoandfindRenameLocationsare proxied inindex.tswith the existingguard()log-and-fall-back pattern; when the cursor is not on a fragment name, the original language service result is returned unchanged.Limitations
gql\...`) only; templates with${...}interpolations are skipped, since interpolation shifts GraphQL offsets away from TS file offsets (the same restrictiondefinition.ts` has).Test plan
test/e2e/references.test.tsagainstfixture-project-tadawith two new fixture files:references-fragment.ts(fragment definition + a spread in a secondgraphql()call in the same file) andreferences-usage.ts(spread of the imported fragment in another file). It forks a real tsserver and asserts, via thereferencesandrenameprotocol commands:isDefinitionset on the definition entry;canRename, the rightdisplayName/triggerSpan, and rename locations covering every occurrence (definition + both spreads, cross-file);pnpm --filter @0no-co/graphqlsp run buildpasses.pnpm run test:e2epasses: 12 test files, 57 tests, 0 failures.🤖 Generated with Claude Code