Skip to content

Commit

Permalink
Unexpected missing graphql data gives response
Browse files Browse the repository at this point in the history
  • Loading branch information
PabloSzx committed Oct 15, 2024
1 parent b9b13f3 commit 118cfd5
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 6 deletions.
5 changes: 5 additions & 0 deletions .changeset/unlucky-plums-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@soundxyz/graphql-react-query': patch
---

Unexpected missing graphql data gives response
33 changes: 27 additions & 6 deletions packages/graphql-react-query/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ export type GraphQLFetcherConfig = {
query: string;
variables: Record<string, unknown> | undefined;
graphqlErrors?: ReadonlyArray<GraphQLError>;
response: Response;
}): unknown;

/**
Expand All @@ -99,12 +100,19 @@ export type GraphQLFetcherConfig = {
): never;
};

export type Fetcher = <Doc extends StringDocumentNode>(args: {
type FetcherReturn<Doc extends StringDocumentNode> = ExecutionResult<ResultOf<Doc>> & {
response: Response;
};

type FetcherParams<Doc extends StringDocumentNode> = {
query: Doc | string;
variables: VariablesOf<Doc> | undefined;

fetchOptions: Partial<RequestInit> | undefined;
}) => PromiseOrValue<ExecutionResult<ResultOf<Doc>>>;
};

export type Fetcher = <Doc extends StringDocumentNode>(
args: FetcherParams<Doc>,
) => PromiseOrValue<FetcherReturn<Doc>>;

export const GraphQLExecutionResultSchema = z
.object({
Expand Down Expand Up @@ -175,7 +183,11 @@ export function GraphQLReactQueryClient<

const fetcher: Fetcher =
fetcherConfig ||
async function Fetcher({ query, variables, fetchOptions }) {
async function Fetcher<Doc extends StringDocumentNode>({
query,
variables,
fetchOptions,
}: FetcherParams<Doc>): Promise<FetcherReturn<Doc>> {
const body = JSON.stringify({ query, variables });
const headers = {
'content-type': 'application/json',
Expand Down Expand Up @@ -272,7 +284,12 @@ export function GraphQLReactQueryClient<

const responseJson = GraphQLExecutionResultSchema.safeParse(res.json.value);

if (responseJson.success) return responseJson.data as ExecutionResult<any>;
if (responseJson.success) {
return {
...(responseJson.data as ExecutionResult<any>),
response: res.response,
};
}

const error = new FetchNetworkUnexpectedPayloadShape(
'Network error, unexpected json payload shape',
Expand Down Expand Up @@ -303,6 +320,7 @@ export function GraphQLReactQueryClient<
data = null,
errors,
extensions,
response,
} = await GraphQLReactQuery.fetcher<Doc>({
query,
variables,
Expand Down Expand Up @@ -332,6 +350,7 @@ export function GraphQLReactQueryClient<
query,
variables,
graphqlErrors: errors,
response,
});

for (const err of errors) {
Expand All @@ -352,6 +371,7 @@ export function GraphQLReactQueryClient<
query,
variables,
graphqlErrors: errors,
response,
});

throw error;
Expand All @@ -360,12 +380,14 @@ export function GraphQLReactQueryClient<
const error = new UnexpectedMissingGraphQLData({
query,
variables,
response,
});

graphqlFetcherConfig?.onErrorWithoutData?.({
error,
query,
variables,
response,
});

throw error;
Expand Down Expand Up @@ -955,7 +977,6 @@ export function GraphQLReactQueryClient<
const { nodes: entityStoreNodesSnapshot } = useProxySnapshot(entityStore);

const orderedList = useMemo<Entity[]>(() => {

const currentListFn = latestListFn.current;
const currentUniq = latestUniq.current;
const currentOrder = latestOrder.current;
Expand Down
4 changes: 4 additions & 0 deletions packages/graphql-react-query/src/errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,15 +133,19 @@ export class UnexpectedMissingGraphQLData extends Error {
public name = 'UnexpectedMissingGraphQLData' as const;
public query: string;
public variables: Record<string, unknown> | undefined;
public response: Response;
constructor({
query,
variables,
response,
}: {
query: string;
variables: typeof UnexpectedMissingGraphQLData.prototype.variables;
response: typeof UnexpectedMissingGraphQLData.prototype.response;
}) {
super('Unexpected missing GraphQL data');
this.query = query;
this.variables = variables;
this.response = response;
}
}

0 comments on commit 118cfd5

Please sign in to comment.