Skip to content

Commit a59dc99

Browse files
authored
Merge pull request #44 from thefrontside/pc/paginate-all
paginate the all query
2 parents b70293c + e4b2465 commit a59dc99

File tree

12 files changed

+120
-65
lines changed

12 files changed

+120
-65
lines changed

cli/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ node_modules
33
build
44
graphgen
55
example/package-lock.json
6+
vite.config.ts.timestamp-*

cli/app/src/components/Factory/Factory.tsx

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,21 @@ import { StrictMode, Suspense } from "react";
33
import { StyledEngineProvider } from "@mui/material/styles";
44
import { GraphInspector } from "../GraphInspector/GraphInspector";
55
import { Topbar } from "../Topbar/Topbar";
6-
import { createClient, dedupExchange, fetchExchange, Provider } from "urql";
6+
import { createClient, dedupExchange, Exchange, fetchExchange, Provider } from "urql";
7+
import { cacheExchange } from "@urql/exchange-graphcache";
8+
import { relayPagination } from '@urql/exchange-graphcache/extras';
79

810
const client = createClient({
911
url: "/graphql",
1012
exchanges: [
1113
dedupExchange,
14+
cacheExchange({
15+
resolvers: {
16+
Query: {
17+
all: relayPagination(),
18+
},
19+
},
20+
}) as Exchange,
1221
fetchExchange,
1322
],
1423
});

cli/app/src/components/GraphInspector/GraphInspector.css

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,3 +50,8 @@
5050
.type-label :first-child {
5151
margin-right: .25rem;
5252
}
53+
54+
.error {
55+
color: #FF9494;
56+
font-size: 2rem;
57+
}

cli/app/src/components/GraphInspector/GraphInspector.tsx

Lines changed: 61 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,61 @@
11
import "./GraphInspector.css";
22
import type { SyntheticEvent } from "react";
3-
import { useCallback, useEffect, useReducer, useRef } from "react";
3+
import { useCallback, useEffect, useReducer, useRef, useState } from "react";
44
import TreeView from "@mui/lab/TreeView";
55
import { Node } from "./Node";
6-
import { all, node } from "./queries";
6+
import { allQuery, node } from "./queries";
77
import { graphReducer } from "./graphReducer";
88
import { VertexNode } from "../../../../graphql/types";
99
import { MinusSquare, PlusSquare } from "./icons";
1010
import { StyledTreeItem } from "./StyledTreeItem";
1111
import { fetchGraphQL } from "../../graphql/fetchGraphql";
1212
import { Loader } from "../Loader/Loader";
13-
13+
import { useQuery } from 'urql';
14+
import type { Page } from '../../../../graphql/relay';
1415
const emptyGraph = { graph: {} };
1516

