Network layer
Apollo Angular comes with two kinds of network layer based on Angular's HttpClient
.
#
Http LinkAn Apollo Link to allow sending a single http request per operation. It's based on Angular's HttpClient
.
Why not @apollo/client/link/http
? You get SSR for free, ability to use Http Interceptors and easier testing.
#
Usage#
HttpClientThe HTTP Link relies on having HttpClient
(from @angular/common/http
)
present in your application.
#
OptionsHTTP Link takes an object with some options on it to customize the behavior of
the link. If your server supports it, the HTTP link can also send over metadata
about the request in the extensions field. To enable this, pass
includeExtensions
as true. If you would like to use persisted queries or just
not to send a query, disable includeQuery
.
name | value | default | required |
---|---|---|---|
uri | string / function | /graphql | false |
includeExtensions | boolean | false | false |
includeQuery | boolean | true | false |
headers | HttpHeaders | none | false |
withCredentials | boolean | none | false |
method | string | POST | false |
useGETForQueries | boolean | false | false |
#
ContextThe HTTP Link uses the headers
field on the context to allow passing headers
to the HTTP request. It also supports the withCredentials
field for defining
credentials policy for request. These options will override the same key if
passed when creating the the link. If some setting is different than the one in
Options, this one will be used.
name | value | default | required |
---|---|---|---|
uri | string | as in options | false |
includeExtensions | boolean | as in options | false |
includeQuery | boolean | as in options | false |
headers | HttpHeaders | none | false |
withCredentials | boolean | as in options | false |
method | string | as in options | false |
useMultipart | boolean | as in options | false |
#
Uri as function#
File uploadIn order to upload a file, you need to turn on useMultipart
flag:
#
Middleware#
Afterware (error)#
Http Batching LinkAn Apollo Link to combine multiple GraphQL operations into single HTTP request.
#
Usage#
HttpClientThe HTTP Link relies on having HttpClient
(from @angular/common/http
)
present in your application.
#
OptionsAccepts the same options as HttpLink
.
#
BatchOptionsThe batching options indicate how operations are batched together.
name | value | default | required |
---|---|---|---|
batchInterval | number | 10 | false |
batchMax | number | 10 | false |
batchKey | Function | - | false |
batchInterval
- the maximum time a batch will wait before automatically being sent over the networkbatchMax
- the size of batchesbatchKey
a function that accepts an operation and returns a string key, which uniquely names the batch the operation belongs to, defaults to returning the same string.
NOTICE:
batchKey
by default batches together requests with the same uri and the same options. Since options from an operation's context overwrites those from a link you could end up with few differents keys and what it means, few separate requests.
#
ContextWorks in the same way as in HttpLink
.
To skip batching you can set skipBatching: true
in operation's context.
NOTICE:
skipBatching
works only with the defaultbatchKey
. To create custom one you should check ifskipBatching
is set in context and generate a randombatchKey
for that operation.