Apollo客户端和服务器中的自动持久查询无法正常工作,并出现PERSISTED_QUERY_NOT_FOUND错误

时间:2019-03-11 17:13:32

标签: apollo react-apollo apollo-client apollo-server apollostack

我试图在基于Graphql的系统中使用广为宣传的自动持久查询来提高性能,但是花了三天之后,我无法解决以下问题。如果您尝试查找,Apollo文档中有很多关于该主题的404页-

在客户端上

我用过

import { createPersistedQueryLink } from "apollo-link-persisted-queries";


const httpLink = createHttpLink({uri: API_URL })


const apqSwitch = createPersistedQueryLink({useGETForHashedQueries: true}).concat(httpLink);

  let links = [errorlink, stateLink, setSiteIdHeaderLink, apqLinkSwitch]

  const link = ApolloLink.from(links)

  const client = new ApolloClient({
    defaultOptions: {
      watchQuery: {
        errorPolicy: 'all'
      },
      query: {
        errorPolicy: 'all'
      },
      mutate: {
        errorPolicy: 'all'
      }
    },
    link,
    cache,
    connectToDevTools: true,
    credentials: 'include',
  })

  return { client, persistor }
}

客户端依赖项:

"apollo-boost": "^0.1.22",
"apollo-cache-inmemory": "^1.3.11",
"apollo-cache-persist": "^0.1.1",
"apollo-client": "^2.4.7",
"apollo-link": "^1.2.3",
"apollo-link-batch-http": "^1.2.8",
"apollo-link-http": "^1.5.9",
"apollo-link-persisted-queries": "^0.2.2",
"apollo-link-retry": "^2.2.5",
"apollo-link-schema": "^1.1.1",

在服务器上:

import { InMemoryLRUCache } from 'apollo-server-caching';
const { RedisCache } = require('apollo-server-cache-redis');

const server = new ApolloServer({
  ...root,
  resolverValidationOptions: {
    requireResolversForResolveType: false,
  },
  persistedQueries: {
    /*
    cache: new CustomRedis(),
    */
    cache: new RedisCache({
      host: 'localhost',
      port: xxxx,
    }),
  },
  formatError,
  formatResponse: (response, query) => formatResponse({ response, query }),
  dataSources
});

服务器依赖性:

"apollo-datasource-rest": "^0.3.2",
"apollo-errors": "^1.9.0",
"apollo-server-cache-redis": "^0.3.1",
"apollo-server-caching": "^0.3.1",
"apollo-server-express": "^2.4.8",
"cors": "^2.8.5",
"dataloader": "^1.4.0",
"express": "^4.16.3",
"glob": "^7.1.3",
"graphql": "^14.0.2",
"graphql-import": "^0.7.1",
"graphql-resolvers": "^0.2.2",

观察

  1. 从客户端看,sha1已被发送到服务器。

变量=%7B%7D&扩展名=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%22fd4c5f1ae3sfsf6ee0f0710b8d303a9703db7a6708b401278e7d7e7d6e7d7e6e7d7e7e6e7e6e7e6e7e6e6e7e6e6e7e7e7e人

  1. 服务器正在Redis缓存中寻找sha1,但找不到
  2. 服务器返回PersistedQueryNotFound”,“ MsgCode”:“ PERSISTED_QUERY_NOT_FOUND 错误给客户

在此之后什么都没有发生,redis缓存中没有执行设置操作,并不断出现PERSISTED_QUERY_NOT_FOUND错误

问题: APQ无法正常工作,不断出现PersistedQueryNotFound错误

1 个答案:

答案 0 :(得分:0)

我还配置了APQ,它们按预期工作。

您能在client端尝试下面的代码吗?服务器端与我几乎相同。

import { createPersistedQueryLink } from "apollo-link-persisted-queries";
import { createHttpLink } from "apollo-link-http";
import { InMemoryCache } from "apollo-cache-inmemory";
import ApolloClient from "apollo-client";

const link = createPersistedQueryLink({useGETForHashedQueries: true}).concat(createHttpLink({ uri: "/graphql" }));

const client = new ApolloClient({
  cache: new InMemoryCache(),
  link: link,
});

已记录here