graphene-mongo icon indicating copy to clipboard operation
graphene-mongo copied to clipboard

Feat default mutation

Open abawchen opened this issue 7 years ago • 4 comments

abawchen avatar Jun 05 '18 09:06 abawchen

Pull Request Test Coverage Report for Build 149

  • 36 of 36 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.5%) to 94.817%

Totals Coverage Status
Change from base Build 142: 0.5%
Covered Lines: 311
Relevant Lines: 328

💛 - Coveralls

coveralls avatar Jun 05 '18 09:06 coveralls

Pull Request Test Coverage Report for Build 147

  • 36 of 36 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.5%) to 94.817%

Totals Coverage Status
Change from base Build 142: 0.5%
Covered Lines: 311
Relevant Lines: 328

💛 - Coveralls

coveralls avatar Jun 05 '18 09:06 coveralls

Pull Request Test Coverage Report for Build 147

  • 36 of 36 (100.0%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.5%) to 94.817%

Totals Coverage Status
Change from base Build 142: 0.5%
Covered Lines: 311
Relevant Lines: 328

💛 - Coveralls

coveralls avatar Jun 05 '18 09:06 coveralls

@abawchen Hey, looks like a good start. An idea would be to include function hooks that can optionally be passed to the mutator generators, such that they get run any time the mutation is called. This would be useful for people to integrate with these mutations to serve their needs, rather than just overwrite them any time they require more custom implementations.

i.e. def gen_create_mutation(resolvable, only_fields=(), exclude_fields=(), triggers=()):

Then in the create_mutation before the return, just iterate triggers and call each. For the update mutator you could pass in a dictionary of what fields were changed, i.e.

{
     'field': ('old value', 'new value')
}

Another thought is that these triggers could be called before the .save(), and that way they could potentially throw an exception to stop the mutation from occurring. We would create some exception class, i.e. TriggerPreventException and then it might look like this:

def create_mutate(self, info, **kwargs):
        resolvable = kwargs.pop('resolvable')
        mutation = kwargs.pop('klass')
        instance = resolvable._meta.model(**kwargs)
        
        try:
        # Run triggers, passing them the proper parameters.
        except TriggerPreventException:
            data = { 'success': False }
            return mutation(**data)

        instance.save()
        data = {
            resolvable._meta.model.__name__.lower(): instance,
            'success': True
        }
        return mutation(**data)

If I have time, I will try to work on this, but my schedule is a bit all over the place atm.

KCarretto avatar Jun 17 '18 22:06 KCarretto