如何将graphql查询的结果作为文件发送?

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

cghchong 回答:如何将graphql查询的结果作为文件发送?

如果您想使用Apollo Server而不是原始的graphql库,请尝试将插件添加到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

,

使用app Express js发送文件:

ngOnInit() {    
  this.studentGrid();    
}

studentGrid() {
  this.gridConfig = this.getGridConfig();
  $('#table-responsive').dpGrid(this.gridConfig);
}

testClick() {
  console.log('testClick');
}

并将您的graphql查询结果写入yourFile.txt

app.get('/URL',(req,res) => res.download('./yourFile.txt'))
本文链接:https://www.f2er.com/3038574.html

大家都在问