graphql-code-generator: `mappers` option results in duplicate identifiers in the generated TS file

having some types remapped using the mappers option leaves both the new (imported) type and the old (generated) type in the generated file.

apparently it wasn’t an issue before, but now the new Typescript does not like that and I get

TS2440: Import declaration conflicts with local declaration

About this issue

  • Original URL
  • State: closed
  • Created 5 years ago
  • Reactions: 6
  • Comments: 21 (5 by maintainers)

Most upvoted comments

Is there maybe an option to automagically alias every mapper?

So for example a config like;

config:
  mapperTypeSuffix: Model
  mappers:
    Product: ./services/product/models#Product

Would generate an import like;

import { Product as ProductModel } from './services/product/models';

@helios1138 @janhartmann You can try to import those like below;

mappers:
        Node: import('./graphql/resolvers').Node
        User: import('./services/Users').User
        Thread: import('./services/Threads').Thread

Available in v1.10.0.

PR #2957 seems to do the correct job as it’s outputting the correct information.

{
  default: false,
  isExternal: true,
  source: '../modules/users/model',
  type: 'UserRecord',
  import: 'User as UserRecord'
}

But somehow, the actual file output does not contain the alias:

import { UserRecord } from '../modules/users/model';

Hi, I’m not sure this actually works - or maybe I’m just missing something… I’m on version 1.9.1. Let’s say I have the following structure:

// src/orm/entities/user.ts
export class User {
  readonly id: string;
  readonly name: string;
}
# src/graphql/users/schema.graphql
type User {
  id: ID!
  name: String!
}

type Query {
  users: [User!]!
}
# codegen.yml
generates:
  src/graphql/types.ts:
    schema: src/graphql/users/schema.graphql
    plugins:
      - typescript
      - typescript-resolvers
    config:
      mappers:
        User: 'src/orm/entities/user#User as UserEntity'

This results in the following line at the beginning of the generated types.ts:

import { UserEntity } from '../orm/entities/user';

The User as bit of the import seems to be somehow lost in translation… which makes the result quite broken. Any idea what am I doing wrong? Pretty please, with cherry on top?

@lachenmayer Could you try using aliases with the following canary version? 1.8.4-alpha-d88e5f9a.53+d88e5f9a

@ardatan this does indeed work, thanks. Though it looks more like a workaround since it makes the config a bit harder to read and results in duplicates, for example, if generics are also used

Thread: import('./services/Threads').Thread
ThreadConnection: Connection<import('./services/Threads').Thread>

In any case, if import as is not an option at this moment, I think it would be good if the workaround you mentioned would be added to the docs since I believe it’s a pretty common case when database types and graphql types have the same name.

Currently, as far as I see, the docs only mention path#Name way of importing

I am hitting this issue as well, when upgrading to TypeScript 3.7.2

The as X would be helpful or something like import * as Something from "./something".