Skip to content

Commit

Permalink
rollback custom handler and don't throw on graphql errors
Browse files Browse the repository at this point in the history
  • Loading branch information
HarroE committed Dec 18, 2023
1 parent d992e3f commit f9a53e8
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 36 deletions.
15 changes: 0 additions & 15 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,4 @@ describe("gqlClientFetch", () => {
expect.anything() // <- body, method, headers, etc, are tested in the above
);
});

it("should use custom response handler", async () => {
const customerFetcher = initClientFetcher("https://localhost/graphql", {
handleResponse: async (response) => ({
customHandler: true,
body: await response.json(),
}),
});

fetchMock.mockResponse(responseString);
const gqlResponse = await customerFetcher(mutation, {
myVar: "baz",
});
expect(gqlResponse).toEqual({ body: response, customHandler: true });
});
});
20 changes: 4 additions & 16 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,15 @@ import {
extractOperationName,
getQueryHash,
getQueryType,
handleResponse as defaultHandleResponse,
handleResponse,
hasPersistedQueryError,
} from "./helpers";

type beforeRequestFn = () => Promise<void>;
type handleResponseFn = (response: Response) => Promise<any>;

type Options = {
beforeRequest?: beforeRequestFn;
persisted?: boolean;
handleResponse?: handleResponseFn;
};

export type ClientFetcher = <TResponse, TVariables>(
Expand All @@ -27,7 +25,7 @@ export type ClientFetcher = <TResponse, TVariables>(
export const initClientFetcher =
(
endpoint: string,
{ beforeRequest, persisted, handleResponse }: Options = {}
{ beforeRequest, persisted }: Options = {}
): ClientFetcher =>
/**
* Executes a GraphQL query post request on the client.
Expand Down Expand Up @@ -82,7 +80,7 @@ export const initClientFetcher =
const hasError = await hasPersistedQueryError(response);

if (!hasError) {
return responseHandler(response, handleResponse);
return handleResponse(response);
}
}

Expand All @@ -93,15 +91,5 @@ export const initClientFetcher =
credentials: "include",
});

return responseHandler(response, handleResponse);
};

const responseHandler = (
response: Response,
handleResponse?: handleResponseFn
) => {
if (handleResponse) {
return handleResponse(response);
}
return defaultHandleResponse(response);
};
};
6 changes: 1 addition & 5 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export const createSha256 = async (message: string) => {
};

/**
* Helper function that parses the response body and returns the data or throws an error
* Helper function that parses the response body and returns the data or throws an error.
* @param response Fetch response object
* @returns GraphQL response body
*/
Expand All @@ -70,10 +70,6 @@ export const handleResponse = async (response: Response) => {
.json()
.catch((err) => invariant(false, "Could not parse JSON from response"));

// Check for GraphQL errors
const hasNoErrors = !(body.errors?.length && body.errors.length > 0);
invariant(hasNoErrors, JSON.stringify(body.errors, null, 2));

return body;
};

Expand Down

0 comments on commit f9a53e8

Please sign in to comment.