A bit more exotic example in Angular 5 shown below. Using HttpClient to send to the GraphQL server, read the response, and then retrieve the response header value and the response body value. In this case, the header is Total-Count. cars is a field (an array of cars) under other field data in the body. Also shown is the use of the first rxjs operator instead of subscribing.
import { HttpClient, HttpHeaders, HttpResponse } from '@angular/common/http'; import { first } from 'rxjs/operators/first'; import { Car, CarPage } from '../models/car'; .......... .......... public find(filter: string, sort: string, limit: number): Observable<CarPage> { let headers = new HttpHeaders().set("Content-Type", "application/graphql"); let carPage: CarPage = { cars: [], totalCount: 0 }; return this.http.post<HttpResponse<any>>('/graphql', `query cars { cars(filter: "${filter}", sort: "${sort}", limit: ${limit}) { id make model year } }`, { headers: headers, observe: "response" } ) .first((_, index) => index === 0, (response: HttpResponse<any>) => { let totalCountHeaderValues = response.headers.getAll("Total-Count"); carPage.totalCount = (totalCountHeaderValues.length > 0) ? parseInt(totalCountHeaderValues[0]) : 0; carPage.cars = response.body.data.cars; return carPage; }) }
source share