Skip to content

Commit f1c03d6

Browse files
authored
Merge branch 'dev' into feat(subgraph)/update-subgraph-to-interfaces
2 parents 7562f1b + 5fa615e commit f1c03d6

19 files changed

+4822
-3575
lines changed

web/codegen.ts

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import type { CodegenConfig } from "@graphql-codegen/cli";
2+
3+
const config: CodegenConfig = {
4+
overwrite: true,
5+
schema: ["https://api.thegraph.com/subgraphs/name/alcercu/kleroscoretest"],
6+
documents: "./src/hooks/queries/*.ts",
7+
generates: {
8+
"./src/graphql/": {
9+
preset: "client",
10+
},
11+
},
12+
};
13+
14+
export default config;

web/codegen.yaml

Lines changed: 0 additions & 7 deletions
This file was deleted.

web/package.json

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@
3535
},
3636
"prettier": "@kleros/kleros-v2-prettier-config",
3737
"devDependencies": {
38+
"@graphql-codegen/cli": "^4.0.1",
39+
"@graphql-codegen/client-preset": "^4.0.1",
3840
"@kleros/kleros-v2-eslint-config": "workspace:^",
3941
"@kleros/kleros-v2-prettier-config": "workspace:^",
4042
"@kleros/kleros-v2-tsconfig": "workspace:^",
@@ -57,9 +59,6 @@
5759
"typescript": "^4.9.5"
5860
},
5961
"dependencies": {
60-
"@graphql-codegen/cli": "^4.0.1",
61-
"@graphql-codegen/typescript": "^2.8.8",
62-
"@graphql-codegen/typescript-operations": "^2.5.13",
6362
"@kleros/kleros-v2-contracts": "workspace:^",
6463
"@kleros/ui-components-library": "^1.9.2",
6564
"@sentry/react": "^7.55.2",

web/src/graphql/fragment-masking.ts

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { ResultOf, DocumentTypeDecoration, TypedDocumentNode } from "@graphql-typed-document-node/core";
2+
import { FragmentDefinitionNode } from "graphql";
3+
import { Incremental } from "./graphql";
4+
5+
export type FragmentType<TDocumentType extends DocumentTypeDecoration<any, any>> =
6+
TDocumentType extends DocumentTypeDecoration<infer TType, any>
7+
? [TType] extends [{ " $fragmentName"?: infer TKey }]
8+
? TKey extends string
9+
? { " $fragmentRefs"?: { [key in TKey]: TType } }
10+
: never
11+
: never
12+
: never;
13+
14+
// return non-nullable if `fragmentType` is non-nullable
15+
export function useFragment<TType>(
16+
_documentNode: DocumentTypeDecoration<TType, any>,
17+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>>
18+
): TType;
19+
// return nullable if `fragmentType` is nullable
20+
export function useFragment<TType>(
21+
_documentNode: DocumentTypeDecoration<TType, any>,
22+
fragmentType: FragmentType<DocumentTypeDecoration<TType, any>> | null | undefined
23+
): TType | null | undefined;
24+
// return array of non-nullable if `fragmentType` is array of non-nullable
25+
export function useFragment<TType>(
26+
_documentNode: DocumentTypeDecoration<TType, any>,
27+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
28+
): ReadonlyArray<TType>;
29+
// return array of nullable if `fragmentType` is array of nullable
30+
export function useFragment<TType>(
31+
_documentNode: DocumentTypeDecoration<TType, any>,
32+
fragmentType: ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>> | null | undefined
33+
): ReadonlyArray<TType> | null | undefined;
34+
export function useFragment<TType>(
35+
_documentNode: DocumentTypeDecoration<TType, any>,
36+
fragmentType:
37+
| FragmentType<DocumentTypeDecoration<TType, any>>
38+
| ReadonlyArray<FragmentType<DocumentTypeDecoration<TType, any>>>
39+
| null
40+
| undefined
41+
): TType | ReadonlyArray<TType> | null | undefined {
42+
return fragmentType as any;
43+
}
44+
45+
export function makeFragmentData<F extends DocumentTypeDecoration<any, any>, FT extends ResultOf<F>>(
46+
data: FT,
47+
_fragment: F
48+
): FragmentType<F> {
49+
return data as FragmentType<F>;
50+
}
51+
export function isFragmentReady<TQuery, TFrag>(
52+
queryNode: DocumentTypeDecoration<TQuery, any>,
53+
fragmentNode: TypedDocumentNode<TFrag>,
54+
data: FragmentType<TypedDocumentNode<Incremental<TFrag>, any>> | null | undefined
55+
): data is FragmentType<typeof fragmentNode> {
56+
const deferredFields = (queryNode as { __meta__?: { deferredFields: Record<string, (keyof TFrag)[]> } }).__meta__
57+
?.deferredFields;
58+
59+
if (!deferredFields) return true;
60+
61+
const fragDef = fragmentNode.definitions[0] as FragmentDefinitionNode | undefined;
62+
const fragName = fragDef?.name?.value;
63+
64+
const fields = (fragName && deferredFields[fragName]) || [];
65+
return fields.length > 0 && fields.every((field) => data && field in data);
66+
}

0 commit comments

Comments
 (0)