我正在使用WP GraphQL
通过wordpress启用graphQL。我可以在wp-admin
内轻松地通过以下查询更新用户,但是当我尝试在nextjs
api路由内使用此查询时,它将失败,并显示以下内容:
// 20191230170624
// http://localhost:3000/api/users/update/role/cm9sZTpzdGFuZGFyZA==
{
"data": {
"graphQLErrors": [
{
"message": "You do not have the appropriate capabilities to perform this action",
"category": "user",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"updateUser"
]
}
],
"networkError": null,
"message": "GraphQL error: You do not have the appropriate capabilities to perform this action"
}
}
最终游戏实际上是通过条纹网钩使用这些终点之一,但我相信这是又一锅鱼。我正在阅读https://docs.wpgraphql.com/guides/authentication-and-authorization/#authentication-with-wpgraphql文档中的内容,但似乎无法设置标题,或者至少从未发送过标题。
这是我的apolloClient
设置:
const authLink = setContext((_, { headers }) => {
// get the authentication token from local storage if it exists
const token = Cookies.get('token')
// return the headers to the context so httpLink can read them
return {
headers: {
...headers,
authorization: `Bearer eyJ0eXAiOiJKV1QiLCJhbG.....iYE`,
},
}
})
const apolloClient = new ApolloClient({
link: ApolloLink.from([
onError(({ graphQLErrors, networkError }) => {
if (graphQLErrors)
graphQLErrors.forEach(({ message, locations, path }) => console.log(`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`))
if (networkError) console.log(`[Network error]: ${networkError}`)
}),
authLink.concat(
createHttpLink({
uri: graphQL,
credentials: 'same-origin',
fetch: fetch,
headers: {
authorization: `Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC9sb2NhbGhvc3QiLCJpYXQiOjE1Nzc3MzU1MzUsIm5iZiI6MTU3NzczNTUzNSwiZXhwIjoxNTc4MzQwMzM1LCJkYXRhIjp7InVzZXIiOnsiaWQiOiIxMTgifX19.mTU-kUpZ-bvpiNMhi_sPQa-xL-PRDHF3eFmwDcO4iYE`,
},
})
),
]),
cache: new InMemoryCache(),
})
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9,en-GB;q=0.8,fr;q=0.7
Cache-Control: no-cache
Connection: keep-alive
Cookie: analyticsCookieAccepted=1; ....... owse%26imgsize%3Dfull; wp-settings-time-45=1575018303; theme=light; w....mwDcO4iYE
Host: localhost:3000
Pragma: no-cache
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: none
Sec-Fetch-User: ?1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/77.0.3865.120 Safari/537.36