Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support ObjectID in findByFields() #75

Merged

Conversation

tubbo
Copy link
Contributor

@tubbo tubbo commented Aug 12, 2021

The findByFields() method could not find documents by a given ObjectID because the stringToId() function was only being run when querying for a field named _id. Since any MongoDB field could in theory contain an ObjectID, this broke queries looking for ObjectIDs in arbitrary fields. To resolve this, remove the condition testing for the field name of _id and run stringToId() on all values. This will validate whether a given String value is actually in the ObjectID format before proceeding, so it seems safe to apply everywhere.

The tests passed locally when I changed this implementation, which means there is probably something wrong with the mocks in the cache.test.js...might be something to look into. This at least confirmed my belief as I was observing very different behavior in the real world than what the tests would have suggested.

Fixes #65

The `findByFields()` method could not find documents by a given
ObjectID because the `stringToId()` function was only being run when
querying for a field named `_id`. Since any MongoDB field could in
theory contain an ObjectID, this broke queries looking for ObjectIDs in
arbitrary fields. To resolve this, remove the condition testing for the
field name of `_id` and run `stringToId()` on all values. This will
validate whether a given String value is actually in the ObjectID format
before proceeding, so it seems safe to apply everywhere.

Fixes GraphQLGuide#65
@lorensr lorensr merged commit 291d972 into GraphQLGuide:master Aug 21, 2021
@lorensr
Copy link
Member

lorensr commented Aug 21, 2021

Thanks for the fix! Published in 0.4.7

@bitops
Copy link
Contributor

bitops commented Sep 1, 2021

@tubbo @lorensr I tried upgrading to 0.4.7 today and findByFields stopped working for a single-field query. Possibly this version needs to be rolled back?

@lorensr
Copy link
Member

lorensr commented Sep 4, 2021

@bitops
Copy link
Contributor

bitops commented Sep 4, 2021

@lorensr yes, I went from 0.4.6 to 0.4.7 - I can give 0.5.0 a whirl at some point this weekend. Thanks! 😸

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Proper way to pass ObjectId in findByFields()
3 participants