Search code examples
pythondjangographqlgraphene-python

Argument \"input\" has invalid value id when updating the company


I am learning graphql so i am using graphene-django. I am developing and api for CRUD apps. However, I am getting an error while updating the company. I get following error 'Argument \"input\" has invalid value'.

Here is what I have done

class CompanyInput(graphene.InputObjectType):

    name = graphene.String(description='Name of your company')
    email = graphene.String(description='Email of your company')
    phone_number = graphene.String(description='Phone number of your company')
    director = graphene.String(description='Director of your company')
    franchise_head = graphene.String(description='Franchise Head of your company')

class UpdateCompany(graphene.Mutation):

    class Arguments:

        input = CompanyInput(description="These fields are required", required=True)
        id = graphene.String(required=True)

    class Meta:
        description = "Update an existing company"
        # model = models.Company

    errors = graphene.String()
    company = graphene.Field(CompanyNode)

    @staticmethod
    def mutate(root, info, input=None):
        print('######################')
        print('root', root)
        print('######################')
        print('info', info, info.context, info.context.user, info.context.user.is_authenticated)
        if not info.context.user.is_authenticated:
            return UpdateCompany(errors=json.dumps('Please Login to continue'))
        try:
            company_instance = models.Company.objects.get(id=id)
            if company_instance:
                company = models.Company(owner=info.context.user,
                                         name=input.name,
                                         email=input.email,
                                         phone_number=input.phone_number,
                                         director=input.director,
                                         franchise_head=input.franchise_head
                                         )
                return UpdateCompany(company=company, instance=company_instance)
        except models.Company.DoesNotExist:
            return UpdateCompany(errors=json.dumps('No Company Exist to Update'))


class Mutation(graphene.ObjectType):

    create_company = CreateCompany.Field()
    update_company = UpdateCompany.Field()

The query for updating the company is

mutation {
  updateCompany(input: {id: "Q29tcGFueU5vZGU6MQ==", name: "Demo Company", email: "[email protected]", phoneNumber: "984217846", director: "Mr Director", franchiseHead: "Mr Franchise"}) {
    errors
    company {
      id
      name
    }
  }
}

I tried the following query as well

mutation {
      updateCompany(id: "Q29tcGFueU5vZGU6MQ==", input: {name: "Demo Company", email: "[email protected]", phoneNumber: "984217846", director: "Mr Director", franchiseHead: "Mr Franchise"}) {
        errors
        company {
          id
          name
        }
      }
    }

This query throws an error mutate() got an unexpected keyword argument 'id'


Solution

  • You missed to pass an id as argument. I mean it should be something like this

    def mutate(root, info, id=None, input=None):
    

    or

    def mutate(self, info, **args):
    

    you will have id and input inside args.