Skip to content

Commit cab784b

Browse files
chore(kleros-sdk): use-urql-for-gql-queries
1 parent 52b31a3 commit cab784b

File tree

4 files changed

+85
-56
lines changed

4 files changed

+85
-56
lines changed

kleros-sdk/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
},
4444
"dependencies": {
4545
"@reality.eth/reality-eth-lib": "^3.2.30",
46+
"@urql/core": "^5.0.8",
4647
"mustache": "^4.2.0",
4748
"viem": "^2.21.26",
4849
"zod": "^3.22.4"

kleros-sdk/src/requests/fetchDisputeDetails.ts

+29-29
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { RequestError } from "../errors";
2+
import { cacheExchange, Client, CombinedError, fetchExchange, gql } from "@urql/core";
23

34
type DisputeDetailsQueryResponse = {
45
dispute: {
@@ -11,42 +12,41 @@ type DisputeDetailsQueryResponse = {
1112
};
1213
};
1314

14-
const fetchDisputeDetails = async (endpoint: string, id: bigint): Promise<DisputeDetailsQueryResponse> => {
15-
const query = `
16-
query DisputeDetails($id: ID!) {
17-
dispute(id: $id) {
18-
arbitrated {
19-
id
20-
}
21-
arbitrableChainId
22-
externalDisputeId
23-
templateId
15+
const query = gql`
16+
query DisputeDetails($id: ID!) {
17+
dispute(id: $id) {
18+
arbitrated {
19+
id
2420
}
21+
arbitrableChainId
22+
externalDisputeId
23+
templateId
2524
}
26-
`;
25+
}
26+
`;
27+
28+
const fetchDisputeDetails = async (endpoint: string, id: bigint) => {
2729
const variables = { id: id.toString() };
2830

2931
try {
30-
const response = await fetch(endpoint, {
31-
method: "POST",
32-
headers: {
33-
"Content-Type": "application/json",
34-
},
35-
body: JSON.stringify({ query, variables }),
32+
const client = new Client({
33+
url: endpoint,
34+
exchanges: [cacheExchange, fetchExchange],
3635
});
3736

38-
if (!response.ok) {
39-
const errorData = (await response.json()) as any;
40-
throw new RequestError(
41-
`Error querying Dispute Details: ${errorData?.errors?.[0]?.message || "Unknown error"}`,
42-
endpoint
43-
);
44-
}
45-
46-
const json = (await response.json()) as { data: DisputeDetailsQueryResponse };
47-
return json.data;
48-
} catch (error: any) {
49-
if (error instanceof Error) {
37+
return client
38+
.query<DisputeDetailsQueryResponse>(query, variables)
39+
.toPromise()
40+
.then((res) => {
41+
if (res?.error) {
42+
throw res.error;
43+
}
44+
return res?.data;
45+
});
46+
} catch (error: unknown) {
47+
if (error instanceof CombinedError) {
48+
throw error;
49+
} else if (error instanceof Error) {
5050
throw new RequestError(`Error querying Dispute Details: ${error.message}`, endpoint);
5151
}
5252
throw new RequestError("An unknown error occurred while querying Dispute Details", endpoint);

kleros-sdk/src/requests/fetchDisputeTemplateFromId.ts

+25-27
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { cacheExchange, Client, CombinedError, fetchExchange, gql } from "@urql/core";
12
import { RequestError } from "../errors";
23

34
type DisputeTemplateQueryResponse = {
@@ -6,40 +7,37 @@ type DisputeTemplateQueryResponse = {
67
templateDataMappings: string;
78
};
89
};
9-
10-
const fetchDisputeTemplateFromId = async (endpoint: string, id: number): Promise<DisputeTemplateQueryResponse> => {
11-
const query = `
12-
query DisputeTemplate($id: ID!) {
13-
disputeTemplate(id: $id) {
14-
templateData
15-
templateDataMappings
16-
}
10+
const query = gql`
11+
query DisputeTemplate($id: ID!) {
12+
disputeTemplate(id: $id) {
13+
templateData
14+
templateDataMappings
1715
}
18-
`;
16+
}
17+
`;
1918

19+
const fetchDisputeTemplateFromId = async (endpoint: string, id: number) => {
2020
const variables = { id: id.toString() };
2121

2222
try {
23-
const response = await fetch(endpoint, {
24-
method: "POST",
25-
headers: {
26-
"Content-Type": "application/json",
27-
},
28-
body: JSON.stringify({ query, variables }),
23+
const client = new Client({
24+
url: endpoint,
25+
exchanges: [cacheExchange, fetchExchange],
2926
});
3027

31-
if (!response.ok) {
32-
const errorData = (await response.json()) as any;
33-
throw new RequestError(
34-
`Error querying Dispute Template: ${errorData?.errors?.[0]?.message || "Unknown error"}`,
35-
endpoint
36-
);
37-
}
38-
39-
const json = (await response.json()) as { data: DisputeTemplateQueryResponse };
40-
return json.data;
41-
} catch (error: any) {
42-
if (error instanceof Error) {
28+
return client
29+
.query<DisputeTemplateQueryResponse>(query, variables)
30+
.toPromise()
31+
.then((res) => {
32+
if (res?.error) {
33+
throw res.error;
34+
}
35+
return res?.data;
36+
});
37+
} catch (error: unknown) {
38+
if (error instanceof CombinedError) {
39+
throw error;
40+
} else if (error instanceof Error) {
4341
throw new RequestError(`Error querying Dispute Template: ${error.message}`, endpoint);
4442
}
4543
throw new RequestError("An unknown error occurred while querying Dispute Template", endpoint);

yarn.lock

+30
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ __metadata:
55
version: 8
66
cacheKey: 10
77

8+
"@0no-co/graphql.web@npm:^1.0.5":
9+
version: 1.0.9
10+
resolution: "@0no-co/graphql.web@npm:1.0.9"
11+
peerDependencies:
12+
graphql: ^14.0.0 || ^15.0.0 || ^16.0.0
13+
peerDependenciesMeta:
14+
graphql:
15+
optional: true
16+
checksum: 3caf39263323ba2a51255035e92582573822d213117879e6d1b732e222a8d14fdc703828d46b4a46f86d2027ee08568196f1d64376ee74250fb58dc465698930
17+
languageName: node
18+
linkType: hard
19+
820
"@aashutoshrathi/word-wrap@npm:^1.2.3":
921
version: 1.2.6
1022
resolution: "@aashutoshrathi/word-wrap@npm:1.2.6"
@@ -7901,6 +7913,7 @@ __metadata:
79017913
dependencies:
79027914
"@reality.eth/reality-eth-lib": "npm:^3.2.30"
79037915
"@types/mustache": "npm:^4.2.5"
7916+
"@urql/core": "npm:^5.0.8"
79047917
"@vitest/ui": "npm:^1.1.3"
79057918
mocha: "npm:^10.2.0"
79067919
mustache: "npm:^4.2.0"
@@ -13354,6 +13367,16 @@ __metadata:
1335413367
languageName: node
1335513368
linkType: hard
1335613369

13370+
"@urql/core@npm:^5.0.8":
13371+
version: 5.0.8
13372+
resolution: "@urql/core@npm:5.0.8"
13373+
dependencies:
13374+
"@0no-co/graphql.web": "npm:^1.0.5"
13375+
wonka: "npm:^6.3.2"
13376+
checksum: c973e6e89785ae45ef447726557143ce7bc9d9f5b887297f0b315b2ff546d20bdfb814a4c899644bd5c5814761fc8d75a8ac66f67f3d57a3c2eadd3ec88adb60
13377+
languageName: node
13378+
linkType: hard
13379+
1335713380
"@vitest/expect@npm:1.2.1":
1335813381
version: 1.2.1
1335913382
resolution: "@vitest/expect@npm:1.2.1"
@@ -41239,6 +41262,13 @@ __metadata:
4123941262
languageName: node
4124041263
linkType: hard
4124141264

41265+
"wonka@npm:^6.3.2":
41266+
version: 6.3.4
41267+
resolution: "wonka@npm:6.3.4"
41268+
checksum: 0f102630182828268b57b54102003449b97abbc2483392239baf856a2fca7b72ae9be67c208415124a3d26a320674ed64387e9bf07a8d0badedb5f607d2ccfdc
41269+
languageName: node
41270+
linkType: hard
41271+
4124241272
"word-wrap@npm:^1.2.3, word-wrap@npm:~1.2.3":
4124341273
version: 1.2.3
4124441274
resolution: "word-wrap@npm:1.2.3"

0 commit comments

Comments
 (0)