nexus icon indicating copy to clipboard operation
nexus copied to clipboard

Can't return undefined in a resolver that allows an optional

Open edjiang opened this issue 5 years ago • 2 comments

If I define:

query {
    success: Boolean
}
// This will work
resolve(_root, args, context) {
    return true
}

// This will work
resolve(_root, args, context) {
    return null
}

// This will NOT work
resolve(_root, args, context) {
    return undefined
}

Is there a reason to differentiate undefined with null? This means that I can't pass a typescript optional directly, and will have to return optional || null

edjiang avatar Dec 21 '20 05:12 edjiang

Me too.

acro5piano avatar Aug 05 '21 09:08 acro5piano

My workaround is to add a custom formatTypegen fn.

export const schema = makeSchema({
  types: [User, Mutation, Query, GraphQLDate],
  outputs: {
    typegen: path.resolve(__dirname, './__generated__/nexus-typegen.ts'),
    schema: path.resolve(__dirname, './__generated__/schema.graphql'),
  },
  formatTypegen: (code, type) => {
    if (type === 'types') {
      return code.replace(/null/g, 'null | undefined');
    }
    return code;
  },
});

acro5piano avatar Aug 05 '21 16:08 acro5piano