Skip to content

Commit e3db4c2

Browse files
committed
Add a new option print for the function createUploadLink.
Closes #274 .
1 parent 37dbf10 commit e3db4c2

File tree

3 files changed

+80
-2
lines changed

3 files changed

+80
-2
lines changed

changelog.md

+6
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,12 @@
6161
- [`compilerOptions.maxNodeModuleJsDepth`](https://www.typescriptlang.org/tsconfig#maxNodeModuleJsDepth) should be reasonably large, e.g. `10`.
6262
- [`compilerOptions.module`](https://www.typescriptlang.org/tsconfig#module) should be `"node16"` or `"nodenext"`.
6363

64+
- Internally, use the function `selectHttpOptionsAndBodyInternal` that was added in [`@apollo/client`](https://npm.im/@apollo/client) [v3.5.5](https://github.com/apollographql/apollo-client/releases/tag/v3.5.5).
65+
66+
### Minor
67+
68+
- Added a new option `print` for the function `createUploadLink`, to customize how the GraphQL query or mutation AST prints to a string for transport. It that works like the same option for [`HttpLink`](https://www.apollographql.com/docs/react/api/link/apollo-link-http).
69+
6470
### Patch
6571

6672
- Updated dev dependencies.

createUploadLink.mjs

+8-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import { createSignalIfSupported } from "@apollo/client/link/http/createSignalIf
55
import { parseAndCheckHttpResponse } from "@apollo/client/link/http/parseAndCheckHttpResponse.js";
66
import { rewriteURIForGET } from "@apollo/client/link/http/rewriteURIForGET.js";
77
import {
8+
defaultPrinter,
89
fallbackHttpConfig,
9-
selectHttpOptionsAndBody,
10+
selectHttpOptionsAndBodyInternal,
1011
} from "@apollo/client/link/http/selectHttpOptionsAndBody.js";
1112
import { selectURI } from "@apollo/client/link/http/selectURI.js";
1213
import { serializeFetchParameter } from "@apollo/client/link/http/serializeFetchParameter.js";
@@ -46,6 +47,9 @@ import isExtractableFile from "./isExtractableFile.mjs";
4647
* Customizes how extracted files are appended to the
4748
* [`FormData`](https://developer.mozilla.org/en-US/docs/Web/API/FormData)
4849
* instance. Defaults to {@linkcode formDataAppendFile}.
50+
* @param {import("@apollo/client/link/http/selectHttpOptionsAndBody.js").Printer} [options.print]
51+
* Prints the GraphQL query or mutation AST to a string for transport.
52+
* Defaults to {@linkcode defaultPrinter}.
4953
* @param {typeof fetch} [options.fetch] [`fetch`](https://fetch.spec.whatwg.org)
5054
* implementation. Defaults to the {@linkcode fetch} global.
5155
* @param {RequestInit} [options.fetchOptions] `fetch` options; overridden by
@@ -78,6 +82,7 @@ export default function createUploadLink({
7882
isExtractableFile: customIsExtractableFile = isExtractableFile,
7983
FormData: CustomFormData,
8084
formDataAppendFile: customFormDataAppendFile = formDataAppendFile,
85+
print = defaultPrinter,
8186
fetch: customFetch,
8287
fetchOptions,
8388
credentials,
@@ -122,8 +127,9 @@ export default function createUploadLink({
122127
},
123128
};
124129

125-
const { options, body } = selectHttpOptionsAndBody(
130+
const { options, body } = selectHttpOptionsAndBodyInternal(
126131
operation,
132+
print,
127133
fallbackHttpConfig,
128134
linkConfig,
129135
contextConfig,

createUploadLink.test.mjs

+66
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import { describe, it } from "node:test";
88
import { ApolloLink } from "@apollo/client/link/core/ApolloLink.js";
99
import { concat } from "@apollo/client/link/core/concat.js";
1010
import { execute } from "@apollo/client/link/core/execute.js";
11+
import { stripIgnoredCharacters } from "graphql";
1112
import { gql } from "graphql-tag";
1213
import revertableGlobals from "revertable-globals";
1314

@@ -315,6 +316,71 @@ describe("Function `createUploadLink`.", { concurrency: true }, () => {
315316
deepStrictEqual(nextData, payload);
316317
});
317318

319+
it("Option `print`.", async () => {
320+
/** @type {unknown} */
321+
let fetchInput;
322+
323+
/** @type {RequestInit | undefined} */
324+
let fetchOptions;
325+
326+
/** @type {unknown} */
327+
let nextData;
328+
329+
const query = "{\n a\n}";
330+
const payload = { data: { a: true } };
331+
332+
await timeLimitPromise(
333+
/** @type {Promise<void>} */ (
334+
new Promise((resolve, reject) => {
335+
execute(
336+
createUploadLink({
337+
print: (ast, originalPrint) =>
338+
stripIgnoredCharacters(originalPrint(ast)),
339+
async fetch(input, options) {
340+
fetchInput = input;
341+
fetchOptions = options;
342+
343+
return new Response(
344+
JSON.stringify(payload),
345+
graphqlResponseOptions,
346+
);
347+
},
348+
}),
349+
{
350+
query: gql(query),
351+
},
352+
).subscribe({
353+
next(data) {
354+
nextData = data;
355+
},
356+
error() {
357+
reject(createUnexpectedCallError());
358+
},
359+
complete() {
360+
resolve();
361+
},
362+
});
363+
})
364+
),
365+
);
366+
367+
strictEqual(fetchInput, defaultUri);
368+
ok(typeof fetchOptions === "object");
369+
370+
const { signal: fetchOptionsSignal, ...fetchOptionsRest } = fetchOptions;
371+
372+
ok(fetchOptionsSignal instanceof AbortSignal);
373+
deepEqual(fetchOptionsRest, {
374+
method: "POST",
375+
headers: { accept: "*/*", "content-type": "application/json" },
376+
body: JSON.stringify({
377+
variables: {},
378+
query: stripIgnoredCharacters(query),
379+
}),
380+
});
381+
deepStrictEqual(nextData, payload);
382+
});
383+
318384
it("Option `fetchOptions.method`.", async () => {
319385
/** @type {unknown} */
320386
let fetchInput;

0 commit comments

Comments
 (0)