Skip to content
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

feat(web-devtools): shift-gql-request-to-server-side #1690

Draft
wants to merge 2 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions web-devtools/.env.local.example
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# Do not enter sensitive information here.
export NEXT_PUBLIC_ALCHEMY_API_KEY=
export NEXT_PUBLIC_DEPLOYMENT=devnet
export NEXT_PUBLIC_CORE_SUBGRAPH=https://api.studio.thegraph.com/query/61738/kleros-v2-core-devnet/version/latest
export NEXT_PUBLIC_DRT_ARBSEPOLIA_SUBGRAPH=https://api.studio.thegraph.com/query/61738/kleros-v2-drt-arbisep-devnet/version/latest
export CORE_SUBGRAPH=https://api.studio.thegraph.com/query/61738/kleros-v2-core-devnet/version/latest
export DRT_ARBSEPOLIA_SUBGRAPH=https://api.studio.thegraph.com/query/61738/kleros-v2-drt-arbisep-devnet/version/latest
2 changes: 1 addition & 1 deletion web-devtools/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { CodegenConfig } from "@graphql-codegen/cli";

import { getGraphqlUrl } from "./src/utils/getGraphqlUrl";
import { getGraphqlUrl } from "./src/actions/getGraphqlUrl";

const config: CodegenConfig = {
overwrite: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
"use server";
import { arbitrumSepolia, arbitrum } from "wagmi/chains";

import { DEFAULT_CHAIN } from "../consts/chains";

export const getGraphqlUrl = (isDisputeTemplate = false, chainId: number = DEFAULT_CHAIN) => {
const CHAINID_TO_DISPUTE_TEMPLATE_SUBGRAPH: { [key: number]: string } = {
[arbitrumSepolia.id]:
process.env.NEXT_PUBLIC_DRT_ARBSEPOLIA_SUBGRAPH ??
"Environment variable NEXT_PUBLIC_DRT_ARBSEPOLIA_SUBGRAPH not set.",
process.env.DRT_ARBSEPOLIA_SUBGRAPH ?? "Environment variable NEXT_PUBLIC_DRT_ARBSEPOLIA_SUBGRAPH not set.",
[arbitrum.id]:
process.env.NEXT_PUBLIC_DRT_ARBMAINNET_SUBGRAPH ??
"Environment variable NEXT_PUBLIC_DRT_ARBMAINNET_SUBGRAPH not set.",
process.env.ARBMAINNET_SUBGRAPH ?? "Environment variable NEXT_PUBLIC_DRT_ARBMAINNET_SUBGRAPH not set.",
};
const coreUrl = process.env.NEXT_PUBLIC_CORE_SUBGRAPH ?? "Environment variables NEXT_PUBLIC_CORE_SUBGRAPH not set.";
const coreUrl = process.env.CORE_SUBGRAPH ?? "Environment variables NEXT_PUBLIC_CORE_SUBGRAPH not set.";
return isDisputeTemplate ? CHAINID_TO_DISPUTE_TEMPLATE_SUBGRAPH[chainId] : coreUrl;
};
16 changes: 16 additions & 0 deletions web-devtools/src/actions/gqlFetcher.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
"use server";

import { TypedDocumentNode } from "@graphql-typed-document-node/core";
import { request } from "graphql-request";

import { getGraphqlUrl } from "./getGraphqlUrl";

export const gqlRequest = async (
document: TypedDocumentNode<any, any>,
variables: Record<string, any>,
chainId?: number,
isDisputeTemplate?: boolean
) => {
const url = await getGraphqlUrl(isDisputeTemplate, chainId);
return await request(url, document, variables);
};
6 changes: 2 additions & 4 deletions web-devtools/src/context/GraphqlBatcher.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@ import React, { useMemo, createContext, useContext } from "react";

import { TypedDocumentNode } from "@graphql-typed-document-node/core";
import { create, windowedFiniteBatchScheduler, Batcher } from "@yornaath/batshit";
import { request } from "graphql-request";
import { gqlRequest } from "actions/gqlFetcher";

import { debounceErrorToast } from "utils/debounceErrorToast";
import { getGraphqlUrl } from "utils/getGraphqlUrl";

interface IGraphqlBatcher {
graphqlBatcher: Batcher<any, IQuery>;
Expand All @@ -23,9 +22,8 @@ const Context = createContext<IGraphqlBatcher | undefined>(undefined);

const fetcher = async (queries: IQuery[]) => {
const promises = queries.map(async ({ id, document, variables, isDisputeTemplate, chainId }) => {
const url = getGraphqlUrl(isDisputeTemplate ?? false, chainId);
try {
return request(url, document, variables).then((result) => ({ id, result }));
return await gqlRequest(document, variables, chainId, isDisputeTemplate).then((result) => ({ id, result }));
} catch (error) {
console.error("Graph error: ", { error });
debounceErrorToast("Graph query error: failed to fetch data.");
Expand Down
Loading