graphql-code-generator: Can't return `null` from nullable filed resolver

To Reproduce Steps to reproduce the behavior:

  1. My GraphQL schema:
type Query {
  optional: Result
}
type Result {
  mandatory: String!
}
  1. My GraphQL operations:
const resolvers: Resolvers = {
  Query: {
    optional: () => Promise.resolve("x").then(r => (r ? {} : null))
  }
};

Expected behavior null is a valid result type, but I get:

src/resolvers.ts(4,3): error TS2322: Type '{ optional: () => Promise<Result | Promise<Result> | {} | null>; }' is not assignable to type 'QueryResolvers<MyContext, {}>'.
      Types of property 'optional' are incompatible.
        Type '() => Promise<Result | Promise<Result> | {} | null>' is not assignable to type 'ResolverFn<Maybe<ResolverTypeWrapper<Result>>, {}, MyContext, {}> | StitchingResolver<Maybe<ResolverTypeWrapper<Result>>, {}, MyContext, {}> | undefined'.
          Type '() => Promise<Result | Promise<Result> | {} | null>' is not assignable to type 'ResolverFn<Maybe<ResolverTypeWrapper<Result>>, {}, MyContext, {}>'.
            Type 'Promise<Result | Promise<Result> | {} | null>' is not assignable to type 'Result | Promise<Result> | Promise<Maybe<ResolverTypeWrapper<Result>>> | null'.
              Type 'Promise<Result | Promise<Result> | {} | null>' is not assignable to type 'Promise<Result>'.
                Type 'Result | Promise<Result> | {} | null' is not assignable to type 'Result'.
                  Type 'null' is not assignable to type 'Result'.

Environment:

  • OS: Linux
  • @graphql-codegen/...: 1.9.1
  • NodeJS: 12

About this issue

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

Commits related to this issue

Most upvoted comments

@wallzero Adding my solution as the Maybe value fixes this issue.

    plugins:
      - typescript
      - typescript-resolvers
    config:
      ....
      maybeValue: 'T extends PromiseLike<infer U> ? Promise<U | null> : T | null'

For me, the problem was a different one, however the error message is very misleading. I also got the error null is not assignable to type XXX. It turned out, that returning null was not the problem. The actual type that I returned from the resolver did not match the expected type for one property. => Make sure the object you return matches the expected return type of the resolver.

@dotansimha I think this should be left open. Promises should be able to return null in case of issue.

@freshollie could you share how you are resolving the promise as a work around? I am trying to figure out a solution while keeping type safety.

For me, the problem was a different one, however the error message is very misleading. I also got the error null is not assignable to type XXX. It turned out, that returning null was not the problem. The actual type that I returned from the resolver did not match the expected type for one property. => Make sure the object you return matches the expected return type of the resolver.

I ran into this issue too and thought it was a problem with the generated types, but like @kirschem , the issue was with my returned internal type not matching the graphql type exactly.

@dotansimha Sorry to be pain - but just run into this as well. Its clearly still issue - why this can’t be reopened since it has all the details it needs?

@Strato 100% correct, I’ve also noticed this years later. However I did find that using the maybe type did help make the typescript error easier to understand and helped me solve my problem

maybeValue: ‘T extends PromiseLike<infer U> ? Promise<U | null> : T | null’

So this didn’t fix the issue I was having, but it did make it obvious where the issue was which I was unable to decipher before that. After I fixed the issue I was able to remove it again.

@kamilchm I’m closing for now. If you have an idea for improvement feel free to share with us 😃