graphql - 如何将graphql查询的结果作为文件发送?
问题描述
我正在使用 Apollo 服务器实现一个 graphQL 服务器。我想将查询的响应作为文本文件而不是 JSON 响应发送。谁能帮助我如何在 Apollo 服务器中做到这一点。我正在使用 NodeJS 进行服务器实现。
解决方案
如果您想使用 Apollo Server 而不是 vanillagraphql
库,请尝试在plugins
配置中添加一个插件:
const server = new ApolloServer({
typeDefs,
resolvers,
// You can import plugins or define them in-line, as shown:
plugins: [
{
requestWillStart(reqCtx) {
return {
willSendResponse(ctx) {
ctx.response.http.headers.set('Content-Disposition', 'attachment; filename=result.json');
}
}
}
}
],
})
参考:https ://www.apollographql.com/docs/apollo-server/integrations/plugins/#willsendresponse
推荐阅读
- vba - 向个人 PowerPoint 插件添加附加行
- swift - Swift中的多个自变量/多元线性回归
- c# - 如何将多个参数传递给 sqlite IN 子句并防止 SQL 注入?
- python - 会话和并行性在 TF2.0 中如何工作?
- javascript - 我正在尝试使用两个按钮计算按钮点击的总数并将其显示在文本区域中
- javascript - 将对象的值数字转换为字符串无法正常工作
- python - 填充缺失值的数据框(熊猫)
- postgresql - 让 LIKE 搜索使用 citext 表达式的索引
- excel - 删除 A 列或 B 列中的单元格为空的行
- go - 使用给定数据创建一个 JSON 数据作为 map[string] 接口