Skip to content

Commit

Permalink
Upgrade to urql v4
Browse files Browse the repository at this point in the history
 - removes the deprecated exchanges that are built into the client now
 - adds the new mandatory properties to the `OperationResult` type
  • Loading branch information
airhorns committed May 16, 2023
1 parent 66c0ebd commit b9d3483
Show file tree
Hide file tree
Showing 15 changed files with 142 additions and 87 deletions.
9 changes: 4 additions & 5 deletions packages/api-client-core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@
},
"dependencies": {
"@opentelemetry/api": "^1.4.0",
"@urql/core": "^3.0.1",
"@urql/exchange-multipart-fetch": "^1.0.1",
"cross-fetch": "^3.0.6",
"@urql/core": "^4.0.7",
"cross-fetch": "^3.1.5",
"gql-query-builder": "^3.7.2",
"graphql": "^16.5.0",
"graphql-ws": "^5.5.5",
"isomorphic-ws": "^4.0.1",
"isomorphic-ws": "^5.0.0",
"lodash.clonedeep": "^4.5.0",
"lodash.isequal": "^4.5.0",
"ws": "^8.11.0"
"ws": "^8.13.0"
},
"devDependencies": {
"@types/lodash.clonedeep": "^4.5.6",
Expand Down
30 changes: 30 additions & 0 deletions packages/api-client-core/spec/support.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ describe("support utilities", () => {
{
operation: null as any,
data: { foo: { bar: "baz" } },
stale: false,
hasNext: false,
},
["foo", "bar"]
)
Expand All @@ -29,6 +31,8 @@ describe("support utilities", () => {
operation: null as any,
data: null,
error: new CombinedError({ networkError: new Error("foobar") }),
stale: false,
hasNext: false,
},

["foo", "bar"]
Expand All @@ -43,6 +47,8 @@ describe("support utilities", () => {
operation: null as any,
data: null,
error: new CombinedError({ networkError: new Error("foobar") }),
stale: false,
hasNext: false,
},

["foo", "bar"]
Expand All @@ -60,6 +66,8 @@ describe("support utilities", () => {
data: null,
// @ts-expect-error an array of network errors doesn't match urql's types, but we've observed it at runtime
error: new CombinedError({ networkError: [new Error("foo"), new Error("foo")] }),
stale: false,
hasNext: false,
},

["foo", "bar"]
Expand All @@ -77,6 +85,8 @@ describe("support utilities", () => {
operation: null as any,
data: null,
error: new CombinedError({ graphQLErrors: [new Error("foo")] }),
stale: false,
hasNext: false,
},

["foo", "bar"]
Expand All @@ -91,6 +101,8 @@ describe("support utilities", () => {
operation: null as any,
data: null,
error: new CombinedError({ graphQLErrors: [new Error("foo"), "bar"] }),
stale: false,
hasNext: false,
},

["foo", "bar"]
Expand All @@ -108,6 +120,8 @@ describe("support utilities", () => {
operation: null as any,
data: null,
error: new CombinedError({ graphQLErrors: [new GraphQLError("inner graphql error")] }),
stale: false,
hasNext: false,
},

["foo", "bar"]
Expand All @@ -122,6 +136,8 @@ describe("support utilities", () => {
operation: null as any,
data: null,
error: new CombinedError({ response: { whatever: true } }),
stale: false,
hasNext: false,
},

["foo", "bar"]
Expand All @@ -138,6 +154,8 @@ describe("support utilities", () => {
operation: null as any,
data: null,
error: new CombinedError({ graphQLErrors: [new GraphQLError("inner graphql error")] }),
stale: false,
hasNext: false,
},
["foo", "bar"]
)
Expand All @@ -150,6 +168,8 @@ describe("support utilities", () => {
operation: null as any,
data: undefined,
error: undefined,
stale: false,
hasNext: false,
},
["foo", "bar"]
);
Expand Down Expand Up @@ -194,6 +214,8 @@ describe("support utilities", () => {
{
operation: null as any,
data: { createWidget: { success: true, errors: null, widget: { bar: "baz" } } },
stale: false,
hasNext: false,
},
["createWidget"]
)
Expand All @@ -212,6 +234,8 @@ describe("support utilities", () => {
widget: null,
},
},
stale: false,
hasNext: false,
},

["createWidget"]
Expand All @@ -231,6 +255,8 @@ describe("support utilities", () => {
widget: null,
},
},
stale: false,
hasNext: false,
},

["createWidget"]
Expand Down Expand Up @@ -266,6 +292,8 @@ describe("support utilities", () => {
widget: null,
},
},
stale: false,
hasNext: false,
},
["createWidget"]
);
Expand Down Expand Up @@ -302,6 +330,8 @@ describe("support utilities", () => {
widget: null,
},
},
stale: false,
hasNext: false,
},
["createWidget"]
);
Expand Down
17 changes: 9 additions & 8 deletions packages/api-client-core/src/GadgetConnection.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { ClientOptions, RequestPolicy } from "@urql/core";
import { Client, cacheExchange, dedupExchange, subscriptionExchange } from "@urql/core";
import { multipartFetchExchange } from "@urql/exchange-multipart-fetch";
import { Client, cacheExchange, fetchExchange, subscriptionExchange } from "@urql/core";
import fetchPolyfill from "cross-fetch";
import type { ExecutionResult } from "graphql";
import type { Sink, Client as SubscriptionClient, ClientOptions as SubscriptionClientOptions } from "graphql-ws";
Expand Down Expand Up @@ -214,10 +213,11 @@ export class GadgetConnection {
requestPolicy: "network-only", // skip any cached data during transactions
exchanges: [
subscriptionExchange({
forwardSubscription(operation) {
forwardSubscription(request) {
const input = { ...request, query: request.query || "" };
return {
subscribe: (sink) => {
const dispose = subscriptionClient!.subscribe(operation, sink as Sink<ExecutionResult>);
const dispose = subscriptionClient!.subscribe(input, sink as Sink<ExecutionResult>);
return {
unsubscribe: dispose,
};
Expand Down Expand Up @@ -328,20 +328,21 @@ export class GadgetConnection {
}

private newBaseClient() {
const exchanges = [dedupExchange];
const exchanges = [];

// apply urql's default caching behaviour when client side (but skip it server side)
if (typeof window != "undefined") {
exchanges.push(cacheExchange);
}

exchanges.push(
multipartFetchExchange,
fetchExchange,
subscriptionExchange({
forwardSubscription: (operation) => {
forwardSubscription: (request) => {
return {
subscribe: (sink) => {
const dispose = this.baseSubscriptionClient.subscribe(operation, sink as Sink<ExecutionResult>);
const input = { ...request, query: request.query || "" };
const dispose = this.baseSubscriptionClient.subscribe(input, sink as Sink<ExecutionResult>);
return {
unsubscribe: dispose,
};
Expand Down
2 changes: 1 addition & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
"dependencies": {
"@gadgetinc/api-client-core": "^0.13.4",
"deep-equal": "^2.2.0",
"urql": "^3.0.3"
"urql": "^4.0.2"
},
"devDependencies": {
"@testing-library/react": "^13.4.0",
Expand Down
10 changes: 10 additions & 0 deletions packages/react/spec/useAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,8 @@ describe("useAction", () => {
},
},
},
stale: false,
hasNext: false,
});

await act(async () => {
Expand Down Expand Up @@ -138,6 +140,8 @@ describe("useAction", () => {
},
},
},
stale: false,
hasNext: false,
});

await act(async () => {
Expand Down Expand Up @@ -181,6 +185,8 @@ describe("useAction", () => {
},
},
},
stale: false,
hasNext: false,
});

await act(async () => {
Expand Down Expand Up @@ -219,6 +225,8 @@ describe("useAction", () => {
},
},
},
stale: false,
hasNext: false,
});

await act(async () => {
Expand Down Expand Up @@ -254,6 +262,8 @@ describe("useAction", () => {
},
},
},
stale: false,
hasNext: false,
});

await act(async () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/react/spec/useBulkAction.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ describe("useBulkAction", () => {
],
},
},
stale: false,
hasNext: false,
});

await act(async () => {
Expand Down Expand Up @@ -132,6 +134,8 @@ describe("useBulkAction", () => {
widgets: null,
},
},
stale: false,
hasNext: false,
});

await act(async () => {
Expand Down Expand Up @@ -169,6 +173,8 @@ describe("useBulkAction", () => {
],
},
},
stale: false,
hasNext: false,
});

await act(async () => {
Expand Down
6 changes: 6 additions & 0 deletions packages/react/spec/useFindBy.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ describe("useFindBy", () => {
},
},
},
stale: false,
hasNext: false,
});

expect(result.current[0].data!.id).toEqual("123");
Expand Down Expand Up @@ -87,6 +89,8 @@ describe("useFindBy", () => {
},
},
},
stale: false,
hasNext: false,
});

expect(result.current[0].data).toBeFalsy();
Expand Down Expand Up @@ -115,6 +119,8 @@ describe("useFindBy", () => {
},
},
},
stale: false,
hasNext: false,
});

const data = result.current[0].data;
Expand Down
10 changes: 10 additions & 0 deletions packages/react/spec/useFindMany.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ describe("useFindMany", () => {
},
},
},
stale: false,
hasNext: false,
});

expect(result.current[0].data![0].id).toEqual("123");
Expand Down Expand Up @@ -96,6 +98,8 @@ describe("useFindMany", () => {
},
},
},
stale: false,
hasNext: false,
});

