标签: django-graphql-jwt

Graphene-Django 和多对多关系查找

我有两个 Django 模型——老师和学生,并且有多对多的关系。老师可以有多个学生,学生也可以有多个老师。有一种称为“备注”的“通过”模型,教师可以在其中将学生标记为最喜欢的。

我是 GraphQL 的新手。我正在尝试实现两个查询:
1. 老师和他们所有的学生
2. 老师和他们最喜欢的学生

我在实施第二个查询时遇到了困难,并且一直无法这样做。

模型.py

SUBJECTS = (
    ("Maths", "Maths"),
    ("Chemistry", "Chemistry"),
    ("Physics", "Physics")
)

class Student(models.Model):
    name = models.CharField(max_length=100)
    age = models.IntegerField(default=0)

    def __str__(self):
        return self.name

class Teacher(models.Model):
    name = models.CharField(max_length=100)
    subject = models.CharField(max_length=100, choices=SUBJECTS)
    students = models.ManyToManyField(Student, through="Remarks")

    def __str__(self):
        return self.name

class Remarks(models.Model):
    student = models.ForeignKey(Student, related_name="student", on_delete=models.DO_NOTHING)
    teacher = models.ForeignKey(Teacher, related_name="teacher", on_delete=models.DO_NOTHING)
    favorite = models.BooleanField(default=False, choices=(
        (True, "Yes"),
        (False, "No")
    ))

Run Code Online (Sandbox Code Playgroud)

模式.py

import graphene
from graphene import relay, ObjectType …
Run Code Online (Sandbox Code Playgroud)

django graphql graphene-python graphene-django django-graphql-jwt

8
推荐指数
1
解决办法
1209
查看次数

Django GraphQL JWT:tokenAuth 突变返回“str 对象没有属性解码”

目前我正在从文档页面运行 django-graphqljwt 的基本示例。https://django-graphql-jwt.domake.io/en/latest/quickstart.html

import graphene
import graphql_jwt


class Mutation(graphene.ObjectType):
    token_auth = graphql_jwt.ObtainJSONWebToken.Field()
    verify_token = graphql_jwt.Verify.Field()
    refresh_token = graphql_jwt.Refresh.Field()


schema = graphene.Schema(mutation=Mutation)
Run Code Online (Sandbox Code Playgroud)

但是,如果我运行tokenAuth突变,它会在GraphiQL界面中抛出以下错误。请注意,如果我输入不正确的凭据,它会抛出一个“ Please enter valid credentials”而不是下面的。

{
  "errors": [
    {
      "message": "'str' object has no attribute 'decode'",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "tokenAuth"
      ]
    }
  ],
  "data": {
    "tokenAuth": null
  }
}
Run Code Online (Sandbox Code Playgroud)

django graphql graphene-django django-graphql-jwt

6
推荐指数
2
解决办法
2041
查看次数

如何在 graphene-django GraphQLTestCase 中使用 django-grahql-jwt 进行身份验证?

我正在尝试根据 graphene django 文档测试我的突变。该突变与@login_required装饰器一起使用,但存在一个问题,因为任何登录测试方法都不起作用。我尝试过self.client.loginself.client.force_login. 我什至做了一个 tokenAuth 突变,并在那里硬编码了一些凭据,但它也不起作用;用户仍然是匿名的。

def test_create_member_mutation(self):
    response = self.query(
        '''
        mutation createMember($firstName: String) {
            createMember(firstName: $firstName) {
                member {
                    id
                }
            }
        }
        ''',
        op_name='createMember',
        variables={'firstName': 'Foo'}
    )

    self.assertResponseNoErrors(response)
Run Code Online (Sandbox Code Playgroud)

python django graphql graphene-django django-graphql-jwt

2
推荐指数
1
解决办法
1836
查看次数

模块“jwt”没有属性“ExpiredSignature”

我一直在开发一个使用 graphene/graphql 的 Django 应用程序,该应用程序使用 Docker alpine 镜像在 AWS 上运行。我一直在使用 django-grapql-jwt-0.3.1 模块在我的应用程序中调用 jwt-2.0.0 身份验证。它从 PyJWT 调用 ExpiredSignature 而不是 ExpiredSignatureError。graphQL 返回“模块‘jwt’没有属性‘ExpiredSignature’”错误。如何解决问题?

在此输入图像描述

django jwt pyjwt graphene-django django-graphql-jwt

2
推荐指数
1
解决办法
5808
查看次数