首页 > 解决方案 > Django GraphQL 端点测试无法使用“变量”字典

问题描述

怎么variablesdjango-graphql-jwt?我可以mutationiGraphQLwith中variables正常使用。然后它应该在我的TestCase

from django.contrib.auth import get_user_model
from graphql_jwt.testcases import JSONWebTokenTestCase
from model_mommy import mommy

from multy_herr.commons.tests import JCMixin
from multy_herr.objections.models import Objection
from multy_herr.tweets.models import Tweet

User = get_user_model()


class UsersTest(JCMixin, JSONWebTokenTestCase):

    def setUp(self) -> None:
        super().setUp()
        mommy.make(Objection, _quantity=3)

    def test_authorized_user_hide_success(self):
        """
        Hide success
        :return:
        """
        tweet = Tweet.objects.first()

        query: str = '''
            mutation{
              objection(input: {
                tweet: $tweetId
                hidden: $hidden
              }){
                id
                hidden
                report
                tweet
                errors{
                  field
                  messages
                }
              }
            }
        '''
        variables = {
            'tweetId': str(tweet.id),
            'hidden': True,
        }
        self.client.authenticate(self.jc)
        res = self.client.execute(query, variables)

这是res.error

res.errors
Out[3]: 
[graphql.error.base.GraphQLError('Variable "$tweetId" is not defined.'),
 graphql.error.base.GraphQLError('Variable "$hidden" is not defined.')]

解决方法:
我使用.replace()inPython为我的变量赋值,但我不喜欢那种骇人听闻的方式

问:
是bug吗?

标签: pythondjangotestinggraphqljwt

解决方案


推荐阅读