【问题标题】:Apollo-client custom Headers and additional variables in Angular2Angular2 中的 Apollo 客户端自定义标头和附加变量
【发布时间】:2017-05-01 20:33:15
【问题描述】:

如何使用 Angular2 使用 npm apollo-client 添加自定义标头并在请求正文中引入其他自定义变量。我希望在我的 graphQL 查询中引入一个额外的自定义变量,例如请求正文中的默认“变量”对象以及自定义标头。我尝试使用 createNetworkInterface 的 applyMiddleware。

以下是预期的请求正文格式:

{
   "query":"",
   "variables":"{}",
   "customVariable":"{}", //This is additional which wants to be introduced
   "operationName":""
}

【问题讨论】:

标签: angular typescript xmlhttprequest graphql apollo-server


【解决方案1】:

对于我们的Angular 2 examples 之一,我们正在添加一个自定义标题,如下所示:

networkInterface.use([{
  applyMiddleware (req, next) {
    if (!req.options.headers) {
      // Create the header object if needed.
      req.options.headers = {};
    }
    req.options.headers['x-graphcool-source'] = 'example:angular-apollo-instagram';
    next();
  },
}]);

【讨论】:

  • 如何在请求正文中添加附加变量??
  • 它是一个你想添加到响应中的常量值吗?如果不是,新变量依赖什么参数?
  • 中间件可以注入服务吗?
猜你喜欢
  • 2018-07-11
  • 2022-11-04
  • 2019-08-09
  • 2017-01-19
  • 1970-01-01
  • 2021-02-09
  • 2020-06-29
  • 2017-08-31
  • 2018-02-14
相关资源
最近更新 更多