17+
const limit = 5;
18+
1619
export function GraphInspector(): JSX.Element {
20+
// TODO: call setAfter when scrolling
21+
const [after] = useState('');
22+
const [typename, setTypename] = useState<string | undefined>();
23+
24+
const [result] = useQuery<{ all: Page<VertexNode> }, {
25+
typename: string;
26+
first: number;
27+
after: string;
28+
}>({
29+
query: allQuery,
30+
pause: !typename,
31+
variables: {
32+
typename,
33+
first: limit,
34+
after
35+
},
36+
});
37+
1738
const [{ graph }, dispatch] = useReducer(graphReducer, emptyGraph);
1839
const expandedNodes = useRef(new Set<string>());
1940

41+
const { data, error } = result;
42+
43+
useEffect(() => {
44+
const edges = data?.all?.edges ?? [];
45+
46+
if (edges.length === 0) {
47+
return;
48+
}
49+
50+
dispatch({
51+
type: "ALL",
52+
payload: {
53+
typename,
54+
nodes: edges.map(edge => edge.node),
55+
},
56+
});
57+
}, [data, typename])
58+
2059
const handleChange = useCallback(
2160
async (_: SyntheticEvent, nodeIds: string[]) => {
2261
if (nodeIds.length === 0) {
@@ -79,22 +118,8 @@ export function GraphInspector(): JSX.Element {
79118
return;
80119
}
81120

82-
try {
83-
const response = await all(nodeId);
84-
85-
dispatch({
86-
type: "ALL",
87-
payload: {
88-
typename: nodeIds[0],
89-
nodes: response.data.all,
90-
},
91-
});
92-
} catch (e) {
93-
console.error(e);
94-
throw e;
95-
}
96-
},
97-
[],
121+
setTypename(nodeId);
122+
}, [],
98123
);
99124

100125
useEffect(() => {
@@ -120,6 +145,10 @@ export function GraphInspector(): JSX.Element {
120145
return <h2>No graph nodes</h2>;
121146
}
122147

148+
if (error) {
149+
return <p className="error">Oh no... {error?.message}</p>
150+
}
151+
123152
return (
124153
<TreeView
125154
aria-label="graph inspector"
@@ -135,20 +164,19 @@ export function GraphInspector(): JSX.Element {
135164
label={<div className="root">{label}</div>}
136165
>
137166
{nodes.length > 0
138-
? nodes.map((vertexNode, i) => {
139-
return (
140-
<StyledTreeItem
141-
key={vertexNode.id}
142-
nodeId={vertexNode.id}
143-
label={
144-
<Node
145-
parentId={`${typename}.nodes.${i}`}
146-
node={vertexNode}
147-
/>
148-
}
149-
/>
150-
);
151-
})
167+
? nodes.map((vertexNode, i) => (
168+
<StyledTreeItem
169+
key={vertexNode.id}
170+
nodeId={vertexNode.id}
171+
label={
172+
<Node
173+
parentId={`${typename}.nodes.${i}`}
174+
node={vertexNode}
175+
/>
176+
}
177+
/>
178+
)
179+
)
152180
: <Loader />}
153181
</StyledTreeItem>
154182
))}

cli/app/src/components/GraphInspector/StyledTreeItem.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { alpha, styled } from "@mui/material/styles";
22
import TreeItem, { treeItemClasses, TreeItemProps } from "@mui/lab/TreeItem";
3-
import { FC } from "react";
3+
import { FC } from 'react';
44

5-
export const StyledTreeItem: FC<TreeItemProps> = styled((
6-
props: TreeItemProps,
7-
) => <TreeItem {...props} />)(({ theme }) => ({
5+
export const StyledTreeItem: FC<TreeItemProps> = styled((props: TreeItemProps) => (
6+
<TreeItem {...props} />
7+
))(({ theme }) => ({
88
[`& .${treeItemClasses.iconContainer}`]: {
99
"& .close": {
1010
opacity: 0.3,

cli/app/src/components/GraphInspector/graphReducer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import produce from "immer";
2-
import { assert } from "../../assert/assert.ts";
2+
import { assert } from "../../assert/assert";
33
import { match } from "ts-pattern";
4-
import type { VertexNode } from "../../../../graphql/types.ts";
4+
import type { VertexNode } from "../../../../graphql/types";
55

66
interface Type {
77
typename: string;

cli/app/src/components/GraphInspector/queries.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { fetchGraphQL } from "../../graphql/fetchGraphql";
2+
import { gql } from 'urql';
23

34
export async function node(id: string) {
45
return await fetchGraphQL(
@@ -33,11 +34,11 @@ export async function node(id: string) {
3334
);
3435
}
3536

36-
export async function all(typename: string) {
37-
return await fetchGraphQL(
38-
`
39-
query All($typename: String!) {
40-
all(typename:$typename) {
37+
export const allQuery = gql`
38+
query All($typename: String!, $first: Int!, $after: String!) {
39+
all(typename:$typename, first: $first, after: $after) {
40+
edges {
41+
node {
4142
id
4243
fields {
4344
key
@@ -57,9 +58,10 @@ export async function all(typename: string) {
5758
}
5859
}
5960
}
60-
`,
61-
{
62-
"typename": typename,
63-
},
64-
);
61+
pageInfo {
62+
hasNextPage
63+
endCursor
64+
}
65+
}
6566
}
67+
`

cli/graphql/.vscode/extensions.json

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

cli/graphql/.vscode/settings.json

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

cli/graphql/inspector.graphql

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,25 @@ type Vertex implements Node {
3232
fields: [FieldEntry!]!
3333
}
3434

35+
type VertexEdge {
36+
node: Vertex!
37+
cursor: String!
38+
}
39+
40+
type PageInfo {
41+
hasNextPage: Boolean!
42+
hasPreviousPage: Boolean!
43+
startCursor: String
44+
endCursor: String
45+
}
46+
47+
type VertexConnection {
48+
count: Int!
49+
total: Int!
50+
pageInfo: PageInfo!
51+
edges: [VertexEdge]!
52+
}
53+
3554
type Type {
3655
typename: String!
3756
count: Int!
@@ -40,7 +59,7 @@ type Type {
4059
type Query {
4160
meta: [Type]
4261
graph: JSON
43-
all(typename: String!): [Vertex!]
62+
all(typename: String!, first: Int!, after: String!): VertexConnection!
4463
node(id: ID!): Node
4564
}
4665

0 commit comments

Comments
 (0)