-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathindex.tsx
More file actions
110 lines (101 loc) · 2.99 KB
/
index.tsx
File metadata and controls
110 lines (101 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import { CSSProperties, FC, useCallback } from 'react';
import {
GraphiQLPlugin,
useGraphiQL,
useGraphiQLActions,
useOperationsEditorState,
useOptimisticState,
} from '@graphiql/react';
import {
Explorer as GraphiQLExplorer,
GraphiQLExplorerProps,
} from 'graphiql-explorer';
import ArrowIcon from './icons/arrow.svg?react';
import FolderPlusIcon from './icons/folder-plus.svg?react';
import CheckboxUncheckedIcon from './icons/checkbox-unchecked.svg?react';
import CheckboxCheckedIcon from './icons/checkbox-checked.svg?react';
import './index.css';
const colors = {
keyword: 'hsl(var(--color-primary))',
def: 'hsl(var(--color-tertiary))',
property: 'hsl(var(--color-info))',
qualifier: 'hsl(var(--color-secondary))',
attribute: 'hsl(var(--color-tertiary))',
number: 'hsl(var(--color-success))',
string: 'hsl(var(--color-warning))',
builtin: 'hsl(var(--color-success))',
string2: 'hsl(var(--color-secondary))',
variable: 'hsl(var(--color-secondary))',
atom: 'hsl(var(--color-tertiary))',
};
const arrowOpen = (
<ArrowIcon style={{ width: 'var(--px-16)', transform: 'rotate(90deg)' }} />
);
const arrowClosed = <ArrowIcon style={{ width: 'var(--px-16)' }} />;
const checkboxUnchecked = (
<CheckboxUncheckedIcon style={{ marginRight: 'var(--px-4)' }} />
);
const checkboxChecked = (
<CheckboxCheckedIcon
style={{ fill: 'hsl(var(--color-info))', marginRight: 'var(--px-4)' }}
/>
);
const styles: Record<string, CSSProperties> = {
buttonStyle: {
cursor: 'pointer',
fontSize: '2em',
lineHeight: 0,
},
explorerActionsStyle: {
paddingTop: 'var(--px-16)',
},
actionButtonStyle: {},
};
export type GraphiQLExplorerPluginProps = Omit<
GraphiQLExplorerProps,
'onEdit' | 'query'
>;
const ExplorerPlugin: FC<GraphiQLExplorerPluginProps> = props => {
const { setOperationName, run } = useGraphiQLActions();
const schema = useGraphiQL(state => state.schema);
// handle running the current operation from the plugin
const handleRunOperation = useCallback(
(operationName: string | null) => {
if (operationName) {
// set the plugin-defined operation name before executing
setOperationName(operationName);
}
run();
},
[run, setOperationName],
);
// load the current editor tab state into the explorer
const [operationsString, handleEditOperations] = useOptimisticState(
useOperationsEditorState(),
);
return (
<GraphiQLExplorer
schema={schema}
onRunOperation={handleRunOperation}
explorerIsOpen
colors={colors}
arrowOpen={arrowOpen}
arrowClosed={arrowClosed}
checkboxUnchecked={checkboxUnchecked}
checkboxChecked={checkboxChecked}
styles={styles}
query={operationsString}
onEdit={handleEditOperations}
{...props}
/>
);
};
export function explorerPlugin(
props?: GraphiQLExplorerPluginProps,
): GraphiQLPlugin {
return {
title: 'GraphiQL Explorer',
icon: FolderPlusIcon,
content: () => <ExplorerPlugin {...props} />,
};
}