[英]Error while passing a json field in graphql mutation with django
the below code is to update the "personalize" field in the user model (django), which is a json field, not sure how to pass it has an argument to the mutation下面的代码是更新用户model(django)中的“个性化”字段,这是一个json字段,不确定如何传递它有一个参数给突变
class AddPersonalization(graphene.Mutation):
ok = graphene.Boolean()
class Arguments():
user_id = graphene.Int(required=True)
personalize = graphene.JSONString(required=True)
def mutate(self, user_id, personalize):
try:
get_user_model().objects.filter(id=user_id).update(personalize=personalize)
except get_user_model().DoesNotExist:
raise Exception("User doesn't exist")
return AddPersonalization(ok=True)
graphql query graphql查询
mutation{
addPersonalization(userId :285 ,personalize:["sample1", "sample2"] )
{
ok
}
}
error response:错误响应:
{
"errors": [
{
"message": "Argument \"personalize\" has invalid value [\"sample1\", \"sample2\"].\nExpected type \"JSONString\", found [\"sample1\", \"sample2\"].",
"locations": [
{
"line": 2,
"column": 47
}
]
}
]
}
It's because you are sending and array of strings, rather than the JSONString object.这是因为您发送的是字符串数组,而不是 JSONString object。
JSON.stringify(Object that you are sending).
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.