GraphQLGuide / apollo-datasource-mongodb

Apollo data source for MongoDB
MIT License
285 stars 64 forks source link

`findOneByFields` - new method #112

Open RemyMachado opened 2 years ago

RemyMachado commented 2 years ago

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?

lorensr commented 2 years ago

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

richardcarrigan commented 1 year ago

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]);
}