Open WouterLVV opened 8 months ago
@WouterLVV we were facing the same issue. We added documentVariableSuffix: ""
to the base type config:
src/generated/types.tsx:
plugins:
- "typescript"
- "typescript-operations"
- "typed-document-node"
config:
documentVariableSuffix: ""
This resolved the issue for us. Nevertheless I also think that it would be very nice if typescript-react-apollo
would apply the DocumentSuffix
while using documentmode: external
Which packages are impacted by your issue?
typescript-react-apollo
Describe the bug
When using the config option
doucmentMode: external
, you cannot customize the name of the GraphQL documents it is importing. Since the default does not correspond to the documents outputted by typed-document-node, this leads to not finding the import.In slightly longer: I am trying to adopt typed-document-node into an existing project. In the current configuration of the project we have one file that generates the operations through the
typescript
andtypescript-operations
plugin, and a different file is generated with thetypescript-react-apollo
plugin.typed-document-node
seems to require to be in the same file astypescript
andtypescript-operations
. However, by default react-apollo also generates its own documents (that are not typeable for as far as I can tell). This duplicate generation causes problems and is generally undesired, so I tried thedocumentNode: external
setting, pointing to the correct file.When using
documentmode: external
for react-apollo, it does not apply any of the naming convention specificied and thus results in a different name it is trying to import.Sidenote: moving to the client preset is currently not an option for this project.
Your Example Website or App
https://github.com/WouterLVV/typescript-react-apollo-external-import-example
Steps to Reproduce the Bug or Issue
Create the following 2 'generates' in your codegen.ts:
Expected behavior
with the example documents, there exists the query
user(...): ...
(in this example only the query name is relevant). The generated GraphQL document in types.ts is calledUserDocument
. The automatically generated import in react-apollo.ts isimport * as Operations from './types';
Current behaviour: react-apollo.ts references
Operations.user
. Expected behaviour: react-apollo.ts referencesOperations.UserDocument
.Screenshots or Videos
No response
Platform
graphql
version: 16.8.1Codegen Config File
import { CodegenConfig } from "@graphql-codegen/cli";
const config: CodegenConfig = { schema: "schema.graphql", documents: "document.graphql", generates: { "types.ts": { plugins: ["typescript", "typescript-operations", "typed-document-node"] }, "react-apollo.ts": { plugins: [ { "typescript-react-apollo": { documentMode: "external", importOperationTypesFrom: "Operations", importDocumentNodeExternallyFrom: "./types" } }]} }, };
export default config;
Additional context
Currently when documentMode is external, react-apollo takes the raw operation name:
I believe the solution would be along the lines of: