-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Upgrade to urql v4 under the hood #171
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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"; | ||
|
@@ -101,7 +100,7 @@ export class GadgetConnection { | |
} else { | ||
this._fetchImplementation = fetchPolyfill; | ||
} | ||
this.websocketImplementation = options.websocketImplementation ?? WebSocket; | ||
this.websocketImplementation = options.websocketImplementation ?? globalThis?.WebSocket ?? WebSocket; | ||
this.websocketsEndpoint = options.websocketsEndpoint ?? options.endpoint + "/batch"; | ||
this.websocketsEndpoint = this.websocketsEndpoint.replace(/^http/, "ws"); | ||
this.environment = options.environment ?? "Development"; | ||
|
@@ -215,10 +214,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, | ||
}; | ||
|
@@ -329,20 +329,21 @@ export class GadgetConnection { | |
} | ||
|
||
private newBaseClient() { | ||
const exchanges = [dedupExchange, urlParamExchange]; | ||
const exchanges = [urlParamExchange]; | ||
|
||
// 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>); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This type changed between versions of urql -- the query is now options for some reason, so this maps to the kind of object that graphql-ws is expecting |
||
return { | ||
unsubscribe: dispose, | ||
}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"name": "@gadgetinc/react", | ||
"version": "0.12.5", | ||
"version": "0.12.6", | ||
"source": "src/index.ts", | ||
"main": "dist/src/index.js", | ||
"types": "dist/src/index.d.ts", | ||
|
@@ -19,9 +19,9 @@ | |
"prerelease": "gitpkg publish" | ||
}, | ||
"dependencies": { | ||
"@gadgetinc/api-client-core": "^0.13.10", | ||
"@gadgetinc/api-client-core": "^0.13.14", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Our react bindings now need the newest api-client-core which also depends on urql 4, as well as depending on urql 4 themselves |
||
"deep-equal": "^2.2.0", | ||
"urql": "^3.0.3" | ||
"urql": "^4.0.4" | ||
}, | ||
"devDependencies": { | ||
"@gadgetinc/api-client-core": "workspace:*", | ||
|
@@ -30,7 +30,7 @@ | |
"@types/lodash": "^4.14.191", | ||
"@types/react": "^18.2.9", | ||
"@types/react-dom": "^18.2.4", | ||
"@urql/core": "^3.0.1", | ||
"@urql/core": "^4.0.10", | ||
"conditional-type-checks": "^1.0.6", | ||
"graphql": "16.5.0", | ||
"lodash": "^4.17.21", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I also upgraded the isomorphic-ws library, which stopped monkeypatching the global. This makes our tests keep working.