在Keystone 5中将数据源添加到Apollo配置选项时,缓存问题5
当第二次执行对服务器的请求时,即使第一个请求突变了实体,返回的结果始终是缓存的结果。
这是导入每个dataSource的两个实例的dataSources.js
文件:
- legacyuserapi = new legacyuserapi();
- LegacyMailapi = New LegacyMailapi();
import legacyUserApi from './LegacyUserApi';
import legacyMailApi from './LegacyMailApi';
export default () => ({
legacyUserApi,
legacyMailApi
});
在keystone.js
文件中,我导入它:
import dataSources from './dataSources';
const apps = [
new GraphQLApp({
apiPath: API_PATH,
apollo: {
dataSources,
introspection: isDev
}
})
]
When executing a request to the server for the second time, the result that returns is always the cached one, even if the first request mutated the entity.
here is the dataSources.js
file that imports two instances of each class of dataSource:
- legacyUserApi = new LegacyUserApi();
- legacyMailApi = new LegacyMailApi();
import legacyUserApi from './LegacyUserApi';
import legacyMailApi from './LegacyMailApi';
export default () => ({
legacyUserApi,
legacyMailApi
});
and in the keystone.js
file I import it:
import dataSources from './dataSources';
const apps = [
new GraphQLApp({
apiPath: API_PATH,
apollo: {
dataSources,
introspection: isDev
}
})
]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Keystone JS使用
Apollo Server
因此,我已经查看了他们的文档:dataSources.js
文件应导入类而不是实例,以便Apollo Server在每个请求中都会创建一个新实例。Keystone js uses
Apollo Server
so I've looked at their documentation:so
dataSources.js
file should import the class and not the instance so that Apollo Server will create a new instance with every request.有可以使用许多方法您的问题并不表示您正在使用的问题。最终,他们中的大多数设置a 控制标题在HTTP响应上,让我们假设在这里就是这样。
该标头通过浏览器和服务器之间的任何共享缓存来解释此标头,例如CDN或代理。缓存后,服务器通常没有任何使存储副本无效的机制。可以通过为最初被缓存的响应设置正确的标头指令来减轻这一点。例如,包括
必备
指令或设置简短的max-age
。另外,设置
私有
指令将仅允许在浏览器中缓存请求。然后,随后的HTTP请求可以指定max-age = 0,no-cache
以强制再次获取数据(例如,在进行突变之后)。这可能使您可以为单个用户解决问题,但是显然,您会失去共享缓存的好处。在不了解您的缓存配置,基础架构和所提出的请求的情况下,很难提供具体建议。也许您可以使用Curl复制来更新问题?这将使您将服务器行为与浏览器的行为隔离。
There are many ways caching can be used on top of Apollo Server but your question doesn't indicate which you're using. Ultimately, most of them set a
Cache-Control
header on the HTTP response so let's assume that's the case here.This header is interpreted by both the browser and any shared caches that sit between it and the server, such as a CDN or proxy. Once a cached, the server generally doesn't have any mechanism to invalidate the stored copy. This can be mitigated by setting the correct header directives for the response that will initially be cached. For example, including the
must-revalidate
directive or setting a shortmax-age
.Alternatively, setting the
private
directive will only allow the request to be cached in the browser. Subsequent HTTP requests could then specifymax-age=0, no-cache
to force the data to be fetched again (for example, after a mutation was made). This may allow you to solve your problem for individual users but, obviously, you loose the benefits of a shared cache.It's difficult to give specific advice without knowing more about your caching configuration, infrastructure and the requests being made. Maybe you can update your question with a reproduction using curl? That'll let you isolate the server behaviour from that of the browser.