我目前正在为 Apollo 客户端使用 vue-apollo 包,并为我的 GraphQl API 使用带有 django 和 graphene-python 的 VueJs 堆栈。
我在下面有一个简单的 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
上使用 django-cors-headers 包设置了 CORS。当我使用 chrome 的 graphiQL 或 Insomnia API 客户端时,所有查询和突变都可以很好地解决,但是从我的 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 应用程序中的常规查询可以很好地解决正确的响应,除了突变,所以这让我很困惑
原文由 Feyisayo Sonubi 发布,翻译遵循 CC BY-SA 4.0 许可协议
400 错误通常意味着查询本身有问题。在本例中,您已经定义(并且您正在传入)一个名为
$username
的变量——但是,您的查询在第 2 行将其引用为$name
。