2016-12-16 213 views
-1

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

低于预期请求体格式:

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

你尝试过什么?查看关于中间件的文档,他们可以用任何你喜欢的方式修改请求:http://dev.apollodata.com/core/network.html#networkInterfaceMiddleware – stubailo

回答

0

对于我们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(); 
    }, 
}]); 
+0

如何在请求体中添加一个额外的变量? – PritiID

+0

它是一个你想添加到响应的常量值吗?如果不是,新变量依赖于哪些参数? – marktani

+0

是否有可能向中间件注入服务? –