[near-operation-file-preset] Fix missing fragment document imports for nested fragments (graphQLTag) - #1530
Conversation
🦋 Changeset detectedLatest commit: 152b967 The changes in this PR will be included in the next version bump. This PR includes changesets to release 3 packages
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 |
e8bcf2f to
5a8e411
Compare
|
Hi @eddeee888, the tests for this fix will only pass once the client plugins ( What are your thoughts on the best way to tackle this? |
|
Hi @eddeee888, if you can merge and release this fix I can update the rest of the plugins in this repo to latest |
|
Hi @wassim-k, |
5a8e411 to
982b839
Compare
…r nested fragments (graphQLTag) Since visitor-plugin-common v7, graphQLTag operations inline every transitively-spread fragment, so import a fragment's *Doc wherever it's interpolated, not just at direct-spread level.
982b839 to
152b967
Compare
|
Hi @eddeee888 Heads-up first: this can't be reproduced on Reproduction// codegen.ts
const config: CodegenConfig = {
schema: './schema.graphql',
documents: './src/**/*.graphql',
generates: {
'./src/types.ts': { plugins: ['typescript'] },
'./src/': {
preset: 'near-operation-file',
presetConfig: { extension: '.generated.ts', baseTypesPath: 'types.ts' },
plugins: ['typescript-operations', 'typescript-react-apollo']
}
}
}# schema.graphql
type Query {
list: [Book!]!
}
type Book {
id: ID!
title: String!
pages: [Page!]!
}
type Page {
id: ID!
number: Int!
}
# src/graphql/page-fragment.graphql
fragment Page on Page {
id
number
}
# src/graphql/book-fragment.graphql
fragment Book on Book {
id
title
pages {
...Page
}
}
# src/graphql/queries.graphql
query List {
list {
...Book
}
}
import { gql } from '@apollo/client';
import { BookFragmentDoc } from './book-fragment.generated'; // PageFragmentDoc never imported
export const ListDocument = gql`
query List {
list {
...Book
}
}
${BookFragmentDoc}
${PageFragmentDoc}`;The same shape already exists in this repo's dev-test ( What's in the PR nowI'd originally scoped this to the preset, but the fix isn't demonstrable — or safely releasable — on
Full suite is green (1591 passing). Happy to split this — the preset fix could land alone if you'd rather take the two majors |
Description
Since
visitor-plugin-commonv7, indocumentMode: graphQLTag(default) an operation document inlines every fragment it transitively spreads, while a fragment document inlines nothing. The preset still imports*Docs by direct-spread (level === 0), so:*Docof fragments reached through another fragment →Cannot find name 'XFragmentDoc';*Docs they no longer interpolate → unused imports.Reproduction
query List { list { ...Book } },fragment Book on Book { pages { ...Page } },fragment Page on Page { id }— each in its own file.Type of change
How Has This Been Tested?
Added new unit tests and ran the fixed version against our codebase with 100s of GraphQL queries
Notes
I have taken some liberty in trimming some code paths that are no longer used, which coincidentally happens to be my code from across the years