pythongraphene-pythongraphql-python

Graphene Python mutation list input


I have a mutation that looks like this

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)

Here, I'm trying to pass a list of users at a time to update some values in my code. But the mutation is throwing errors. Can someone suggest a list input for the mutation as an example?


Solution

  • Passing any of list of values of other types is easy using similar to list(String) For example

    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)
    

    In the above code, to pass a list of user ids and list of strings I've used list(ID) & list(String). It worked