Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support documentVariableSuffix when using documentMode: 'external' #674

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/plugins/typescript/react-apollo/src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ export class ReactApolloVisitor extends ClientSideBaseVisitor<
documentVariableName: string,
): string {
return this.config.documentMode === DocumentMode.external
? `Operations.${node.name?.value ?? ''}`
? `Operations.${node.name?.value ?? ''}${this.config.documentVariableSuffix}`
Copy link

@eddeee888 eddeee888 Jan 7, 2025

Choose a reason for hiding this comment

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

There's a special mode for importDocumentNodeExternallyFrom called near-operation-file that is intended to be used with Webpack GraphQL Loader. This loader allows the operation name from a .graphql file. For example, if we have the following file:

# document.graphql
query starwarCharacters {...}

then we can import and use it like this:

import * Operations from './document.graphql'

useQuery(Operations.starwarCharacters)

node.name?.value in this case is starwarCharacters. This key must match the operation name.


So, I think in this case, adding this.config.documentVariableSuffix here might impact the GraphQL loader use case because it requires documentVariableSuffix=''.

The fix is more likely this:

this.config.documentMode === DocumentMode.external &&
      this.config.importDocumentNodeExternallyFrom === 'near-operation-file'
      ? `Operations.${node.name?.value ?? ''}`
      : `Operations.${documentVariableName}`;

Could you try this change and update the tests please?

: documentVariableName;
}

Expand Down