Skip to content

Commit 61f040c

Browse files
committed
feat(urql-exchange): support urql v4
1 parent b010be5 commit 61f040c

File tree

5 files changed

+42
-31
lines changed

5 files changed

+42
-31
lines changed

.changeset/grumpy-pans-end.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/executor-urql-exchange': patch
3+
---
4+
5+
Support urql v4

packages/executors/urql-exchange/package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,12 @@
5353
"tslib": "^2.4.0"
5454
},
5555
"devDependencies": {
56-
"@urql/core": "3.1.1",
57-
"wonka": "6.2.3"
56+
"@urql/core": "4.0.2",
57+
"wonka": "6.3.1"
5858
},
5959
"peerDependencies": {
6060
"graphql": "^15.2.0 || ^16.0.0",
61-
"@urql/core": "^3.0.0",
61+
"@urql/core": "^3.0.0 || ^4.0.0",
6262
"wonka": "^6.0.0"
6363
},
6464
"type": "module"

packages/executors/urql-exchange/src/index.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import {
88
mergeResultPatch,
99
Operation,
1010
OperationResult,
11-
getOperationName,
1211
OperationContext,
1312
ExchangeIO,
1413
AnyVariables,
@@ -21,15 +20,12 @@ export function executorExchange(executor: Executor): Exchange {
2120
function makeYogaSource<TData extends Record<string, any>>(
2221
operation: Operation<TData>
2322
): Source<OperationResult<TData>> {
24-
const operationName = getOperationName(operation.query);
25-
2623
const extraFetchOptions =
2724
typeof operation.context.fetchOptions === 'function'
2825
? operation.context.fetchOptions()
2926
: operation.context.fetchOptions;
3027
const executionRequest: ExecutionRequest<any, OperationContext> = {
3128
document: operation.query,
32-
operationName,
3329
operationType: operation.kind as OperationTypeNode,
3430
variables: operation.variables,
3531
context: operation.context,
@@ -44,18 +40,22 @@ export function executorExchange(executor: Executor): Exchange {
4440
return make<OperationResult<TData>>(observer => {
4541
let ended = false;
4642
Promise.resolve(executor(executionRequest))
47-
.then(async (result: ExecutionResult | AsyncIterable<ExecutionResult>) => {
43+
.then(async result => {
4844
if (ended || !result) {
4945
return;
5046
}
5147
if (!isAsyncIterable(result)) {
52-
observer.next(makeResult(operation, result));
48+
observer.next(makeResult(operation, result as ExecutionResult));
5349
} else {
5450
let prevResult: OperationResult<TData, AnyVariables> | null = null;
5551

5652
for await (const value of result) {
5753
if (value) {
58-
prevResult = prevResult ? mergeResultPatch(prevResult, value) : makeResult(operation, value);
54+
if (prevResult && value.incremental) {
55+
prevResult = mergeResultPatch(prevResult, value as ExecutionResult);
56+
} else {
57+
prevResult = makeResult(operation, value as ExecutionResult);
58+
}
5959
observer.next(prevResult);
6060
}
6161
if (ended) {

packages/executors/urql-exchange/tests/browser-urql-exchange.spec.ts

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ describe('URQL Yoga Exchange', () => {
1010
it('skips', () => {});
1111
return;
1212
}
13+
const aCharCode = 'a'.charCodeAt(0);
1314
const yoga = createYoga({
1415
logging: false,
1516
maskedErrors: false,
@@ -23,7 +24,7 @@ describe('URQL Yoga Exchange', () => {
2324
readFile(file: File!): String!
2425
}
2526
type Subscription {
26-
time: String
27+
alphabet: String
2728
}
2829
`,
2930
resolvers: {
@@ -34,11 +35,13 @@ describe('URQL Yoga Exchange', () => {
3435
readFile: (_, args: { file: File }) => args.file.text(),
3536
},
3637
Subscription: {
37-
time: {
38+
alphabet: {
3839
async *subscribe() {
40+
let i = 0;
3941
while (true) {
40-
await new Promise(resolve => setTimeout(resolve, 1000));
41-
yield new Date().toISOString();
42+
yield String.fromCharCode(aCharCode + i);
43+
await new Promise(resolve => setTimeout(resolve, 300));
44+
i++;
4245
}
4346
},
4447
resolve: str => str,
@@ -81,8 +84,8 @@ describe('URQL Yoga Exchange', () => {
8184
const observable = pipe(
8285
client.subscription(
8386
/* GraphQL */ `
84-
subscription Time {
85-
time
87+
subscription Alphabet {
88+
alphabet
8689
}
8790
`,
8891
{}
@@ -95,27 +98,24 @@ describe('URQL Yoga Exchange', () => {
9598
await new Promise<void>((resolve, reject) => {
9699
const subscription = observable.subscribe({
97100
next: (result: ExecutionResult) => {
98-
collectedValues.push(result.data?.time as string);
101+
collectedValues.push(result.data?.alphabet as string);
99102
i++;
100103
if (i > 2) {
101104
subscription.unsubscribe();
102105
resolve();
103106
}
104107
},
105108
complete: () => {
109+
console.log('bitti');
106110
resolve();
107111
},
108112
error: (error: Error) => {
109113
reject(error);
110114
},
111115
});
112116
});
113-
expect(collectedValues.length).toBe(3);
117+
expect(collectedValues).toEqual(['a', 'b', 'c']);
114118
expect(i).toBe(3);
115-
const now = new Date();
116-
for (const value of collectedValues) {
117-
expect(new Date(value).getFullYear()).toBe(now.getFullYear());
118-
}
119119
});
120120
it('should handle file uploads correctly', async () => {
121121
const query = /* GraphQL */ `

yarn.lock

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
# yarn lockfile v1
33

44

5+
"@0no-co/graphql.web@^1.0.0":
6+
version "1.0.0"
7+
resolved "https://registry.yarnpkg.com/@0no-co/graphql.web/-/graphql.web-1.0.0.tgz#af3f5e3b9359d2f011bc37a93654348b92d4d893"
8+
integrity sha512-JBq2pWyDchE1vVjj/+c4dzZ8stbpew4RrzpZ3vYdn1WJFGHfYg6YIX1fDdMKtSXJJM9FUlsoDOxemr9WMM2p+A==
9+
510
"@algolia/[email protected]":
611
version "1.8.3"
712
resolved "https://registry.yarnpkg.com/@algolia/autocomplete-core/-/autocomplete-core-1.8.3.tgz#97354875342a4ece9491017517bd7c7f69adec61"
@@ -3315,12 +3320,13 @@
33153320
"@typescript-eslint/types" "5.57.1"
33163321
eslint-visitor-keys "^3.3.0"
33173322

3318-
"@urql/core@3.1.1":
3319-
version "3.1.1"
3320-
resolved "https://registry.yarnpkg.com/@urql/core/-/core-3.1.1.tgz#a49cd572360d01f2469a786b294fba2269a65e53"
3321-
integrity sha512-Mnxtq4I4QeFJsgs7Iytw+HyhiGxISR6qtyk66c9tipozLZ6QVxrCiUPF2HY4BxNIabaxcp+rivadvm8NAnXj4Q==
3323+
"@urql/core@4.0.2":
3324+
version "4.0.2"
3325+
resolved "https://registry.yarnpkg.com/@urql/core/-/core-4.0.2.tgz#cadcebe8bae3447847a005ca368af57c2637969e"
3326+
integrity sha512-GrJhMDqFP0URS47h6tMSHN/hmeTB1BNWnlXkWTxZo+b2ZB1+E9rgsuG2VTfaErXtZySGLwyCsDHtT+kZLN2cMA==
33223327
dependencies:
3323-
wonka "^6.1.2"
3328+
"@0no-co/graphql.web" "^1.0.0"
3329+
wonka "^6.3.0"
33243330

33253331
"@vercel/ncc@^0.36.0":
33263332
version "0.36.1"
@@ -12337,10 +12343,10 @@ which@^2.0.1:
1233712343
dependencies:
1233812344
isexe "^2.0.0"
1233912345

12340-
wonka@6.2.3, wonka@^6.1.2:
12341-
version "6.2.3"
12342-
resolved "https://registry.yarnpkg.com/wonka/-/wonka-6.2.3.tgz#88f7852a23a3d53bca7411c70d66e9ce8f93a366"
12343-
integrity sha512-EFOYiqDeYLXSzGYt2X3aVe9Hq1XJG+Hz/HjTRRT4dZE9q95khHl5+7pzUSXI19dbMO1/2UMrTf7JT7/7JrSQSQ==
12346+
wonka@6.3.1, wonka@^6.3.0:
12347+
version "6.3.1"
12348+
resolved "https://registry.yarnpkg.com/wonka/-/wonka-6.3.1.tgz#315b2a91e134c4032980836e6d97e13d0927634d"
12349+
integrity sha512-nJyGPcjuBiaLFn8QAlrHd+QjV9AlPO7snOWAhgx6aX0nQLMV6Wi0nqfrkmsXIH0efngbDOroOz2QyLnZMF16Hw==
1234412350

1234512351
word-wrap@^1.2.3:
1234612352
version "1.2.3"

0 commit comments

Comments
 (0)