graphql-compose-mongoose icon indicating copy to clipboard operation
graphql-compose-mongoose copied to clipboard

Nested filter always returns all entries

Open misterboe opened this issue 5 years ago • 3 comments

I try to filter by a nested field of type boolean. Autosuggestion works fine, but the filter does nothing. All records where shown in the results.

{
  pages(filter:{response:{hasErrors:true}}) {
    projectID
    url
    session
    response {
      hasErrors
      status
      headers
    }
  }
}

schemaComposer looks like that:

schemaComposer.Query.addFields({
    pages: PagesTC.mongooseResolvers.findMany(),
    page: PagesTC.mongooseResolvers.findOne(),
})

const graphqlSchema = schemaComposer.buildSchema()
export default graphqlSchema

misterboe avatar Oct 27 '20 20:10 misterboe

I was banging my head against nested operators last night before realizing this morning that it's really a nested filter issue. FWIW, it appears that filters on nested objects within arrays work. Any luck digging around to see where the issue might be?

kaijuliu avatar Jan 11 '21 17:01 kaijuliu

After digging around in the code, I discovered that nested filters (and thus nested operators) work if you use subdocuments versus nested paths (https://mongoosejs.com/docs/subdocs.html#subdocuments-versus-nested-paths). I was looking at https://github.com/graphql-compose/graphql-compose-mongoose/blob/master/src/resolvers/helpers/filter.ts#L147 and discovered that:

  • subdocuments leave their parent field names in modelFields (e.g. pages) and fall into that else case
  • nested paths leave their dot-qualified names in modelField (e.g. pages.projectID) and fall out of all the cases

Hope this helps!

kaijuliu avatar Jan 11 '21 18:01 kaijuliu

I likewise spent quite some time smashing my head into my keyboard working out this exact issue.

Turns out after also sleuthing through the repo, it looks like this issue has been fixed. Updating to 9.2.2 solved the problem for me.

Seems to be a duplicate of #304 which was then tweaked for #315

Should be good to close?

SeanLMcCullough avatar Apr 13 '21 05:04 SeanLMcCullough