graphql-compose / graphql-compose-mongoose

Mongoose model converter to GraphQL types with resolvers for graphql-compose https://github.com/nodkz/graphql-compose
MIT License
709 stars 94 forks source link

If a TC's findMany resolver is added to schema multiple times, filter options for subsequent resolvers are ignored #421

Open hagabaka opened 1 year ago

hagabaka commented 1 year ago

When you use a TC's findMany resolver multiple times in schema, for example, first to add a relation, then to add a findMany query, and then to add a pagination query:

X.addRelation('y', {
  resolver: X.mongooseResolvers.findMany({filter: filterOptions1})
}),
schemaComposer.Query.addFields({
  xFindMany: X.mongooseResolvers.findMany({filter: filterOptions2}),
  xPaginate: X.mongooseResolvers.paginate({
   findManyOpts: {filter: filteroptions3}
  })
});

Then xFindMany and xPaginate queries will use filterOptions1, but not filterOptions2 or filterOptions3.

It seems that by default the generated filter input type for all the findMany resolvers of the same model share the same name, so the type is created for the first findMany, and reused for all other ones. If this default behavior can't be changed, it would be nice to at least have a run time warning and/or FAQ entry in documentation about the limitation.

ZachFontenot commented 1 year ago

I also ran into this issue and I thought I was going crazy because passing the options was doing nothing, but now I see why this is happening.

And actually, I can't track down which is running first, so I'm unable to add any options at all?

hagabaka commented 1 year ago

I think a workaround is using baseTypeName, prefix, and/or suffix options of FilterHelperArgsOpts to have different type names for different usages of findMany. https://github.com/graphql-compose/graphql-compose-mongoose#filterhelperargsopts