question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to write update function with graphene-django

See original GitHub issue

I am imprementing update function of the attribute of User with graphene-django

Of course we implement it as a mutation, mutate can be written with kwargs, We have to define all elements in Arguments class and field settings after all.

  1. how can we update only one element, for example?

Currently, I can not update without passing all elements

  1. How can I write these well with kwargs?

I want to change that there are three rewriting places for changing elements.(setting filed, arg, return)

thank you

class UpdateUser(graphene.Mutation):
    user = graphene.Field(UserType)
    username   = graphene.String(required=True)
    first_name = graphene.String(required=True)
    last_name  = graphene.String(required=True)
    #???password   = graphene.String(required=True)
    email      = graphene.String(required=True)


    class Arguments:
        username   = graphene.String(required=True)
        first_name = graphene.String(required=True)
        last_name  = graphene.String(required=True)
        #???password   = graphene.String(required=True)
        email      = graphene.String(required=True)


    @login_required
    def mutate(self, info, **kwargs):
        user = info.context.user
        for k, v in kwargs.items():
            user.k = v
        # user.set_password(password)
        user.save()
        return UpdateUser(username=username,
                      first_name=first_name,
                      last_name=last_name,
                      email=email )

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

8reactions
bianchiidentitycommented, Aug 15, 2018
class UserInput(graphene.InputObjectType):
    username   = graphene.String(required=False)
    first_name = graphene.String(required=False)
    last_name  = graphene.String(required=False)
    password   = graphene.String(required=False)
    email      = graphene.String(required=False)


class UpdateUser(graphene.Mutation):
    user = graphene.Field(UserType)

    class Arguments:
        user_data = UserInput(required=True)

    @login_required
    def mutate(self, info, user_data=None):
        user = info.context.user
        
        for k, v in user_data.items():
            if (k == 'password') and (v is not None):
                user.set_password(user_data.password)
            else:
                setattr(user, k, v)

        try:
        user.full_clean()
        user.save()
        return UpdateUser(user=user)
    except ValidationError as e:
        return UpdateUser(user=user, errors=e)
1reaction
bianchiidentitycommented, Aug 15, 2018
mutation {
  updateUser(userData: {username: "yes", email: "ueoa@gmail.com", firstName: "k", lastName: "k"}){
    user {
      username
	  email
	  firstName
	  lastName
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Mutations - Graphene-Python
Graphene -Django makes it easy to perform mutations. With Graphene-Django we ... Use the method get_serializer_kwargs to override how updates are applied.
Read more >
Create or Update Update a many-many ... - Stack Overflow
This should fix it obj, created = models.Rating.objects.update_or_create( user_id=user, movie_id=movie, defaults={"rating": rating} ).
Read more >
Building GraphQL APIs in Django with Graphene - Twilio
Learn how to build a GraphQL API in Django using the Graphene package.
Read more >
GraphQL with Django — simple yet powerful (CRUD) — Part 2
So this time around we're going to dive-in deep and cover all the CRUD operations (Create, Read, Update, Delete). But as far as...
Read more >
Django + Graphene: From REST to GraphQL - FullStack Labs
Graphene provides us with a schema object attached with resolve methods to fetch data. In Django, we are accustomed to creating apps according...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found