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 { createHeadersWithClientAwareness , fetch , mergeHeaders , mergeHttpContext , prioritize } from './utils' ;
1010
1111export declare namespace HttpBatchLink {
1212 export type Options = {
@@ -61,6 +61,7 @@ export class HttpBatchLinkHandler extends ApolloLink {
6161 return new Observable ( ( observer : any ) => {
6262 const body = this . createBody ( operations ) ;
6363 const headers = this . createHeaders ( operations ) ;
64+ const context = this . createHttpContext ( operations ) ;
6465 const { method, uri, withCredentials } = this . createOptions ( operations ) ;
6566
6667 if ( typeof uri === 'function' ) {
@@ -74,6 +75,7 @@ export class HttpBatchLinkHandler extends ApolloLink {
7475 options : {
7576 withCredentials,
7677 headers,
78+ context
7779 } ,
7880 } ;
7981
@@ -162,6 +164,16 @@ export class HttpBatchLinkHandler extends ApolloLink {
162164 ) ;
163165 }
164166
167+ private createHttpContext ( operations : ApolloLink . Operation [ ] ) : HttpContext {
168+ return operations . reduce (
169+ ( context : HttpContext , operation : ApolloLink . Operation ) => {
170+ const { httpContext } = operation . getContext ( ) ;
171+ return httpContext ? mergeHttpContext ( httpContext , context ) : context ;
172+ } ,
173+ mergeHttpContext ( this . options . httpContext , new HttpContext ( ) ) ,
174+ )
175+ }
176+
165177 private createBatchKey ( operation : ApolloLink . Operation ) : string {
166178 const context : Context & { skipBatching ?: boolean } = operation . getContext ( ) ;
167179
0 commit comments