apollo-datasource-mongodb icon indicating copy to clipboard operation
apollo-datasource-mongodb copied to clipboard

`findOneByFields` - new method

Open RemyMachado opened this issue 3 years ago • 2 comments

I am wondering why isn't there a findOneByFields method, similar to the existing findByFields one.

Currently I'm using this hack:

    async findOneByFields(values) {
        return this.findByFields(values).then((documents) => documents?.[0])
    }

Is there a reason why it's not provided?

Is my wrapping function useless because of how the batching/caching work?

RemyMachado avatar Oct 05 '22 19:10 RemyMachado

No reason I know of. Batching and caching should work with your helper

lorensr avatar Oct 08 '22 22:10 lorensr

Not that I think it's necessary, but one major use case for this would be authenticated queries where it's necessary to identify the user trying to access the document.

Example:

async findOneByFields(documentId, userId) {
    return this.findByFields({ _id: documentId, userId }).then(documents => documents?.[0]);
}

richardcarrigan avatar Feb 02 '23 15:02 richardcarrigan