graphql-code-generator-community icon indicating copy to clipboard operation
graphql-code-generator-community copied to clipboard

Cannot import typescript operation definition file with 'near-operation-plugin' and document: external

Open eltonio450 opened this issue 1 year ago • 1 comments

Which packages are impacted by your issue?

@graphql-codegen/near-operation-file-preset

Describe the bug

Hello!

When using 'near-operation-file' with 'typescript-react-apollo' (but I guess it's the same with other plugins), we cannot import typescript files containing queries/mutations, only .graphql files work

Your Example Website or App

https://codesandbox.io/p/devbox/youthful-bose-wxjhxz

Steps to Reproduce the Bug or Issue

Using this config:

import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
  schema: "schema.graphql",
  documents: "document.ts",
  emitLegacyCommonJSImports: false,
  generates: {
    ".": {
      plugins: ["typescript-react-apollo"],
      preset: "near-operation-file",
      presetConfig: {
        baseTypesPath: "./types.ts",
      },
      config: {
        importDocumentNodeExternallyFrom: "near-operation-file",
        importOperationTypesFrom: "Types",
        documentMode: "external",
      },
    },
    "types.ts": { plugins: ["typescript", "typescript-operations"] },
  },
};

export default config;

this config makes sense: the goal is to import queries/mutations defined in typescript files into local mtFile.generated.ts

Expected behavior

in the reproduction repository, line 25 of document.generated.ts should be: `return Apollo.useQuery<Types.UserQuery, Types.UserQueryVariables>(Operations.userQuery, options); ``

and not: return Apollo.useQuery<Types.UserQuery, Types.UserQueryVariables>(Operations.user, options);

userQuery is the name of the typescript variable, whereas user is the name of the graphql query

Screenshots or Videos

No response

Platform

  • OS: MacOS
  • NodeJS: 18
  • graphql 16.3.0
  • @graphql-codegen/* version(s): latest

Codegen Config File

import { CodegenConfig } from "@graphql-codegen/cli";

const config: CodegenConfig = {
  schema: "schema.graphql",
  documents: "document.ts",
  emitLegacyCommonJSImports: false,
  generates: {
    ".": {
      plugins: ["typescript-react-apollo"],
      preset: "near-operation-file",
      presetConfig: {
        baseTypesPath: "./types.ts",
      },
      config: {
        importDocumentNodeExternallyFrom: "near-operation-file",
        importOperationTypesFrom: "Types",
        documentMode: "external",
      },
    },
    "types.ts": { plugins: ["typescript", "typescript-operations"] },
  },
};

export default config;

Additional context

remotely related to https://github.com/dotansimha/graphql-code-generator/issues/3272#issuecomment-1427850754

eltonio450 avatar Mar 29 '24 15:03 eltonio450

Unfortunately its the same for typescript-react-query, it simply does not use the correct types inside each operation file.

dohomi avatar Aug 28 '24 06:08 dohomi