dotansimha / graphql-code-generator-community

MIT License
114 stars 147 forks source link

`typescript-urql` incorrectly prefixes document variables #180

Open arempe93 opened 2 years ago

arempe93 commented 2 years ago

Issue workflow progress


Describe the bug

When using import-types with typescript-urql, the generated document variable will also incorrectly get prefixed. This same behavior also occurs if you set the importOperationTypesFrom config manually with no presets.

import * as Types from './graphql.d'

const UserDocument = ...

export function useUserQuery(options?: Omit<Urql.UseQueryArgs<Types.UserQueryVariables>, 'query'>) {
  return Urql.useQuery<Types.UserQuery>({ query: Types.UserDocument, ...options });
};

Link to location in reproduction

I believe the problem code is here:

https://github.com/dotansimha/graphql-code-generator/blob/cb59afdcea134385e34d544b90e1cd189945230a/packages/plugins/typescript/urql/src/visitor.ts#L145-L147

Sidenote

This greedy prefixing behavior does not occur if you use the near-operation-file preset with baseTypesPath+importTypesNamespace. It actually does the opposite and doesn't prefix anything; you need to use the typesPrefix config. But at least that config does not prefix the document variable.

To Reproduce

My codegen.yml config file:

schema: schema.graphql
documents: document.graphql
generates:
  graphql.d.ts:
    plugins:
      - typescript
      - typescript-operations
  index.ts:
    preset: "import-types"
    presetConfig:
      typesPath: "./graphql.d"
    plugins:
      - typescript-urql

Link to codegen.yml in reproduction

Expected behavior

A Types. prefix will not be added to the document variable when using import-types

Workaround

Use the add plugin with typesPrefix config instead of import-types preset.

schema: schema.graphql
documents: document.graphql
generates:
  graphql.d.ts:
    plugins:
      - typescript
      - typescript-operations
  index.ts:
    # preset: "import-types"
    # presetConfig:
    #   typesPath: "./graphql.d"
    plugins:
      - typescript-urql
      - add:
          content: "import * as Types from './graphql.d'"
    config:
      typesPrefix: "Types."

Environment:

fr0ntenddev commented 2 years ago

same issue observed