Skip to content

Commit 0562712

Browse files
feat(kleros-sdk): gql-client-caching
1 parent cab784b commit 0562712

File tree

3 files changed

+24
-12
lines changed

3 files changed

+24
-12
lines changed

kleros-sdk/src/requests/fetchDisputeDetails.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { RequestError } from "../errors";
2-
import { cacheExchange, Client, CombinedError, fetchExchange, gql } from "@urql/core";
2+
import { CombinedError, gql } from "@urql/core";
3+
import getClient from "./gqlClient";
34

45
type DisputeDetailsQueryResponse = {
56
dispute: {
@@ -29,11 +30,7 @@ const fetchDisputeDetails = async (endpoint: string, id: bigint) => {
2930
const variables = { id: id.toString() };
3031

3132
try {
32-
const client = new Client({
33-
url: endpoint,
34-
exchanges: [cacheExchange, fetchExchange],
35-
});
36-
33+
const client = getClient(endpoint);
3734
return client
3835
.query<DisputeDetailsQueryResponse>(query, variables)
3936
.toPromise()

kleros-sdk/src/requests/fetchDisputeTemplateFromId.ts

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1-
import { cacheExchange, Client, CombinedError, fetchExchange, gql } from "@urql/core";
1+
import { CombinedError, gql } from "@urql/core";
22
import { RequestError } from "../errors";
3+
import getClient from "./gqlClient";
34

45
type DisputeTemplateQueryResponse = {
56
disputeTemplate: {
@@ -20,11 +21,7 @@ const fetchDisputeTemplateFromId = async (endpoint: string, id: number) => {
2021
const variables = { id: id.toString() };
2122

2223
try {
23-
const client = new Client({
24-
url: endpoint,
25-
exchanges: [cacheExchange, fetchExchange],
26-
});
27-
24+
const client = getClient(endpoint);
2825
return client
2926
.query<DisputeTemplateQueryResponse>(query, variables)
3027
.toPromise()

kleros-sdk/src/requests/gqlClient.ts

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { cacheExchange, Client, fetchExchange } from "@urql/core";
2+
3+
const clients = new Map<string, Client>();
4+
5+
const getClient = (endpoint: string) => {
6+
let client = clients.get(endpoint);
7+
8+
if (!client) {
9+
client = new Client({
10+
url: endpoint,
11+
exchanges: [cacheExchange, fetchExchange],
12+
});
13+
clients.set(endpoint, client);
14+
}
15+
return client;
16+
};
17+
18+
export default getClient;

0 commit comments

Comments
 (0)