【发布时间】:2020-05-09 14:48:00
【问题描述】:
我正在使用 Vue cli 构建前端,并使用带有 Graphene 的 Django 构建后端。突变工作正常,但查询不行。
当我从 GraphiQL 运行相同的查询时,工作正常。
前端
- @vue/cli 4.1.2
- vue-apollo 3.0.2
后端
- python 3.8
- django 3.0.2
- 石墨烯-django 2.8.0
- django-graphql-jwt 0.3.0
queries.js
import gql from 'graphql-tag'
export const ME_QUERY = gql`
query me {
me {
username
is_active
}
}
`
Home.vue
<script>
import { ME_QUERY } from '@/graphql/queries'
export default {
name: 'home',
async mounted () {
await this.$apollo
.query({
query: ME_QUERY
})
.then((result) => {
console.log(result)
})
.catch(({ graphQLErrors }) => {
graphQLErrors.map(({ message, locations, path }) => console.log(`Error Message: ${message}, Location: ${locations}, Path: ${path}`))
})
}
}
</script>
schema.py
from django.contrib.auth import authenticate, login, get_user_model
import graphene
from graphene_django import DjangoObjectType
import graphql_jwt
from graphql_jwt.decorators import jwt_cookie
class UserType(DjangoObjectType):
class Meta:
model = get_user_model()
class ObtainJSONWebToken(graphql_jwt.JSONWebTokenMutation):
user = graphene.Field(UserType)
@classmethod
def resolve(cls, root, info, **kwargs):
return cls(user=info.context.user)
class Query(graphene.ObjectType):
me = graphene.Field(UserType)
def resolve_me(root, info):
user = info.context.user
if user.is_anonymous:
raise Exception('Authentication failure!!')
return user
class Mutation(graphene.ObjectType):
# token_auth = graphql_jwt.ObtainJSONWebToken.Field()
verify_token = graphql_jwt.Verify.Field()
refresh_token = graphql_jwt.Refresh.Field()
revoke_token = graphql_jwt.Revoke.Field()
log_in = ObtainJSONWebToken.Field()
schema = graphene.Schema(query=Query, mutation=Mutation)
感谢提前
【问题讨论】:
标签: django vue.js vue-apollo