11import { print } from 'graphql' ;
2+ import { Observable } from 'rxjs' ;
23import { HttpClient , HttpHeaders } from '@angular/common/http' ;
34import { Injectable } from '@angular/core' ;
4- import {
5- ApolloLink ,
6- FetchResult ,
7- Observable as LinkObservable ,
8- Operation ,
9- } from '@apollo/client/core' ;
10- import { BatchHandler , BatchLink } from '@apollo/client/link/batch' ;
11- import { BatchOptions , Body , Context , OperationPrinter , Options , Request } from './types' ;
5+ import { ApolloLink } from '@apollo/client' ;
6+ import { BatchLink } from '@apollo/client/link/batch' ;
7+ import type { HttpLink } from './http-link' ;
8+ import { Body , Context , OperationPrinter , Request } from './types' ;
129import { createHeadersWithClientAwareness , fetch , mergeHeaders , prioritize } from './utils' ;
1310
11+ export declare namespace HttpBatchLink {
12+ export type Options = {
13+ batchMax ?: number ;
14+ batchInterval ?: number ;
15+ batchKey ?: ( operation : ApolloLink . Operation ) => string ;
16+ } & HttpLink . Options ;
17+ }
18+
1419export const defaults = {
1520 batchInterval : 10 ,
1621 batchMax : 10 ,
@@ -27,9 +32,9 @@ export const defaults = {
2732 */
2833export function pick < K extends keyof Omit < typeof defaults , 'batchInterval' | 'batchMax' > > (
2934 context : Context ,
30- options : Options ,
35+ options : HttpBatchLink . Options ,
3136 key : K ,
32- ) : ReturnType < typeof prioritize < Context [ K ] | Options [ K ] | ( typeof defaults ) [ K ] > > {
37+ ) : ReturnType < typeof prioritize < Context [ K ] | HttpBatchLink . Options [ K ] | ( typeof defaults ) [ K ] > > {
3338 return prioritize ( context [ key ] , options [ key ] , defaults [ key ] ) ;
3439}
3540
@@ -41,7 +46,7 @@ export class HttpBatchLinkHandler extends ApolloLink {
4146
4247 constructor (
4348 private readonly httpClient : HttpClient ,
44- private readonly options : BatchOptions ,
49+ private readonly options : HttpBatchLink . Options ,
4550 ) {
4651 super ( ) ;
4752
@@ -52,8 +57,8 @@ export class HttpBatchLinkHandler extends ApolloLink {
5257 this . print = this . options . operationPrinter ;
5358 }
5459
55- const batchHandler : BatchHandler = ( operations : Operation [ ] ) => {
56- return new LinkObservable ( ( observer : any ) => {
60+ const batchHandler : BatchLink . BatchHandler = ( operations : ApolloLink . Operation [ ] ) => {
61+ return new Observable ( ( observer : any ) => {
5762 const body = this . createBody ( operations ) ;
5863 const headers = this . createHeaders ( operations ) ;
5964 const { method, uri, withCredentials } = this . createOptions ( operations ) ;
@@ -90,7 +95,7 @@ export class HttpBatchLinkHandler extends ApolloLink {
9095
9196 const batchKey =
9297 options . batchKey ||
93- ( ( operation : Operation ) => {
98+ ( ( operation : ApolloLink . Operation ) => {
9499 return this . createBatchKey ( operation ) ;
95100 } ) ;
96101
@@ -103,8 +108,8 @@ export class HttpBatchLinkHandler extends ApolloLink {
103108 }
104109
105110 private createOptions (
106- operations : Operation [ ] ,
107- ) : Required < Pick < Options , 'method' | 'uri' | 'withCredentials' > > {
111+ operations : ApolloLink . Operation [ ] ,
112+ ) : Required < Pick < HttpBatchLink . Options , 'method' | 'uri' | 'withCredentials' > > {
108113 const context : Context = operations [ 0 ] . getContext ( ) ;
109114
110115 return {
@@ -114,7 +119,7 @@ export class HttpBatchLinkHandler extends ApolloLink {
114119 } ;
115120 }
116121
117- private createBody ( operations : Operation [ ] ) : Body [ ] {
122+ private createBody ( operations : ApolloLink . Operation [ ] ) : Body [ ] {
118123 return operations . map ( operation => {
119124 const includeExtensions = prioritize (
120125 operation . getContext ( ) . includeExtensions ,
@@ -144,10 +149,11 @@ export class HttpBatchLinkHandler extends ApolloLink {
144149 } ) ;
145150 }
146151
147- private createHeaders ( operations : Operation [ ] ) : HttpHeaders {
152+ private createHeaders ( operations : ApolloLink . Operation [ ] ) : HttpHeaders {
148153 return operations . reduce (
149- ( headers : HttpHeaders , operation : Operation ) => {
150- return mergeHeaders ( headers , operation . getContext ( ) . headers ) ;
154+ ( headers : HttpHeaders , operation : ApolloLink . Operation ) => {
155+ const { headers : contextHeaders } = operation . getContext ( ) ;
156+ return contextHeaders ? mergeHeaders ( headers , contextHeaders ) : headers ;
151157 } ,
152158 createHeadersWithClientAwareness ( {
153159 headers : this . options . headers ,
@@ -156,7 +162,7 @@ export class HttpBatchLinkHandler extends ApolloLink {
156162 ) ;
157163 }
158164
159- private createBatchKey ( operation : Operation ) : string {
165+ private createBatchKey ( operation : ApolloLink . Operation ) : string {
160166 const context : Context & { skipBatching ?: boolean } = operation . getContext ( ) ;
161167
162168 if ( context . skipBatching ) {
@@ -175,8 +181,11 @@ export class HttpBatchLinkHandler extends ApolloLink {
175181 return prioritize ( context . uri , this . options . uri , '' ) + opts ;
176182 }
177183
178- public request ( op : Operation ) : LinkObservable < FetchResult > | null {
179- return this . batcher . request ( op ) ;
184+ public request (
185+ op : ApolloLink . Operation ,
186+ forward : ApolloLink . ForwardFunction ,
187+ ) : Observable < ApolloLink . Result > {
188+ return this . batcher . request ( op , forward ) ;
180189 }
181190}
182191
@@ -186,7 +195,7 @@ export class HttpBatchLinkHandler extends ApolloLink {
186195export class HttpBatchLink {
187196 constructor ( private readonly httpClient : HttpClient ) { }
188197
189- public create ( options : BatchOptions ) : HttpBatchLinkHandler {
198+ public create ( options : HttpBatchLink . Options ) : HttpBatchLinkHandler {
190199 return new HttpBatchLinkHandler ( this . httpClient , options ) ;
191200 }
192201}
0 commit comments