11import { print } from 'graphql' ;
22import { Observable } from 'rxjs' ;
3- import { HttpClient , HttpHeaders } from '@angular/common/http' ;
3+ import { HttpClient , HttpContext , HttpHeaders } from '@angular/common/http' ;
44import { Injectable } from '@angular/core' ;
55import { ApolloLink } from '@apollo/client' ;
66import { BatchLink } from '@apollo/client/link/batch' ;
77import type { HttpLink } from './http-link' ;
88import { Body , Context , OperationPrinter , Request } from './types' ;
9- import { createHeadersWithClientAwareness , fetch , mergeHeaders , prioritize } from './utils' ;
9+ import {
10+ createHeadersWithClientAwareness ,
11+ fetch ,
12+ mergeHeaders ,
13+ mergeHttpContext ,
14+ prioritize ,
15+ } from './utils' ;
1016
1117export declare namespace HttpBatchLink {
1218 export type Options = {
@@ -61,6 +67,7 @@ export class HttpBatchLinkHandler extends ApolloLink {
6167 return new Observable ( ( observer : any ) => {
6268 const body = this . createBody ( operations ) ;
6369 const headers = this . createHeaders ( operations ) ;
70+ const context = this . createHttpContext ( operations ) ;
6471 const { method, uri, withCredentials } = this . createOptions ( operations ) ;
6572
6673 if ( typeof uri === 'function' ) {
@@ -74,6 +81,7 @@ export class HttpBatchLinkHandler extends ApolloLink {
7481 options : {
7582 withCredentials,
7683 headers,
84+ context,
7785 } ,
7886 } ;
7987
@@ -162,6 +170,16 @@ export class HttpBatchLinkHandler extends ApolloLink {
162170 ) ;
163171 }
164172
173+ private createHttpContext ( operations : ApolloLink . Operation [ ] ) : HttpContext {
174+ return operations . reduce (
175+ ( context : HttpContext , operation : ApolloLink . Operation ) => {
176+ const { httpContext } = operation . getContext ( ) ;
177+ return httpContext ? mergeHttpContext ( httpContext , context ) : context ;
178+ } ,
179+ mergeHttpContext ( this . options . httpContext , new HttpContext ( ) ) ,
180+ ) ;
181+ }
182+
165183 private createBatchKey ( operation : ApolloLink . Operation ) : string {
166184 const context : Context & { skipBatching ?: boolean } = operation . getContext ( ) ;
167185
0 commit comments