Skip to content

Commit

Permalink
feat(kleros-sdk): gql-client-caching
Browse files Browse the repository at this point in the history
  • Loading branch information
Harman-singh-waraich committed Oct 24, 2024
1 parent cab784b commit 0562712
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 12 deletions.
9 changes: 3 additions & 6 deletions kleros-sdk/src/requests/fetchDisputeDetails.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { RequestError } from "../errors";
import { cacheExchange, Client, CombinedError, fetchExchange, gql } from "@urql/core";
import { CombinedError, gql } from "@urql/core";
import getClient from "./gqlClient";

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

try {
const client = new Client({
url: endpoint,
exchanges: [cacheExchange, fetchExchange],
});

const client = getClient(endpoint);
return client
.query<DisputeDetailsQueryResponse>(query, variables)
.toPromise()
Expand Down
9 changes: 3 additions & 6 deletions kleros-sdk/src/requests/fetchDisputeTemplateFromId.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { cacheExchange, Client, CombinedError, fetchExchange, gql } from "@urql/core";
import { CombinedError, gql } from "@urql/core";
import { RequestError } from "../errors";
import getClient from "./gqlClient";

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

try {
const client = new Client({
url: endpoint,
exchanges: [cacheExchange, fetchExchange],
});

const client = getClient(endpoint);
return client
.query<DisputeTemplateQueryResponse>(query, variables)
.toPromise()
Expand Down
18 changes: 18 additions & 0 deletions kleros-sdk/src/requests/gqlClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { cacheExchange, Client, fetchExchange } from "@urql/core";

const clients = new Map<string, Client>();

const getClient = (endpoint: string) => {
let client = clients.get(endpoint);

if (!client) {
client = new Client({
url: endpoint,
exchanges: [cacheExchange, fetchExchange],
});
clients.set(endpoint, client);
}
return client;
};

export default getClient;

0 comments on commit 0562712

Please sign in to comment.