简体   繁体   English

在使用 django 的 graphql 突变中传递 json 字段时出错

[英]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.

相关问题 突变错误,类型为“ Mutation \\”的字段“ createProduct”上的未知参数“ barcode”-django - mutation error, Unknown argument \“barcode\” on field \“createProduct\” of type \“Mutation\” - django 如何创建 graphql 突变与 Django 中的关系 - How to create a graphql mutation with a relation in Django 使用带有石墨烯的 django-graphql-auth 自定义突变的响应 - Customize a response from a mutation using django-graphql-auth with graphene 如何处理 graphql django 更新突变中的多条记录 - how to handle multiple records in graphql django update mutation 将带有url的值传递给Django视图时出错 - Error while passing value with url to django view django-graphql-jwt 与 django-phone-field Object 类型 PhoneNumber 不是 JSON 可序列化 - django-graphql-jwt with django-phone-field Object of type PhoneNumber is not JSON serializable 从我的 python 代码中进行 graphQL 突变,得到错误 - Making a graphQL mutation from my python code, getting error 在GraphQL突变中设置cookie - Set cookie in GraphQL mutation 将字符字段更改为 Django 中的数组字段时出错 - Error while changing a Char field to Array Field in Django 在视图 django 中通过 url 传递参数时找不到页面错误 - page not found error while passing paarmeter through url in view django
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM