diff --git a/.changeset/friendly-tables-give.md b/.changeset/friendly-tables-give.md new file mode 100644 index 0000000..6c3afb0 --- /dev/null +++ b/.changeset/friendly-tables-give.md @@ -0,0 +1,5 @@ +--- +"@labdigital/graphql-fetcher": patch +--- + +remove json parse from ast node diff --git a/src/client.test.ts b/src/client.test.ts index b00edd5..9177ba2 100644 --- a/src/client.test.ts +++ b/src/client.test.ts @@ -161,6 +161,7 @@ describe("gqlClientFetch", () => { }); it("should use the provided timeout duration", async () => { + vi.useFakeTimers(); const fetcher = initClientFetcher("https://localhost/graphql", { defaultTimeout: 1, }); @@ -171,6 +172,8 @@ describe("gqlClientFetch", () => { myVar: "baz", }); + vi.runAllTimers(); + expect(timeoutSpy).toHaveBeenCalledWith(1); // It should not try to POST the query if the persisted query cannot be parsed @@ -201,16 +204,19 @@ describe("gqlClientFetch", () => { expect(fetchMock).toHaveBeenCalledTimes(1); }); - it("should allow passing extra HTTP headers", async () => { const mockedFetch = fetchMock.mockResponse(responseString); - const gqlResponse = await fetcher(query, { - myVar: "baz", - }, { - headers: { - "X-extra-header": "foo", + const gqlResponse = await fetcher( + query, + { + myVar: "baz", + }, + { + headers: { + "X-extra-header": "foo", + }, } - }); + ); expect(gqlResponse).toEqual(response); diff --git a/src/client.ts b/src/client.ts index a32ef9f..974a6ae 100644 --- a/src/client.ts +++ b/src/client.ts @@ -71,9 +71,7 @@ export const initClientFetcher = options.signal = AbortSignal.timeout(defaultTimeout); } - const query = isNode(astNode) - ? JSON.parse(print(astNode)) - : astNode.toString(); + const query = isNode(astNode) ? print(astNode) : astNode.toString(); const operationName = extractOperationName(query); diff --git a/src/server.test.ts b/src/server.test.ts index 47d3ce8..fd13d8f 100644 --- a/src/server.test.ts +++ b/src/server.test.ts @@ -233,6 +233,7 @@ describe("gqlServerFetch", () => { }); it("should use the provided timeout duration", async () => { + vi.useFakeTimers(); const timeoutSpy = vi.spyOn(AbortSignal, "timeout"); const gqlServerFetch = initServerFetcher("https://localhost/graphql", { defaultTimeout: 1, @@ -241,6 +242,8 @@ describe("gqlServerFetch", () => { await gqlServerFetch(query, { myVar: "baz" }, {}); + vi.runAllTimers(); + expect(timeoutSpy).toHaveBeenCalledWith(1); // It should not try to POST the query if the persisted query cannot be parsed diff --git a/src/server.ts b/src/server.ts index 88d62af..58b0f36 100644 --- a/src/server.ts +++ b/src/server.ts @@ -11,7 +11,7 @@ import { getQueryType, pruneObject, } from "./helpers"; -import { print, ASTNode } from "graphql"; +import { print } from "graphql"; import { isNode } from "graphql/language/ast"; type Options = { @@ -51,9 +51,7 @@ export const initServerFetcher = { cache, next = {} }: CacheOptions, signal: AbortSignal = AbortSignal.timeout(defaultTimeout) ): Promise> => { - const query = isNode(astNode) - ? JSON.parse(print(astNode)) - : astNode.toString(); + const query = isNode(astNode) ? print(astNode) : astNode.toString(); const operationName = extractOperationName(query) || "(GraphQL)"; diff --git a/src/testing.ts b/src/testing.ts index 3c32aca..04aa7e2 100644 --- a/src/testing.ts +++ b/src/testing.ts @@ -12,5 +12,5 @@ export class TypedDocumentString // Choosing a leaf type will trigger the print visitor to output the value directly // instead of trying to visit the children - kind = "StringValue"; + kind = "IntValue"; }