首页 > 解决方案 > 如何将graphql查询的结果作为文件发送?

问题描述

我正在使用 Apollo 服务器实现一个 graphQL 服务器。我想将查询的响应作为文本文件而不是 JSON 响应发送。谁能帮助我如何在 Apollo 服务器中做到这一点。我正在使用 NodeJS 进行服务器实现。

标签: graphqlgraphql-jsapollo-serverexpress-graphql

解决方案


如果您想使用 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


推荐阅读