vuejs2 - 如何解决访问 DatoCMS 的 Apollo 错误 401
问题描述
我遵循了本指南:
但是当我运行时npm run dev
,我的 localhost:3000 上出现错误 401
这是我的 nuxt.config.js
module.exports = {
/*
** Headers of the page
*/
head: {
title: 'hello-world',
meta: [
{ charset: 'utf-8' },
{ name: 'viewport', content: 'width=device-width, initial-scale=1' },
{ hid: 'description', name: 'description', content: 'Nuxt.js project' }
],
link: [
{ rel: 'icon', type: 'image/x-icon', href: '/favicon.ico' }
]
},
/*
** Customize the progress bar color
*/
loading: { color: '#3B8070' },
/*
** Build configuration
*/
build: {
/*
** Run ESLint on save
*/
extend (config, { isDev, isClient }) {
if (isDev && isClient) {
config.module.rules.push({
enforce: 'pre',
test: /\.(js|vue)$/,
loader: 'eslint-loader',
exclude: /(node_modules)/
})
}
}
},
modules: [
'@nuxtjs/apollo',
],
apollo: {
clientConfigs: {
default: {
httpEndpoint: 'https://graphql.datocms.com',
getAuth: () => 'Bearer XXXXXXXXXXXXX' //my apikey
}
}
}
}
这是执行查询的 vue 文件。我目前在页面中没有显示任何内容,但我仍然收到错误消息。
<template>
<div>
All blog posts
</div>
</template>
<script>
import gql from 'graphql-tag'
export default {
apollo: {
allPosts: gql`{
allPosts{
title,
text,
slug
}
}`
}
}
</script>
在 DatoCMS 中正确定义了 post 数据类型,我使用 API Explorer 对其进行了测试
解决方案
在文章的评论中找到了解决方案。您需要将您的身份验证放在一个单独的文件中,如下所示:
~/apollo/config.js
export default function(context){
return {
httpEndpoint: ‘https://graphql.datocms.com',
getAuth:() => ‘my-token’ //Bearer is added by default
}
}
然后在 nuxt.config.js apollo 中:
apollo: {
clientConfigs: {
default: ‘~/apollo/config.js’
}
}
推荐阅读
- html - 如何使用 flexbox 将三列布局变成两列布局(调整选项卡大小时)?
- ios - 音频检测器可以在设备上工作,但不能在模拟器上工作......和准确性
- python - 在哪里可以找到 Twilio 的运营商查找返回的运营商字符串列表?
- angular - 如何改进:两个总和给定一个整数数组,返回两个数字的索引......使用角度
- c++ - 为什么在 BST 插入程序中使用 void 不起作用
- c# - 为什么我的 azure webjob 没有记录到存储 blob?
- javascript - 有什么办法可以在一个表单中使用多个输入?
- python - 当用户单击 tkinter 中的错误消息框时聚焦空输入框
- angular - 使用 angular-oauth2-oidc 实现 OAuth2 代码流
- javascript - 无法将 mysql 返回的对象推送到数组中