0

我有一个看起来像这样的突变

class ApisInteractiveGlobal(BaseMutation):
success = Boolean()
error = String()

class Arguments:
    user_id = List(ID)
    api_name = String()
    global_apis = Boolean()
    my_org_api = Boolean()

def mutate(self, info, user_id=[], api_name=None, global_apis=None, my_org_api=None):
    for id in user_id:
        user = User.objects.get(id=id)
        api = user.apis_interactive.get(name=api_name)
        if my_org_api is not None:
            api.my_orgapis = my_org_api
        if global_apis is not None:
            api.global_apis = global_apis
        api.save(update_fields=['global_apis', 'my_orgapis'])
    return ApisInteractiveGlobal(success=True)

在这里,我试图一次传递一个用户列表来更新我的代码中的一些值。但是突变会引发错误。有人可以建议突变的列表输入为例吗?

4

1 回答 1

0

传递任何其他类型的值列表很容易使用类似于 list(String) 例如

class ApisInteractiveGlobal(BaseMutation):
success = Boolean()
error = String()

class Arguments:
    user_id = List(ID)
    apis = List(String)
    global_apis = Boolean()
    my_org_api = Boolean()

def mutate(self, info, user_id=[], apis=[], global_apis=None, my_org_api=None):
    for id in user_id:
        for api in apis:
            user = User.objects.get(id=id)
            api = user.apis_interactive.get(name=api)
            if my_org_api is not None:
                api.my_orgapis = my_org_api
            if global_apis is not None:
                api.global_apis = global_apis
            api.save(update_fields=['global_apis', 'my_orgapis'])
    return ApisInteractiveGlobal(success=True)

在上面的代码中,为了传递用户 ID 列表和字符串列表,我使用了 list(ID) 和 list(String)。有效

于 2021-04-02T04:41:29.747 回答