我正在学习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'
答案 0 :(得分:1)
您错过了传递id作为参数。我的意思是应该是这样
def mutate(root, info, id=None, input=None):
或
def mutate(self, info, **args):
您将在args
中输入ID和输入。