dotansimha/graphql-code-generator

Replace `Promise` with `PromiseLike` in resolver types

Open

#6 378 ouverte le 29 juil. 2021

Voir sur GitHub
 (4 commentaires) (1 réaction) (0 assignés)TypeScript (1 295 forks)batch import
corehelp wantedkind/enhancement

Métriques du dépôt

Stars
 (10 341 stars)
Métriques de merge PR
 (Merge moyen 22j 9h) (53 PRs mergées en 30 j)

Description

Is your feature request related to a problem? Please describe.

When using non-native Promise library like Bluebird, the return type of resolvers will often be PromiseLike, but it doesn't adapt with generated resolver-types because the generated code is using native Promise type.

https://github.com/dotansimha/graphql-code-generator/blob/8e4d5826df7c01fec3fa26d200d171881ee3a04a/packages/plugins/typescript/resolvers/src/index.ts#L138

sample code:

const resolvers: Resolvers = {
  Query: {
    name: () => { // string field
      return Promise.resolve('1') as PromiseLike<string>
    }
  }
}

error message:

(...omitted)
    Type 'PromiseLike<string>' is not assignable to type 'ResolverTypeWrapper<string> | Promise<ResolverTypeWrapper<string>>'.
      Type 'PromiseLike<string>' is missing the following properties from type 'Promise<ResolverTypeWrapper<string>>': catch, finally, [Symbol.toStringTag]

Describe the solution you'd like

Can Promise be just replaced with PromiseLike?

:memo: https://github.com/graphql/graphql-js/blob/0fef3c49907b63b1ea5a4ff1da7011775f465fc2/src/execution/execute.ts#L505-L506

Describe alternatives you've considered

We can customize it by using customResolverFn.

So this feature request is not mandatory, but I feel it's better if I don't have to customize and manage it in our code.

Additional context

Guide contributeur