Graphql-code-generator: typescript-resolvers' `mappers` config default aliasing not using alias

Created on 8 Feb 2020  路  10Comments  路  Source: dotansimha/graphql-code-generator

Describe the bug
The mappers property of the typescript-resolvers plugin isn't using the alias name for imports, but instead using the field name.

To Reproduce
Steps to reproduce the behavior:

Given this plugin config:

      - typescript-resolvers:
          mappers:
            Account: ./models/Account#default as AccountModel

Returns this output:

import { default as Account } from './models/Account';

Expected behavior output

import AccountModel from './models/Account';

Environment:

  • @graphql-codegen/...: 1.12.1

Additional context
Aliasing appears to be undocumented but I found it in #2932.

I think the issue is caused by https://github.com/dotansimha/graphql-code-generator/blame/87c18582d6b76093e3c1f1254eb67734d8b454b9/packages/plugins/other/visitor-plugin-common/src/mappers.ts#L38, introduced in #3189. I think this could be fixed by making namedDefault a separate conditional, like so:

      const namedDefault = items[1].includes('default as');
      asDefault = items[1] === 'default' || namedDefault;
      if (namedDefault) {
        const [, aliasType] = items[1].split(' as ');
        type = aliasType
        importElement = aliasType
      } else if (asDefault) {
        type = `${gqlTypeName}`;
        importElement = `${gqlTypeName}`;
      } else {
        if (items[1].includes(' as ')) {
          const [importedType, aliasType] = items[1].split(' as ');
          type = aliasType;
          importElement = `${importedType} as ${aliasType}`;
        } else {
          type = items[1];
          importElement = items[1];
        }
      }
bug plugins waiting-for-release

All 10 comments

Found a workaround for the time being:

      - typescript-resolvers:
          mappers:
            Account: ./models/Account#default  as AccountModel

(two spaces between default and as)

@kamilkisiela can you please take a look? :)

Why is import { default as Foo } from './models' bad since it's the same as import Foo from './models? It's a generated file so why does it matter, what's your use case?

The issue is that it doesn't use the alias. Even if it uses the import { default as Account } from './models/Account'; variation, it should still use the AccountModel alias and not Account. The only reason I dropped the default as is because it's unnecessary and non-standard.

Ohh I see!

@kamilkisiela I'm still encountering the issue in the alpha version. mapperTypeSuffix appears to be completely ignored, while the as aliasing is still broken.

  mapperTypeSuffix: Model
  mappers:
    Company: "../api/models/company.model#default as CompanyModel"
    Person: "../api/models/person.model#default"

yields this code:

import { default as Company } from '../api/models/company.model';
import Person from '../api/models/person.model'; // conflict

In both cases, I'd expect to see this as the output instead:

import CompanyModel from '../api/models/company.model';
import PersonModel from '../api/models/person.model';

@dotansimha @schmod https://github.com/dotansimha/graphql-code-generator/pull/3601

The mapperTypeSuffix was broken even before my PR, now it should be fine.

Fixed in v1.13.0

Can confirm that both issues are fixed in 1.13.0.

Thank you so much for the attention to detail that you've given this project!

Was this page helpful?
0 / 5 - 0 ratings