首页 > 解决方案 > 用于测试 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 }选项时,它可以正常工作。我在这里可能做错了什么?

如果有人能帮我解决这个问题,那就太好了。

标签: node.jsgraphqlapolloapollo-clientapollo-server

解决方案


推荐阅读