Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Using interfaces in SerializerMutation #1163

Answered by zbyte64
tuanden0 asked this question in Q&A
Discussion options

I want to using interface with Mutation because there are some fields that I have calculate before return to user.
I can using interface to avoid rewrite code when return a list ojbects or a single object in Query.
But I can't use interface in Mutation to represent data before return to user after create of update it.

So i have to rewrite resolves to create and update.

How can I use interface with Mutation?

You must be logged in to vote

When you say interface do you mean the graphene interfaces like how relay.Node gets used?

In any event, I have used both mixin and decorator patterns to recycle code on mutations.

Sample of a decorator that can wrap the resolver method though I would recommend using a mixin if you can:

def mutate_record_history(Mutation):
 resolver = Mutation._meta.resolver
 # go around frozen
 Mutation._meta.__dict__["resolver"] = wrap_mutation_fn(Mutation, resolver)
 return Mutation

Edit: stackoverflow on mixins: https://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful

Replies: 2 comments

Comment options

When you say interface do you mean the graphene interfaces like how relay.Node gets used?

In any event, I have used both mixin and decorator patterns to recycle code on mutations.

Sample of a decorator that can wrap the resolver method though I would recommend using a mixin if you can:

def mutate_record_history(Mutation):
 resolver = Mutation._meta.resolver
 # go around frozen
 Mutation._meta.__dict__["resolver"] = wrap_mutation_fn(Mutation, resolver)
 return Mutation

Edit: stackoverflow on mixins: https://stackoverflow.com/questions/533631/what-is-a-mixin-and-why-are-they-useful

You must be logged in to vote
0 replies
Answer selected by zbyte64
Comment options

Great! I solved my problem, thank you for answer.

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants
Converted from issue

This discussion was converted from issue #1067 on April 14, 2021 20:01.

AltStyle によって変換されたページ (->オリジナル) /