问题描述:使用ApolloClient请求时,设置了rejectUnauthorized: false
选项,但是ApolloClient不识别这个选项。
解决方法:
检查ApolloClient的版本,确保使用的是最新版本。有些旧版本的ApolloClient可能不支持rejectUnauthorized
选项。
如果ApolloClient的版本是最新的,但仍然无法识别rejectUnauthorized
选项,可以使用以下方法进行解决。
2.1 安装https
模块,执行以下命令:
npm install https
2.2 在ApolloClient的初始化代码中,引入https
模块,并使用https.Agent
来创建一个自定义的HTTPS代理。
const { ApolloClient, InMemoryCache, HttpLink } = require("@apollo/client");
const https = require("https");
const agent = new https.Agent({
rejectUnauthorized: false
});
const client = new ApolloClient({
link: new HttpLink({
uri: "https://example.com/graphql",
fetchOptions: {
agent
}
}),
cache: new InMemoryCache()
});
2.3 通过以上步骤,我们创建了一个自定义的HTTPS代理,并将其传递给ApolloClient的fetchOptions
选项中的agent
属性。这样就可以绕过rejectUnauthorized
的检查了。
2.4 使用上述代码示例替换原有的ApolloClient初始化代码,重新运行应用程序,问题应该就会解决了。
需要注意的是,rejectUnauthorized: false
选项会绕过SSL证书验证,可能存在安全风险,请在生产环境中谨慎使用。
上一篇:ApolloClient - 如何传递数组而不是字符串?
下一篇:ApolloClient v3 fetchMore with nested query results ApolloClient v3使用嵌套查询结果进行fetchMore