expect(result.current[0].data![0].id).toEqual("123");
Expand Down Expand Up @@ -130,6 +134,8 @@ describe("useFindMany", () => {
},
},
},
stale: false,
hasNext: false,
});

expect(result.current[0].data![0].id).toEqual("def");
Expand Down Expand Up @@ -161,6 +167,8 @@ describe("useFindMany", () => {
},
},
},
stale: false,
hasNext: false,
});

expect(result.current[0].data!.length).toEqual(0);
Expand Down Expand Up @@ -192,6 +200,8 @@ describe("useFindMany", () => {
},
},
},
stale: false,
hasNext: false,
});

const beforeObject = result.current[0];
Expand Down
6 changes: 6 additions & 0 deletions packages/react/spec/useFindOne.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ describe("useFindOne", () => {
email: "test@test.com",
},
},
stale: false,
hasNext: false,
});

expect(result.current[0].data!.id).toEqual("123");
Expand All @@ -72,6 +74,8 @@ describe("useFindOne", () => {
data: {
user: null,
},
stale: false,
hasNext: false,
});

expect(result.current[0].data).toBeFalsy();
Expand All @@ -98,6 +102,8 @@ describe("useFindOne", () => {
email: "test@test.com",
},
},
stale: false,
hasNext: false,
});

const beforeObject = result.current[0];
Expand Down
Loading

0 comments on commit b9d3483

Please sign in to comment.