繁体   English   中英

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

[英]Apollo-client returns "400 (Bad Request) Error" on sending mutation to server

我目前正在为 Apollo 客户端使用vue-apollo package,为我的 GraphQl API 使用 VueJs 堆栈和 django 和 graphene-python。

我在下面使用 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 settings.py上设置了 CORS 和django-cors-headers package。当我使用 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)
      })
    }
  }
};

新用户.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 应用程序中的常规查询可以很好地解决正确的响应,但突变除外,所以这让我感到非常困惑

400 错误通常意味着查询本身存在问题。 在本例中,您已经定义(并且正在传入)一个名为$username的变量——但是,您的查询在第 2 行将其引用为$name

除了 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 希望它在未来有所帮助。

如果这正是您发送的内容,请确保突变的格式不正确。 您需要在突变中使用左括号

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

对于这些查询中的任何一个,当我遇到错误时,我将其粘贴到 graphiql 或 graphql 操场中以识别格式错误是什么,以便隔离什么是错误的。

对我来说,事实上我使用了一个未在 GraphQL 模式中定义的字段。 时刻小心!

对于后端使用 laravel 的人,这帮助我解决了问题

在 laravel 项目中找到文件config/cors.php并将行'paths' => ['api/*', 'sanctum/csrf-cookie'],更改为'paths' => ['api/*', 'graphql', 'sanctum/csrf-cookie'],

同样在你的 vue 应用程序中,确保你没有在apollo config中使用no-cors模式

问候

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM