apollo-datasource-mongodb
apollo-datasource-mongodb copied to clipboard
`findOneByFields` - new method
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?
No reason I know of. Batching and caching should work with your helper
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]);
}