node.js - 用于测试 APQ(自动持久查询)的示例 Apollo 客户端代码
问题描述
我试图用一个用haskell编写的服务器来测试APQ。以下是示例 Apollo 客户端代码,我写来测试它:
const { createPersistedQueryLink } = require("apollo-link-persisted-queries")
const { createHttpLink } = require("apollo-link-http")
const { InMemoryCache } = require("apollo-cache-inmemory")
const { ApolloClient } = require("apollo-client")
const { gql } = require('apollo-server');
const { ApolloLink } = require("apollo-link")
const fetch = require("node-fetch")
const link = ApolloLink.from([
createPersistedQueryLink(),
createHttpLink({
uri: "http://localhost:8080/v1/graphql",
fetch: fetch,
headers: {
"admin-secret":"password"
}
})
]);
const client = new ApolloClient({
cache: new InMemoryCache(),
link: link
})
async function main() {
const response = await client
.query({
query: gql`
query {
child {
name
}
}`
})
console.log(response.data)
}
main().catch(err => console.log("Err:", err))
但是每当我运行这个文件时,我都会收到以下错误:
graphQLErrors: [
{
extensions: [Object],
message: "the key 'query' was not present"
}
],
当我检查在 POST 正文中发送的请求正文时,我得到以下信息:
{"operationName":null,"variables":{},"extensions":{"persistedQuery":{"version":1,"sha256Hash":"0832c514aef4b1a6d84702e8b2fab452cbb0af61f0a1c4a4c30405e671d40527"}}}
它表明查询未在 Post Body 中发送。这可能是我收到上述错误的原因。
因此,我在这一点上感到困惑:see_no_evil:
我阅读了大量的博客,但不清楚当{ useGETForHashedQueries: true }
没有给出选项时使用什么 HTTP 方法。从我上面的实验来看,似乎使用了 POST 方法。
但是如果使用 POST 方法,为什么 POST 正文中没有发送查询。
但
当我使用该{ useGETForHashedQueries: true }
选项时,它可以正常工作。我在这里可能做错了什么?
如果有人能帮我解决这个问题,那就太好了。
解决方案
推荐阅读
- sql - 如何使用 LIKE 运算符精确匹配某些字符
- c# - 在 c# 中从日期时间中提取日期并使用数据表插件进行排序 - ASP.NET Core MVC C#
- docusignapi - DocuSign 封装的 COMPLETED 事件中包含的 pdfDocument 是最新版本吗?
- java - 我不断收到 NullPointerException 错误,我已经做了几个小时了,但找不到解决办法
- java - 编辑时的春季启动通知
- c# - Microsoft.Office.Interop.Excel - 如何将范围从一个单元格添加到另一个单元格?
- php - 在 Slim PHP 中处理 Stripe 错误
- ios - Xamarin iOS 16.5:证书是“不在钥匙串中”
- r - 尝试绘制曲面时 for 循环出错
- php - PHP session id cookie是如何加密的?