【问题标题】:Question about graphene-django mutation documentation关于graphene-django突变文档的问题
【发布时间】:2021-03-07 01:12:14
【问题描述】:

虽然我已经能够使我的应用程序正常工作,但我还是担心以正确的方式做事。因此,有些东西我“不明白”:

在文档here 中,QuestionMutation 类中有一个question 属性。这实际上是什么意思?它说它定义了响应,我不明白这是什么意思。

文档中的代码:

class QuestionType(DjangoObjectType):
    class Meta:
        model = Question


class QuestionMutation(graphene.Mutation):
    class Arguments:
        # The input arguments for this mutation
        text = graphene.String(required=True)
        id = graphene.ID()

    # The class attributes define the response of the mutation
    question = graphene.Field(QuestionType)

    @classmethod
    def mutate(cls, root, info, text, id):
        question = Question.objects.get(pk=id)
        question.text = text
        question.save()
        # Notice we return an instance of this mutation
        return QuestionMutation(question=question)

在我的代码中,我已经能够做到这一点:

我的类型:

class UserType(DjangoObjectType):

    class Meta:
        model = User
        fields = (
            'id',
            'username',
            'password',
            'email',
            'first_name',
            'last_name',
            'is_active',
            'group_ids',
        )

    full_name = graphene.String()           # Python property
    full_identification = graphene.String() # Python property

我的突变:

class UpdateUser(graphene.Mutation):
    # --------> I could comment these and no problem. Why ? <--------
    # id = graphene.ID()
    # username = graphene.String()
    # email = graphene.String()
    # first_name = graphene.String()
    # last_name = graphene.String()
    # is_active = graphene.Boolean()

    class Arguments:
        id = graphene.ID()
        username = graphene.String()
        email = graphene.String()
        first_name = graphene.String()
        last_name = graphene.String()
        is_active = graphene.Boolean()

    class Meta:
        output = UserType

    @login_required
    def mutate(self, info, **kwargs):
        user = get_object_or_404(User, id=kwargs['id'])
        for attr, value in kwargs.items():
            setattr(user, attr, value)
        user.save()
        return user
        # I'm not returning explicit UserType, is it a problem ?
        # I actually do it with the Meta class. I guess it is the same for this ?

它可以工作,而我没有为响应属性指定任何内容。我什至不返回同样的东西。 如果我做错了,有人可以解释一下吗?

你可以在这里看到,如果我称之为突变:

mutation {
  updateUser (
    id: 42
    username: "updated_user"
    firstName: "updated"
    lastName: "user"
    email: "updated.user@test.com"
  ) {
    id
    username
    firstName
    lastName
    email
  }
}

即使没有返回值,我也能得到这个答案:

{
  "data": {
    "updateUser": {
      "id": "42",
      "username": "updated_user",
      "firstName": "updated",
      "lastName": "user",
      "email": "updated.user@test.com"
    }
  }
}

【问题讨论】:

    标签: django graphene-python graphene-django


    【解决方案1】:

    我还问了HERE这个问题,看来有更多关于这些配置的信息THERE

    最后,由于我设置了ouput = UserTypemodel = User,我的UserTypeUser 类是duck typed,即使没有定义返回字段(问题中评论的那些),它也像一个魅力一样工作)。

    换句话说,这个代码示例似乎是执行此操作的正确方法,官方文档并没有这样做,但它也打算像这样工作(您可以在@987654324 中阅读@)。

    class UserType(DjangoObjectType):
    
        class Meta:
            model = User
            fields = (
                'id',
                'username',
                'password',
                'email',
                'first_name',
                'last_name',
                'is_active',
                'group_ids',
            )
    
        full_name = graphene.String()           # Python property
        full_identification = graphene.String() # Python property
    
    class UpdateUser(graphene.Mutation):
            # We can omit return fields since UserType is defined as output ('output = UserType')
            # and UserType has already defined fields in its class.
    
        class Arguments:
            id = graphene.ID()
            username = graphene.String()
            email = graphene.String()
            first_name = graphene.String()
            last_name = graphene.String()
            is_active = graphene.Boolean()
    
        class Meta:
            output = UserType
    
        @login_required
        def mutate(self, info, **kwargs):
            user = get_object_or_404(User, id=kwargs['id'])
            for attr, value in kwargs.items():
                setattr(user, attr, value)
            user.save()
            return user
            # Returning a User can be done since it is linked to the UserType and we
            # said we return a UserType with 'output = UserType'
    

    【讨论】:

      猜你喜欢
      • 2023-03-29
      • 2021-11-07
      • 2019-04-22
      • 1970-01-01
      • 2020-04-25
      • 2018-07-13
      • 2020-01-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多