I am using apollo-angular package to query backend using graphQL. Everything works fine until I add headers to the query.
I am adding custom header to get flattened data from the backend.
When I add header to the query I get data as undefined. Though I can see the response coming properly (flattened data) within the networks tab.
Here is my code :
constructor(private apollo: Apollo) { }
getPos(id: string, date: string) : Observable<any> {
return this.apollo
.watchQuery({
query: gql`
{
pos(id: 10001, date: "2017-02-01") {
id
quantity
price
security {
id
....
}
....
}
}`,
context: {
headers: new HttpHeaders().set("isFlatten", "true") // adding header
}
})
.valueChanges
.pipe(
tap(resp => console.log(resp.data)),
map(result => result.data['pos'])
);
}
Response data (flattened)
{
"data": [
{
"data.pos.security.__typename": "Security",
"data.pos.quantity": 14,
"data.pos.id": 3,
"data.pos.price": 740.6634841227037,
"data.pos.security.id": 296
.............
},
{
"data.pos.security.__typename": "Security",
"data.pos.quantity": 34,
"data.pos.id": 13,
"data.pos.price": 755,
"data.pos.security.id": 290
...........
}]
}
Note : Also If I remove the context attribute it does print the response properly (non-flattened data)