我有无法解决的问题。我检查了代码中的所有内容,它似乎可以正常工作,但是当我运行服务器并转到Insomnia或localhost:8000 / graphql并键入投票查询时,似乎根本不存在,但是在代码中却有该查询。 它是一个简单的厨师应用程序,您可以与通过JWT令牌进行身份验证的用户进行投票。没有那么多代码,使我无法了解所有我制作的模型和模式,并且在使用graphql失眠时仍然会出现此错误。
links / models.py
from django.db import models
from django.conf import settings
class Link(models.Model):
url = models.URLField()
description = models.TextField()
posted_by = models.ForeignKey(settings.AUTH_USER_MODEL, null=True, on_delete=models.CASCADE)
class Vote(models.Model):
user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
link = models.ForeignKey('links.Link', related_name='votes', on_delete=models.CASCADE)
links / schema.py(linkapp)
import graphene
from graphene_django import DjangoObjectType
from .models import Link, Vote
from users.schema import UserType
class LinkType(DjangoObjectType):
class Meta:
model = Link
class VoteType(DjangoObjectType):
class Meta:
model = Vote
class Query(graphene.ObjectType):
links = graphene.List(LinkType)
votes = graphene.List(VoteType)
def resolve_links(self, info, **kwargs):
return Link.objects.all()
def resolve_votes(self, info, **kwargs):
return Votes.objects.all()
class CreateLink(graphene.Mutation):
id = graphene.Int()
url = graphene.String()
description = graphene.String()
posted_by = graphene.Field(UserType)
class Arguments:
url = graphene.String()
description = graphene.String()
def mutate(self, info, url, description):
user = info.context.user or None
link = Link(
url=url,
description=description,
posted_by=user,
)
link.save()
return CreateLink(
id=link.id,
url=link.url,
description=link.description,
posted_by=link.posted_by,
)
# Add the CreateVote mutation
class CreateVote(graphene.Mutation):
user = graphene.Field(UserType)
link = graphene.Field(LinkType)
class Arguments:
link_id = graphene.Int()
def mutate(self, info, link_id):
user = info.context.user
if user.is_anonymous:
raise Exception('You must be logged in to vote')
link = Link.objects.filter(id=link_id).first()
if not link:
raise Exception('Invalid Link')
Vote.objects.create(
user=user,
link=link,
)
return CreateVote(user=user, link=link)
# Add the mutation to the Mutation class
class Mutation(graphene.ObjectType):
create_link = CreateLink.Field()
create_vote = CreateVote.Field()
users / schema.py
from django.contrib.auth import get_user_model
import graphene
from graphene_django import DjangoObjectType
class UserType(DjangoObjectType):
class Meta:
model = get_user_model()
class CreateUser(graphene.Mutation):
user = graphene.Field(UserType)
class Arguments:
username = graphene.String(required=True)
password = graphene.String(required=True)
email = graphene.String(required=True)
def mutate(self, info, username, password, email):
user = get_user_model()(
username=username,
email=email,
)
user.set_password(password)
user.save()
return CreateUser(user=user)
class Mutation(graphene.ObjectType):
create_user = CreateUser.Field()
# queries
class Query(graphene.AbstractType):
me = graphene.Field(UserType)
users = graphene.List(UserType)
def resolve_me(self, info):
user = info.context.user
if user.is_anonymous:
raise Exception('Not logged in!')
return user
def resolve_users(self, info):
return get_user_model().objects.all()
在名为testmodel的主文件中是settings.py和url.py,我也拥有schema.py来注册所有突变和查询。
schema.py
import graphene
import graphql_jwt
import links.schema
import users.schema
class Query(links.schema.Query, users.schema.Query, graphene.ObjectType):
pass
class Mutation(links.schema.Mutation, users.schema.Mutation, graphene.ObjectType):
token_auth = graphql_jwt.ObtainJSONWebToken.Field()
verify_token = graphql_jwt.Verify.Field()
refresh_token = graphql_jwt.Refresh.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
那是我拥有的所有代码,我无法查询投票。我在上面向您显示的打印屏幕中收到该错误。我无法查询“查询”类型的字段投票。
我想念谁?