Skip to content

Commit

Permalink
update version (next)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions[bot] authored and mvantellingen committed Jan 21, 2025
1 parent 3dc1d79 commit 76b6a03
Show file tree
Hide file tree
Showing 9 changed files with 100 additions and 86 deletions.
3 changes: 2 additions & 1 deletion .changeset/pre.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"changesets": [
"beige-donuts-attend",
"five-weeks-talk",
"tough-grapes-hug"
"tough-grapes-hug",
"warm-tomatoes-wonder"
]
}
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
20 changes: 12 additions & 8 deletions src/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down
2 changes: 1 addition & 1 deletion src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type Options = {
*/
defaultHeaders?: Headers | Record<string, string>;

mode?: ModeFlags
mode?: ModeFlags;

/**
* Function to customize creating the documentId from a query
Expand Down
4 changes: 2 additions & 2 deletions src/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@ export interface NextFetchRequestConfig {
}

export const pruneObject = <T>(object: T): Partial<T> => {
const data: Record<string, unknown> = {}
const data: Record<string, unknown> = {};
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;

Expand Down
82 changes: 34 additions & 48 deletions src/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,49 +18,42 @@ it.each(["both", "document", "persistent"])(
documentId: "123",
} as GraphQLRequest<Record<string, unknown>>);

const result: Record<string, unknown> = {}
const result: Record<string, unknown> = {};
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) => {
Expand All @@ -72,39 +65,32 @@ it.each(["both", "document", "persistent"])(
documentId: "123",
} as GraphQLRequest<Record<string, unknown>>);

const result: Record<string, unknown> = {}
const result: Record<string, unknown> = {};
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;
}
}
},
);

49 changes: 28 additions & 21 deletions src/server.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -199,7 +203,7 @@ describe("gqlServerFetch", () => {
}),
cache: "force-cache",
next: { revalidate: 900 },
signal: expect.any(AbortSignal)
signal: expect.any(AbortSignal),
},
);
});
Expand All @@ -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
Expand Down
18 changes: 14 additions & 4 deletions src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
Expand Down Expand Up @@ -227,10 +232,15 @@ const gqlPersistedQuery = async <TVariables>(
* @param response Fetch response object
* @returns GraphQL response body
*/
const parseResponse = async (request: GraphQLRequest<unknown>, response: Response) => {
const parseResponse = async (
request: GraphQLRequest<unknown>,
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();
Expand Down

0 comments on commit 76b6a03

Please sign in to comment.