【问题标题】:Vue-apollo. Queries don't work return 400 Bad RequestVue-阿波罗。查询不起作用返回 400 Bad Request
【发布时间】: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


    【解决方案1】:

    在Firefox中安装扩展Graphql后发现我的错误。

    {
      "errors": [
        {
          "message": "Cannot query field \"is_active\" on type \"UserType\". Did you mean \"isActive\"?",
          "locations": [
            {
              "line": 4,
              "column": 5
            }
          ]
        }
      ]
    }
    

    更改为isActive 查询工作正常。

    【讨论】:

      猜你喜欢
      • 2018-04-26
      • 2017-12-11
      • 2021-12-21
      • 2020-09-26
      • 2020-11-17
      • 2020-07-26
      • 1970-01-01
      • 1970-01-01
      • 2020-11-01
      相关资源
      最近更新 更多