代码之家  ›  专栏  ›  技术社区  ›  milan

更新公司时,参数“input”的值id无效

  •  3
  • milan  · 技术社区  · 6 年前

    graphene-django

    这就是我所做的

    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()
    

    更新公司的查询为

    mutation {
      updateCompany(input: {id: "Q29tcGFueU5vZGU6MQ==", name: "Demo Company", email: "abc@gmail.com", phoneNumber: "984217846", director: "Mr Director", franchiseHead: "Mr Franchise"}) {
        errors
        company {
          id
          name
        }
      }
    }
    

    mutation {
          updateCompany(id: "Q29tcGFueU5vZGU6MQ==", input: {name: "Demo Company", email: "abc@gmail.com", phoneNumber: "984217846", director: "Mr Director", franchiseHead: "Mr Franchise"}) {
            errors
            company {
              id
              name
            }
          }
        }
    

    此查询引发错误 mutate() got an unexpected keyword argument 'id'

    1 回复  |  直到 6 年前
        1
  •  1
  •   Tushant    6 年前

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

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

    args