Skip to content

Can't return null from nullable filed resolver #3174

Closed
@kamilchm

Description

@kamilchm

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

Metadata

Metadata

Assignees

Labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions