Skip to content

Commit

Permalink
fix: remove json parse from ast node print
Browse files Browse the repository at this point in the history
  • Loading branch information
korsvanloon committed Oct 25, 2024
1 parent 29233a5 commit 3acb3a5
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/friendly-tables-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@labdigital/graphql-fetcher": patch
---

remove json parse from ast node
20 changes: 13 additions & 7 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ describe("gqlClientFetch", () => {
});

it("should use the provided timeout duration", async () => {
vi.useFakeTimers();
const fetcher = initClientFetcher("https://localhost/graphql", {
defaultTimeout: 1,
});
Expand All @@ -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
Expand Down Expand Up @@ -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);

Expand Down
4 changes: 1 addition & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 3 additions & 0 deletions src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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
Expand Down
6 changes: 2 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -51,9 +51,7 @@ export const initServerFetcher =
{ cache, next = {} }: CacheOptions,
signal: AbortSignal = AbortSignal.timeout(defaultTimeout)
): Promise<GqlResponse<TResponse>> => {
const query = isNode(astNode)
? JSON.parse(print(astNode))
: astNode.toString();
const query = isNode(astNode) ? print(astNode) : astNode.toString();

const operationName = extractOperationName(query) || "(GraphQL)";

Expand Down
2 changes: 1 addition & 1 deletion src/testing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ export class TypedDocumentString<TResult, TVariables>

// 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";
}

0 comments on commit 3acb3a5

Please sign in to comment.