Django GraphQL端点测试无法使用“variables”字典

2024-10-06 10:34:39 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在django-graphql-jwt中使用variables? 我可以在iGraphQLvariables中正常使用mutation。那么它应该在我的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.')]

解决方法:
我在Python中使用.replace()为变量赋值,但我不喜欢这种粗俗的方式

问题:
是虫子吗


Tags: fromimportselfmodelreserrorvariablesgraphql