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

Posted

技术标签:

【中文标题】更新公司时,参数“输入”的值 id 无效【英文标题】:Argument \"input\" has invalid value id when updating the company 【发布时间】:2019-03-05 14:29:39 【问题描述】:

我正在学习 graphql,所以我正在使用 graphene-django。我正在为 CRUD 应用程序开发和 api。但是,我在更新公司时遇到错误。我收到以下错误“参数“输入”具有无效值”。

这就是我所做的

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】:

您错过了将 id 作为参数传递。我的意思是应该是这样的

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

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

您将在args 中拥有 id 和输入。

【讨论】:

以上是关于更新公司时,参数“输入”的值 id 无效的主要内容,如果未能解决你的问题,请参考以下文章

输入了无效参数:

webservice接口,如何用Java代码更新数据库中的一个字段的值,其他字段值保持不变?

输入的地址似乎无效。请更正您的地址并重新提交

ASP.NET MVC - 输入值更改更新 URL 参数

ORA-00904 使用 CASE 语句更新表时标识符无效

安装oracle时提示:ORA-01078处理系统参数失败