我尝试在基于 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",
观察:
- 从客户端我看到 sha1 正在发送到服务器。
变量=%7B%7D&extensions=%7B%22persistedQuery%22%3A%7B%22version%22%3A1%2C%22sha256Hash%22%3A%22fd4c5f1ae3sfsf6ee0f0710b8d303a9703db7a6708b401278e2fd664f56f4e9176%7176
- 服务器正在 redis 缓存中寻找 sha1 但找不到
- 服务器返回 PersistedQueryNotFound","MsgCode":"PERSISTED_QUERY_NOT_FOUND 错误给客户端
之后什么都没有发生,redis缓存中没有执行设置操作并不断收到PERSISTED_QUERY_NOT_FOUND错误
问题: APQ 不工作,不断收到 PersistedQueryNotFound 错误