Skip to content

Commit 616e740

Browse files
derickorobrichard
authored andcommitted
add options object to fetchImpl (#5)
* add credentials param to fetch and xhr * update readme
1 parent 02aa10e commit 616e740

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

README.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# fetch-multipart-graphql
2+
23
Cross-browser function to fetch and parse streaming multipart graphql responses.
34

45
This allows you to efficiently fetch streamed GraphQL responses that use the @defer directive as supported by [Apollo Server](https://blog.apollographql.com/introducing-defer-in-apollo-server-f6797c4e9d6e). It can be easily used in a Relay Modern network layer to support deferred queries.
@@ -8,7 +9,6 @@ This allows you to efficiently fetch streamed GraphQL responses that use the @de
89
In a Relay Network Layer:
910

1011
```javascript
11-
1212
import fetchMultipart from 'fetch-multipart-graphql';
1313
import { Observable } from 'relay-runtime';
1414

@@ -17,21 +17,25 @@ function fetchQuery(operation, variables) {
1717
fetchMultipart('/graphql', {
1818
method: 'POST',
1919
headers: {
20-
'content-type': 'application/json'
20+
'content-type': 'application/json',
2121
},
2222
body: JSON.stringify({
2323
query: operation.text,
2424
variables,
2525
}),
26+
credentials: 'same-origin',
2627
onNext: json => sink.next(json),
2728
onError: err => sink.error(err),
2829
onComplete: () => sink.complete(),
2930
});
3031
});
3132
}
32-
3333
```
3434

35+
#### Handling cookies and other auth headers
36+
37+
The `credentials` param is passed to [`fetch`](https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/fetch#Parameters). For XHR requests, [`XMLHttpRequest.withCredentials`](https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/withCredentials) is mapped to true when `credentials: 'include'`.
38+
3539
## Browser Support
3640

3741
Tested in the latest Chrome, Firefox, Safari, Edge, and Internet Explorer 11. Requires a polyfill for TextEncoder/Decoder. Since only utf-8 encoding is required, it's recommended to use [text-encoding-utf-8](https://www.npmjs.com/package/text-encoding-utf-8) to minimize impact on bundle size.

src/fetch.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { PatchResolver } from './PatchResolver';
22

3-
export function fetchImpl(url, { method, headers, body, onNext, onError, onComplete }) {
4-
return fetch(url, { method, headers, body }).then(response => {
3+
export function fetchImpl(url, { method, headers, credentials, body, onNext, onError, onComplete }) {
4+
return fetch(url, { method, headers, body, credentials }).then(response => {
55
// @defer uses multipart responses to stream patches over HTTP
66
if (
77
response.status < 300 &&

src/xhr.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,9 @@ function supportsXhrResponseType(type) {
1111
return false;
1212
}
1313

14-
export function xhrImpl(url, { method, headers, body, onNext, onError, onComplete }) {
14+
export function xhrImpl(url, { method, headers, credentials, body, onNext, onError, onComplete }) {
1515
const xhr = new XMLHttpRequest();
16+
xhr.withCredentials = credentials === 'include'; // follow behavior of fetch credentials param https://github.com/github/fetch#sending-cookies
1617
let index = 0;
1718
let isDeferred = false;
1819

0 commit comments

Comments
 (0)