作者:想翻身的咸鱼。。。 | 来源:互联网 | 2022-12-05 12:49
我目前正在为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)
我还settings.py
使用django-cors-headers软件包在Django 上设置了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应用中的常规查询可以很好地解决正确的响应(变异除外),因此这让我感到困惑
1> Daniel Reard..:
通常有400个错误表示查询本身存在问题。在这种情况下,您已经定义(并且正在传递)一个名为的变量$username
-但是,您的查询$name
在第2行中引用了该变量。