首页 > 解决方案 > 不能在石墨烯测试中使用字符串化 JSON

问题描述

我正在尝试编写一个测试,该测试为我的一个字段(questionData)接收一个字符串化的 json 值,但它失败了:

我这样写了突变:

        query = '''mutation {
            createScorecard(
                name: "Test Scorecard",
                creatorId: 395549257913,
                questionData: "[{\"title\":\"This is a test question\",\"description\":\"Test question description\",\"answers\":[{\"text\":\"Yes\",\"point\":50,\"autofail\":true},{\"text\":\"No\",\"point\":50,\"autofail\":false}]}]"
            ) {
                scorecard {
                    name
                }
                error
            }
        }'''

但我不断收到此错误:

{'errors': [{'message': '语法错误 GraphQL (5:40) 预期 :, 找到字符串 ":"\n\n4: creatorId: 395549257913,\n5: questionData: "[{"title":"这是一道试题","description":"试题说明","answers":[{"text":"Yes","point":50,"autofail":true},{"text":"否","point":50,"autofail":false}]}]"\n ^\n6: ) {\n', 'locations': [{'line': 5, 'column': 40}] }]}

标签: pythongraphqlgraphene-pythongraphene2

解决方案


转义字符串时需要使用双反斜杠

https://github.com/graphql-python/graphene/issues/521


推荐阅读