From 76b6a033a1a4a9391fcdec041cd1ad6893900800 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Tue, 21 Jan 2025 17:50:03 +0000 Subject: [PATCH] update version (next) --- .changeset/pre.json | 3 +- CHANGELOG.md | 6 ++++ package.json | 2 +- src/client.test.ts | 20 ++++++----- src/client.ts | 2 +- src/helpers.ts | 4 +-- src/request.test.ts | 82 +++++++++++++++++++-------------------------- src/server.test.ts | 49 +++++++++++++++------------ src/server.ts | 18 +++++++--- 9 files changed, 100 insertions(+), 86 deletions(-) diff --git a/.changeset/pre.json b/.changeset/pre.json index 11acfe8..6bb439d 100644 --- a/.changeset/pre.json +++ b/.changeset/pre.json @@ -7,6 +7,7 @@ "changesets": [ "beige-donuts-attend", "five-weeks-talk", - "tough-grapes-hug" + "tough-grapes-hug", + "warm-tomatoes-wonder" ] } diff --git a/CHANGELOG.md b/CHANGELOG.md index 80fe480..e7b7e66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ # @labdigital/react-query-opal +## 2.0.0-next.3 + +### Major Changes + +- 3dc1d79: Internal refactor to better support persisted operations / trusted documents + ## 1.5.0-next.2 ### Patch Changes diff --git a/package.json b/package.json index a97f2a6..fd92890 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@labdigital/graphql-fetcher", - "version": "1.5.0-next.2", + "version": "2.0.0-next.3", "description": "Custom fetcher for react-query to use with @labdigital/node-federated-token", "type": "module", "main": "./dist/index.cjs", diff --git a/src/client.test.ts b/src/client.test.ts index 09fc64e..a1983d4 100644 --- a/src/client.test.ts +++ b/src/client.test.ts @@ -56,10 +56,12 @@ describe("gqlClientFetch", () => { body: JSON.stringify({ query: query, variables: { myVar: "baz" }, - extensions: { persistedQuery: { - version: 1, - sha256Hash: await createSha256(query.toString()), - }}, + extensions: { + persistedQuery: { + version: 1, + sha256Hash: await createSha256(query.toString()), + }, + }, }), // Method was post: method: "POST", @@ -242,10 +244,12 @@ describe("gqlClientFetch", () => { body: JSON.stringify({ query: query, variables: { myVar: "baz" }, - extensions: { persistedQuery: { - version: 1, - sha256Hash: await createSha256(query.toString()), - }}, + extensions: { + persistedQuery: { + version: 1, + sha256Hash: await createSha256(query.toString()), + }, + }, }), // Method was post: method: "POST", diff --git a/src/client.ts b/src/client.ts index c988207..8790446 100644 --- a/src/client.ts +++ b/src/client.ts @@ -41,7 +41,7 @@ type Options = { */ defaultHeaders?: Headers | Record; - mode?: ModeFlags + mode?: ModeFlags; /** * Function to customize creating the documentId from a query diff --git a/src/helpers.ts b/src/helpers.ts index 401f0bd..d26b1a1 100644 --- a/src/helpers.ts +++ b/src/helpers.ts @@ -56,14 +56,14 @@ export interface NextFetchRequestConfig { } export const pruneObject = (object: T): Partial => { - const data: Record = {} + const data: Record = {}; for (const key in object) { if (isNotEmpty(object[key])) { data[key] = object[key]; } } return JSON.parse(JSON.stringify(data ?? null)); -} +}; const isNotEmpty = (value: unknown) => value && Object.keys(value).length > 0; diff --git a/src/request.test.ts b/src/request.test.ts index 7b611ba..0471439 100644 --- a/src/request.test.ts +++ b/src/request.test.ts @@ -18,49 +18,42 @@ it.each(["both", "document", "persistent"])( documentId: "123", } as GraphQLRequest>); - const result: Record = {} + const result: Record = {}; data.forEach((value, key) => { - result[key] = value - }) + result[key] = value; + }); switch (mode) { case "both": { - expect(result).toStrictEqual( - { - documentId: "123", - op: "hello", - variables: '{"name":"world"}', - extensions: '{"persistedQuery":{"version":1,"sha256Hash":"123"}}', - }, - ); + expect(result).toStrictEqual({ + documentId: "123", + op: "hello", + variables: '{"name":"world"}', + extensions: '{"persistedQuery":{"version":1,"sha256Hash":"123"}}', + }); break; } case "persisted": { - expect(result).toStrictEqual( - { - documentId: "123", - op: "hello", - variables: '{"name":"world"}', - extensions: '{"persistedQuery":{"version":1,"sha256Hash":"123"}}', - } - ); + expect(result).toStrictEqual({ + documentId: "123", + op: "hello", + variables: '{"name":"world"}', + extensions: '{"persistedQuery":{"version":1,"sha256Hash":"123"}}', + }); break; } case "document": { - expect(result).toStrictEqual( - { - op: "hello", - variables: '{"name":"world"}', - extensions: '{"persistedQuery":{"version":1,"sha256Hash":"123"}}', - } - ); + expect(result).toStrictEqual({ + op: "hello", + variables: '{"name":"world"}', + extensions: '{"persistedQuery":{"version":1,"sha256Hash":"123"}}', + }); break; } } }, ); - it.each(["both", "document", "persistent"])( "createRequestURL - minimal mode=%s", (mode) => { @@ -72,39 +65,32 @@ it.each(["both", "document", "persistent"])( documentId: "123", } as GraphQLRequest>); - const result: Record = {} + const result: Record = {}; data.forEach((value, key) => { - result[key] = value - }) + result[key] = value; + }); switch (mode) { case "both": { - expect(result).toStrictEqual( - { - documentId: "123", - op: "hello", - }, - ); + expect(result).toStrictEqual({ + documentId: "123", + op: "hello", + }); break; } case "persisted": { - expect(result).toStrictEqual( - { - documentId: "123", - op: "hello", - } - ); + expect(result).toStrictEqual({ + documentId: "123", + op: "hello", + }); break; } case "document": { - expect(result).toStrictEqual( - { - op: "hello", - } - ); + expect(result).toStrictEqual({ + op: "hello", + }); break; } } }, ); - diff --git a/src/server.test.ts b/src/server.test.ts index 6d69c78..3bfb22c 100644 --- a/src/server.test.ts +++ b/src/server.test.ts @@ -82,10 +82,12 @@ describe("gqlServerFetch", () => { body: JSON.stringify({ query: query.toString(), variables: { myVar: "baz" }, - extensions: { persistedQuery: { - version: 1, - sha256Hash: await createSha256(query.toString()), - }}, + extensions: { + persistedQuery: { + version: 1, + sha256Hash: await createSha256(query.toString()), + }, + }, }), headers: new Headers({ "Content-Type": "application/json", @@ -118,10 +120,12 @@ describe("gqlServerFetch", () => { body: JSON.stringify({ query: queryMutation.toString(), variables: { myVar: "baz" }, - extensions: { persistedQuery: { - version: 1, - sha256Hash: await createSha256(queryMutation.toString()), - }}, + extensions: { + persistedQuery: { + version: 1, + sha256Hash: await createSha256(queryMutation.toString()), + }, + }, }), headers: new Headers({ "Content-Type": "application/json", @@ -199,7 +203,7 @@ describe("gqlServerFetch", () => { }), cache: "force-cache", next: { revalidate: 900 }, - signal: expect.any(AbortSignal) + signal: expect.any(AbortSignal), }, ); }); @@ -218,18 +222,21 @@ describe("gqlServerFetch", () => { expect(gqlResponse).toEqual(response); expect(mockedFetch).toHaveBeenCalledTimes(1); - expect(mockedFetch).toHaveBeenCalledWith("https://localhost/graphql?op=myQuery", { - method: "POST", // <- Note that when caching is disabled, the method is 'POST' - body: JSON.stringify({ - query: query.toString(), - variables: { myVar: "baz" }, - }), - headers: new Headers({ - "Content-Type": "application/json", - }), - cache: "no-store", - signal: expect.any(AbortSignal), - }); + expect(mockedFetch).toHaveBeenCalledWith( + "https://localhost/graphql?op=myQuery", + { + method: "POST", // <- Note that when caching is disabled, the method is 'POST' + body: JSON.stringify({ + query: query.toString(), + variables: { myVar: "baz" }, + }), + headers: new Headers({ + "Content-Type": "application/json", + }), + cache: "no-store", + signal: expect.any(AbortSignal), + }, + ); }); // This seems as if we test fetch itself but we're actually testing whether the fetcher properly propagates the fetch errors to the package consumers diff --git a/src/server.ts b/src/server.ts index 2a6b7d2..a8fccc6 100644 --- a/src/server.ts +++ b/src/server.ts @@ -146,7 +146,7 @@ export const initServerFetcher = } catch (err: unknown) { span.setStatus({ code: SpanStatusCode.ERROR, - message: err instanceof Error ? err.message : String(err) + message: err instanceof Error ? err.message : String(err), }); throw err; } @@ -168,7 +168,12 @@ export const initServerFetcher = if (!isPersistedQuery(request) && hasPersistedQueryError(response)) { // If the cached query doesn't exist, fall back to POST request and // let the server cache it. - response = await gqlPost(url, request, { cache, next }, requestOptions); + response = await gqlPost( + url, + request, + { cache, next }, + requestOptions, + ); } span.end(); @@ -227,10 +232,15 @@ const gqlPersistedQuery = async ( * @param response Fetch response object * @returns GraphQL response body */ -const parseResponse = async (request: GraphQLRequest, response: Response) => { +const parseResponse = async ( + request: GraphQLRequest, + response: Response, +) => { invariant( response.ok, - errorMessage(`Response for ${request.operationName} errored: ${response.status} ${response.statusText}`), + errorMessage( + `Response for ${request.operationName} errored: ${response.status} ${response.statusText}`, + ), ); return await response.json();