关于 graphene-django 突变文档的问题

Question about graphene-django mutation documentation

虽然我已经能够使我的应用程序工作,但我对以正确的方式做事有一些担忧。因此,有些事情我“不明白”:

在文档中,here,在QuestionMutation class中,有一个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 ?

它有效,而我没有为响应属性指定任何内容。我什至不 return 同类的东西。 有人可以解释我是否做错了吗?

你可以在这里看到,如果我调用这个突变:

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

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

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

我也问了这个问题HERE and it appears that there is more information about these configuration THERE

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

换句话说,这个代码示例似乎是执行此操作的正确方法,官方文档没有这样做,但它也打算这样工作(正如您可以在code docs).

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'