diff --git a/packages/mongodb/src/resolvers.ts b/packages/mongodb/src/resolvers.ts index fb3604e9e5..695c97c822 100644 --- a/packages/mongodb/src/resolvers.ts +++ b/packages/mongodb/src/resolvers.ts @@ -19,6 +19,10 @@ export async function resolveQueryObjectId( ): Promise> export async function resolveQueryObjectId(value: ObjectIdParam): Promise export async function resolveQueryObjectId(value: ObjectIdParam | IdQueryObject) { + if (!value) { + return undefined + } + if (typeof value === 'string' || typeof value === 'number' || value instanceof ObjectId) { return toObjectId(value) } diff --git a/packages/mongodb/test/resolvers.test.ts b/packages/mongodb/test/resolvers.test.ts index b6d0cffc54..597a9fccf6 100644 --- a/packages/mongodb/test/resolvers.test.ts +++ b/packages/mongodb/test/resolvers.test.ts @@ -24,4 +24,12 @@ describe('ObjectId resolvers', () => { assert.ok(oids.$in && oids.$in[0] instanceof ObjectId) assert.ok(oids.$ne instanceof ObjectId) }) + + it('resolveQueryObjectId with falsey value', async () => { + await resolveQueryObjectId(undefined) + await resolveQueryObjectId(null) + await resolveQueryObjectId(0) + + assert.ok('Falsey value does not throw exception') + }) })