graphql-mongo-helpers icon indicating copy to clipboard operation
graphql-mongo-helpers copied to clipboard

Potential issue with MongoId fields

Open GeorgeMSchaffer opened this issue 7 years ago • 2 comments

Hi there, there seems to be an issue with fields prefixed with an "_" such as the _id as field, when I run:

const filter = buildMongoConditionsFromFilters({},{ _id: "5c3fa5a6c0db55422042e379", });

it results in: "id" is not a valid operator on field "". at handleFieldOperator ​./node_modules/@entria/graphql-mongo-helpers/dist/buildMongoConditionsFromFilters.js:39​ at ​​​Object.keys.reduce​​​ ​./node_modules/@entria/graphql-mongo-helpers/dist/buildMongoConditionsFromFilters.js:84​ at ​​​buildConditionsObject​​​ ​./node_modules/@entria/graphql-mongo-helpers/dist/buildMongoConditionsFromFilters.js:60​ at ​​​buildMongoConditionsFromFilters​​​ ​./node_modules/@entria/graphql-mongo-helpers/dist/buildMongoConditionsFromFilters.js:120​ at ​​​Object.​​​ ​scratchpad.js:4:0​"

This seems to stem from support from the way Mongo operators are handled. Fields are split on '_' to derive the operators. Since "id".split('') results in ['',id], "id" is assumed to be the operator and it causes to fail the valid operator test. This would also affect came cased fields but i saw from the comments that they are not supported.

Of course, i could be misreading the whole thing :) If not let me know if you would be interested in a pulll request. I am thinking easy way to solve it was thinking of adding another if statement to buildCondtionsObject to handle fields that start with "_". I also see a way to solve this and add support for camel casing without affecting backwards compatibility, but it would be larger refactor. Let me know your thoughts.

GeorgeMSchaffer avatar Jan 17 '19 19:01 GeorgeMSchaffer

can you add a failing test?

sibelius avatar Jan 17 '19 19:01 sibelius

the same problem occurred to me some time ago, I don't remember how I solved it.

Tsugami avatar Dec 17 '21 04:12 Tsugami