Apollo客户端在向服务器发送突变时返回“ 400(错误请求)错误”

时间:2018-09-09 19:17:33

标签: django vue.js graphql apollo graphene-python

我目前正在将vue-apollo软件包用于Apollo客户端,并将其VueJ堆栈与Django和graphene-python一起用于我的GraphQl API。

我在下面用vue-apollo进行了简单的设置:

import Vue from 'vue'
import { ApolloClient } from 'apollo-client'
import { HttpLink } from 'apollo-link-http'
import { InMemoryCache } from 'apollo-cache-inmemory'
import VueApollo from 'vue-apollo'
import Cookies from 'js-cookie'


const httpLink = new HttpLink({
  credentials: 'same-origin',
  uri: 'http://localhost:8000/api/',
})

// Create the apollo client
const apolloClient = new ApolloClient({
  link: httpLink,
  cache: new InMemoryCache(),
  connectToDevTools: true,
})

export const apolloProvider = new VueApollo({
  defaultClient: apolloClient,
})

// Install the vue plugin
Vue.use(VueApollo)

我还使用django-cors-headers软件包在Django settings.py上设置了CORS。当我将graphiQL或Insomnia API客户端用于chrome时,所有查询和突变都可以很好地解决,但是可以从vue应用尝试以下突变:

'''

import gql from "graphql-tag";
import CREATE_USER from "@/graphql/NewUser.gql";

export default {
  data() {
    return {
      test: ""
    };
  },
  methods: {
    authenticateUser() {
      this.$apollo.mutate({
        mutation: CREATE_USER,
        variables: {
          email: "test@example.com",
          password: "pa$$word",
          username: "testuser"
        }
      }).then(data => {
          console.log(result)
      })
    }
  }
};

NewUser.gql

mutation createUser($email: String!, $password: String!, $username: String!) {
  createUser (username: $name, password: $password, email: $email)
  user {
    id
    username
    email
    password
  }
}

返回以下错误响应:

POST http://localhost:8000/api/ 400 (Bad Request)

ApolloError.js?d4ec:37 Uncaught (in promise) Error: Network error: Response not successful: Received status code 400

但是,在我的Vue应用程序中,常规查询可以很好地解决除突变之外的正确响应,因此我真的感到困惑

4 个答案:

答案 0 :(得分:6)

400个错误通常意味着查询本身存在问题。在这种情况下,您已经定义(并且正在传递)名为$username的变量-但是,您的查询在第2行将其引用为$name

答案 1 :(得分:2)

如果正是您要发送的突变,请确保突变的格式不正确。您需要在突变中添加一个开括号

mutation createUser($email: String!, $password: String!, $username: String!) {
  createUser (username: $name, password: $password, email: $email) {
    user {
      id
      username
      email
      password
    }
  }
}

使用这些查询中的任何一个,当我遇到错误时,都会将其粘贴到graphiql或graphql游乐场中,以识别格式错误,以找出错误所在。

答案 2 :(得分:1)

除了graphiQL,我还要补充一点,apollo-link-error软件包也将有很大帮助。 通过导入其错误处理程序{onError},您可以通过控制台获取有关网络和应用程序(graphql)级别产生的错误的详细信息:

import { onError } from 'apollo-link-error';
import { ApolloLink } from 'apollo-link';

const errorLink = onError(({ graphQLErrors, networkError }) => {
  if (graphQLErrors) {
    console.log('graphQLErrors', graphQLErrors);
  }
  if (networkError) {
    console.log('networkError', networkError);
  }
});

const httpLink = ...

const link = ApolloLink.from([errorLink, httpLink]);

const client = new ApolloClient({
  ...,
  link,
  ...
});

通过在实例化Apollo客户端的位置添加此配置,您将获得与此错误类似的错误:

GraphQLError {消息:“语法错误:应为{,找到名称为“ createUser”“”}

更多信息可在Apollo Doc-错误处理:https://www.apollographql.com/docs/react/features/error-handling中找到。 希望对将来有帮助。

答案 3 :(得分:0)

对我来说,事实是我使用的是GraphQL模式中未定义的字段。始终要小心!