我目前正在使用Apollo客户端的vue-apollo包,其中包含VueJs堆栈和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 settings.py
上安装了CORS和django-cors-headers软件包.当我使用graphiQL或用于chrome的失眠API客户端时,所有的查询和Mutations 都能很好地解决,但在我的vue应用程序中try 以下Mutations :
'''
import gql from "graphql-tag";
import CREATE_USER from "@/graphql/新用户.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应用程序中,常规查询可以很好地解决正确的响应,除了Mutations ,所以这让我非常困惑