@operato/graphql 10.29.69 → 10.29.76

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,130 +0,0 @@
1
- import { expect } from '@open-wc/testing';
2
- import gql from 'graphql-tag';
3
- import { execute } from '@apollo/client/core/index.js';
4
- import { getClientLink } from '../src/graphql-env.js';
5
- import { uploadContext, uploadFetch } from '../src/upload-progress.js';
6
- /**
7
- * Upload progress.
8
- *
9
- * ## Why this is tested
10
- *
11
- * Only XHR can tell how much of a body went out, so an upload that asks for progress leaves `fetch`.
12
- * What must hold:
13
- *
14
- * ```
15
- * unchanged a request that does not ask for progress — every query, every plain upload — still
16
- * goes through fetch with the same arguments; no XHR is made
17
- * progress an upload that asks gets fractions up to 1, and the same Response fetch would give
18
- * credentials the session cookie still goes (credentials: include → withCredentials)
19
- * abort the link's abort stops the XHR and fails like an aborted fetch
20
- * ```
21
- */
22
- class FakeXHR {
23
- constructor() {
24
- this.method = '';
25
- this.url = '';
26
- this.withCredentials = false;
27
- this.headers = {};
28
- this.aborted = false;
29
- this.status = 200;
30
- this.statusText = 'OK';
31
- this.responseText = '{"data":{"attach":{"id":"1"}}}';
32
- this.upload = {};
33
- FakeXHR.made.push(this);
34
- }
35
- open(method, url) {
36
- this.method = method;
37
- this.url = url;
38
- }
39
- setRequestHeader(name, value) {
40
- this.headers[name] = value;
41
- }
42
- getAllResponseHeaders() {
43
- return 'content-type: application/json\r\nx-one: 1\r\n';
44
- }
45
- send(body) {
46
- this.body = body;
47
- setTimeout(() => {
48
- var _a, _b, _c;
49
- (_b = (_a = this.upload).onprogress) === null || _b === void 0 ? void 0 : _b.call(_a, { lengthComputable: true, loaded: 50, total: 100 });
50
- (_c = this.onload) === null || _c === void 0 ? void 0 : _c.call(this);
51
- });
52
- }
53
- abort() {
54
- var _a;
55
- this.aborted = true;
56
- (_a = this.onabort) === null || _a === void 0 ? void 0 : _a.call(this);
57
- }
58
- }
59
- FakeXHR.made = [];
60
- describe('upload progress', () => {
61
- const realXHR = window.XMLHttpRequest;
62
- const realFetch = window.fetch;
63
- let fetched;
64
- beforeEach(() => {
65
- FakeXHR.made = [];
66
- fetched = [];
67
- window.XMLHttpRequest = FakeXHR;
68
- window.fetch = (async (...args) => {
69
- fetched.push(args);
70
- return new Response('{"data":{"one":1}}', { status: 200, headers: { 'content-type': 'application/json' } });
71
- });
72
- });
73
- afterEach(() => {
74
- window.XMLHttpRequest = realXHR;
75
- window.fetch = realFetch;
76
- });
77
- it('leaves a request without progress to fetch, as it was', async () => {
78
- const init = { method: 'POST', body: 'x', credentials: 'include' };
79
- await uploadFetch('/graphql', init);
80
- expect(FakeXHR.made.length).to.equal(0);
81
- expect(fetched.length).to.equal(1);
82
- expect(fetched[0][0]).to.equal('/graphql');
83
- expect(fetched[0][1]).to.deep.equal(init);
84
- });
85
- it('reports progress and answers with the response fetch would give', async () => {
86
- const fractions = [];
87
- const response = await uploadFetch('/graphql', {
88
- method: 'POST',
89
- credentials: 'include',
90
- headers: { 'Apollo-Require-Preflight': 'true' },
91
- body: 'form',
92
- onUploadProgress: (fraction) => fractions.push(fraction)
93
- });
94
- const xhr = FakeXHR.made[0];
95
- expect(xhr.withCredentials, 'the session cookie stays home').to.equal(true);
96
- expect(xhr.headers['apollo-require-preflight']).to.equal('true');
97
- expect(xhr.body).to.equal('form');
98
- expect(fractions).to.deep.equal([0.5, 1]);
99
- expect(response.status).to.equal(200);
100
- expect(response.headers.get('x-one')).to.equal('1');
101
- expect(await response.json()).to.deep.equal({ data: { attach: { id: '1' } } });
102
- });
103
- it('stops the upload when aborted', async () => {
104
- const controller = new AbortController();
105
- const sent = uploadFetch('/graphql', { method: 'POST', body: 'form', signal: controller.signal, onUploadProgress: () => { } });
106
- controller.abort();
107
- const error = await sent.then(() => null, e => e);
108
- expect(FakeXHR.made[0].aborted).to.equal(true);
109
- expect(error === null || error === void 0 ? void 0 : error.name).to.equal('AbortError');
110
- });
111
- it('sends a query through fetch and an upload with progress through XHR', async () => {
112
- const run = (operation) => new Promise((resolve, reject) => execute(getClientLink(), operation).subscribe({ next: resolve, error: reject }));
113
- await run({ query: gql `query { one }` });
114
- expect(fetched.length, 'a query left fetch').to.equal(1);
115
- expect(FakeXHR.made.length, 'a query went through XHR').to.equal(0);
116
- const fractions = [];
117
- const file = new File(['abc'], 'a.pdf', { type: 'application/pdf' });
118
- const result = await run({
119
- query: gql `mutation ($file: Upload!) { attach(file: $file) { id } }`,
120
- variables: { file },
121
- context: uploadContext(fraction => fractions.push(fraction))
122
- });
123
- expect(fetched.length, 'the upload went through fetch').to.equal(1);
124
- expect(FakeXHR.made.length).to.equal(1);
125
- expect(FakeXHR.made[0].body).to.be.instanceOf(FormData);
126
- expect(fractions).to.deep.equal([0.5, 1]);
127
- expect(result.data.attach.id).to.equal('1');
128
- });
129
- });
130
- //# sourceMappingURL=upload-progress.test.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"upload-progress.test.js","sourceRoot":"","sources":["../../test/upload-progress.test.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,kBAAkB,CAAA;AACzC,OAAO,GAAG,MAAM,aAAa,CAAA;AAE7B,OAAO,EAAE,OAAO,EAAE,MAAM,8BAA8B,CAAA;AAEtD,OAAO,EAAE,aAAa,EAAE,MAAM,uBAAuB,CAAA;AACrD,OAAO,EAAE,aAAa,EAAE,WAAW,EAAE,MAAM,2BAA2B,CAAA;AAEtE;;;;;;;;;;;;;;;GAeG;AAEH,MAAM,OAAO;IAiBX;QAdA,WAAM,GAAG,EAAE,CAAA;QACX,QAAG,GAAG,EAAE,CAAA;QACR,oBAAe,GAAG,KAAK,CAAA;QACvB,YAAO,GAA2B,EAAE,CAAA;QAEpC,YAAO,GAAG,KAAK,CAAA;QACf,WAAM,GAAG,GAAG,CAAA;QACZ,eAAU,GAAG,IAAI,CAAA;QACjB,iBAAY,GAAG,gCAAgC,CAAA;QAC/C,WAAM,GAAsC,EAAE,CAAA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B,MAAM,IAAI,GAAG,IAAI,IAAI,CAAC,CAAC,KAAK,CAAC,EAAE,OAAO,EAAE,EAAE,IAAI,EAAE,iBAAiB,EAAE,CAAC,CAAA;QACpE,MAAM,MAAM,GAAQ,MAAM,GAAG,CAAC;YAC5B,KAAK,EAAE,GAAG,CAAA,0DAA0D;YACpE,SAAS,EAAE,EAAE,IAAI,EAAE;YACnB,OAAO,EAAE,aAAa,CAAC,QAAQ,CAAC,EAAE,CAAC,SAAS,CAAC,IAAI,CAAC,QAAQ,CAAC,CAAC;SAC7D,CAAC,CAAA;QAEF,MAAM,CAAC,OAAO,CAAC,MAAM,EAAE,+BAA+B,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QACnE,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,CAAC,CAAC,CAAA;QACvC,MAAM,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,UAAU,CAAC,QAAQ,CAAC,CAAA;QACvD,MAAM,CAAC,SAAS,CAAC,CAAC,EAAE,CAAC,IAAI,CAAC,KAAK,CAAC,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,CAAA;QACzC,MAAM,CAAC,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,EAAE,CAAC,CAAC,EAAE,CAAC,KAAK,CAAC,GAAG,CAAC,CAAA;IAC7C,CAAC,CAAC,CAAA;AACJ,CAAC,CAAC,CAAA","sourcesContent":["import { expect } from '@open-wc/testing'\nimport gql from 'graphql-tag'\n\nimport { execute } from '@apollo/client/core/index.js'\n\nimport { getClientLink } from '../src/graphql-env.js'\nimport { uploadContext, uploadFetch } from '../src/upload-progress.js'\n\n/**\n * Upload progress.\n *\n * ## Why this is tested\n *\n * Only XHR can tell how much of a body went out, so an upload that asks for progress leaves `fetch`.\n * What must hold:\n *\n * ```\n * unchanged a request that does not ask for progress — every query, every plain upload — still\n * goes through fetch with the same arguments; no XHR is made\n * progress an upload that asks gets fractions up to 1, and the same Response fetch would give\n * credentials the session cookie still goes (credentials: include → withCredentials)\n * abort the link's abort stops the XHR and fails like an aborted fetch\n * ```\n */\n\nclass FakeXHR {\n static made: FakeXHR[] = []\n\n method = ''\n url = ''\n withCredentials = false\n headers: Record<string, string> = {}\n body: any\n aborted = false\n status = 200\n statusText = 'OK'\n responseText = '{\"data\":{\"attach\":{\"id\":\"1\"}}}'\n upload: { onprogress?: (e: any) => void } = {}\n onload?: () => void\n onerror?: () => void\n onabort?: () => void\n\n constructor() {\n FakeXHR.made.push(this)\n }\n open(method: string, url: string) {\n this.method = method\n this.url = url\n }\n setRequestHeader(name: string, value: string) {\n this.headers[name] = value\n }\n getAllResponseHeaders() {\n return 'content-type: application/json\\r\\nx-one: 1\\r\\n'\n }\n send(body: any) {\n this.body = body\n setTimeout(() => {\n this.upload.onprogress?.({ lengthComputable: true, loaded: 50, total: 100 })\n this.onload?.()\n })\n }\n abort() {\n this.aborted = true\n this.onabort?.()\n }\n}\n\ndescribe('upload progress', () => {\n const realXHR = window.XMLHttpRequest\n const realFetch = window.fetch\n let fetched: any[]\n\n beforeEach(() => {\n FakeXHR.made = []\n fetched = []\n ;(window as any).XMLHttpRequest = FakeXHR\n window.fetch = (async (...args: any[]) => {\n fetched.push(args)\n return new Response('{\"data\":{\"one\":1}}', { status: 200, headers: { 'content-type': 'application/json' } })\n }) as any\n })\n\n afterEach(() => {\n window.XMLHttpRequest = realXHR\n window.fetch = realFetch\n })\n\n it('leaves a request without progress to fetch, as it was', async () => {\n const init = { method: 'POST', body: 'x', credentials: 'include' as RequestCredentials }\n await uploadFetch('/graphql', init)\n\n expect(FakeXHR.made.length).to.equal(0)\n expect(fetched.length).to.equal(1)\n expect(fetched[0][0]).to.equal('/graphql')\n expect(fetched[0][1]).to.deep.equal(init)\n })\n\n it('reports progress and answers with the response fetch would give', async () => {\n const fractions: number[] = []\n const response = await uploadFetch('/graphql', {\n method: 'POST',\n credentials: 'include',\n headers: { 'Apollo-Require-Preflight': 'true' },\n body: 'form',\n onUploadProgress: (fraction: number) => fractions.push(fraction)\n } as any)\n\n const xhr = FakeXHR.made[0]\n expect(xhr.withCredentials, 'the session cookie stays home').to.equal(true)\n expect(xhr.headers['apollo-require-preflight']).to.equal('true')\n expect(xhr.body).to.equal('form')\n expect(fractions).to.deep.equal([0.5, 1])\n\n expect(response.status).to.equal(200)\n expect(response.headers.get('x-one')).to.equal('1')\n expect(await response.json()).to.deep.equal({ data: { attach: { id: '1' } } })\n })\n\n it('stops the upload when aborted', async () => {\n const controller = new AbortController()\n const sent = uploadFetch('/graphql', { method: 'POST', body: 'form', signal: controller.signal, onUploadProgress: () => {} } as any)\n\n controller.abort()\n\n const error = await sent.then(\n () => null,\n e => e\n )\n expect(FakeXHR.made[0].aborted).to.equal(true)\n expect(error?.name).to.equal('AbortError')\n })\n\n it('sends a query through fetch and an upload with progress through XHR', async () => {\n const run = (operation: any) =>\n new Promise((resolve, reject) => execute(getClientLink(), operation).subscribe({ next: resolve, error: reject }))\n\n await run({ query: gql`query { one }` })\n expect(fetched.length, 'a query left fetch').to.equal(1)\n expect(FakeXHR.made.length, 'a query went through XHR').to.equal(0)\n\n const fractions: number[] = []\n const file = new File(['abc'], 'a.pdf', { type: 'application/pdf' })\n const result: any = await run({\n query: gql`mutation ($file: Upload!) { attach(file: $file) { id } }`,\n variables: { file },\n context: uploadContext(fraction => fractions.push(fraction))\n })\n\n expect(fetched.length, 'the upload went through fetch').to.equal(1)\n expect(FakeXHR.made.length).to.equal(1)\n expect(FakeXHR.made[0].body).to.be.instanceOf(FormData)\n expect(fractions).to.deep.equal([0.5, 1])\n expect(result.data.attach.id).to.equal('1')\n })\n})\n"]}