Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
alessbell committed Mar 7, 2023
1 parent b30aead commit 5b8f2e2
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 16 deletions.
17 changes: 10 additions & 7 deletions src/link/http/createHttpLink.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ import {
import { createSignalIfSupported } from './createSignalIfSupported';
import { rewriteURIForGET } from './rewriteURIForGET';
import { fromError } from '../utils';
import { maybe } from '../../utilities';
import { maybe, getMainDefinition } from '../../utilities';

const backupFetch = maybe(() => fetch);

Expand Down Expand Up @@ -132,20 +132,23 @@ export const createHttpLink = (linkOptions: HttpOptions = {}) => {
const definitionIsSubscription = (d: DefinitionNode) => {
return d.kind === 'OperationDefinition' && d.operation === 'subscription';
};
const isSubscription = definitionIsSubscription(getMainDefinition(operation.query));
// does not match custom directives beginning with @defer
const hasDefer = hasDirectives(['defer'], operation.query);
if (
useGETForQueries &&
!operation.query.definitions.some(definitionIsMutation)
) {
options.method = 'GET';
}

// does not match custom directives beginning with @defer
if (
hasDirectives(['defer'], operation.query) ||
operation.query.definitions.some(definitionIsSubscription)
) {
if (hasDefer || isSubscription) {
options.headers = options.headers || {};
options.headers.accept = "multipart/mixed; deferSpec=20220824, application/json";
let acceptHeader = "multipart/mixed; ";
// if (isSubscription) acceptHeader += 'boundary=\"graphql\"; subscriptionSpec=1.0, application/json';
if (isSubscription) acceptHeader += 'boundary=graphql, application/json';
if (hasDefer) acceptHeader += 'deferSpec=20220824, application/json';
options.headers.accept = acceptHeader;
}

if (options.method === 'GET') {
Expand Down
25 changes: 16 additions & 9 deletions src/link/http/parseAndCheckHttpResponse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export async function readMultipartBody<
);
}
const decoder = new TextDecoder("utf-8");
const contentType = response.headers?.get('content-type');
const contentType = response.headers?.get("content-type");
const delimiter = "boundary=";

// parse boundary value and ignore any subsequent name/value pairs after ;
Expand Down Expand Up @@ -61,7 +61,9 @@ export async function readMultipartBody<
contentType &&
contentType.toLowerCase().indexOf("application/json") === -1
) {
throw new Error("Unsupported patch content type: application/json is required.");
throw new Error(
"Unsupported patch content type: application/json is required."
);
}
const body = message.slice(i);

Expand All @@ -71,11 +73,16 @@ export async function readMultipartBody<
Object.keys(result).length > 1 ||
"data" in result ||
"incremental" in result ||
"errors" in result
"errors" in result ||
"payload" in result
) {
// for the last chunk with only `hasNext: false`,
// we don't need to call observer.next as there is no data/errors
observer.next?.(result);
if ("payload" in result) {
observer.next?.(result.payload as T);
} else {
// for the last chunk with only `hasNext: false` or `done: true`
// we don't need to call observer.next as there is no data/errors
observer.next?.(result);
}
}
} catch (err) {
handleError(err, observer);
Expand Down Expand Up @@ -108,13 +115,13 @@ export function parseJsonBody<T>(response: Response, bodyText: string): T {
try {
return JSON.parse(bodyText);
} catch (err) {
return bodyText
return bodyText;
}
}
};
throwServerError(
response,
getResult(),
`Response not successful: Received status code ${response.status}`,
`Response not successful: Received status code ${response.status}`
);
}

Expand Down

0 comments on commit 5b8f2e2

Please sign in to comment.