Vincit / objection-graphql

GraphQL schema generator for objection.js
MIT License
307 stars 35 forks source link

Support "in-model" filters and arguments #59

Open SkeLLLa opened 6 years ago

SkeLLLa commented 6 years ago

It would be nice to have ability to define args and filters that should be exposed. So let's see the example.

By default it exposes lots default fileters and args. Also module allows to add custom filters through .argFactory() method.

So the first problem is that it's hard to remove not needed filters for certain fields. Also it's became rather complicated when you have lots of models defined in different files. So in order to add some filters or args for each model, you'll need to define all that stuff somewhere (probably in your model file), then collect it into one object in file where schema is built and pass it to .argFactory() method.

Instead of doing this it would be better if module will check for presence of some static properties in model class e.g.:

class MyModel extends Model {
  static get defaultFilters() {
    return {
      id: ['eq', 'isNull'],
      time: ['eq', 'gt', 'gte', 'lt', 'lte']
    };
  }
}

So when model will be generated it will try to get this property and if it exist it will only add filters for each field that are defined in that object.

The same approach could be implemented for additional filters and args. And as a result .argFactory() method will be under the hood of this module and users will just define all the stuff they need directly in model class.

So it will be something similar to @timhuff approach with mutations https://github.com/Vincit/objection-graphql/pull/43. But in this case there will be no need to call additional method like extendWithModelMutations, it would just check for some properties in modelClass and get all info from there.

timhuff commented 6 years ago

This is exactly how I feel this library should be set up.