-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
Copy pathindex.tsx
177 lines (166 loc) · 4.81 KB
/
index.tsx
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
import {
GraphiQLPlugin,
useEditorContext,
useExecutionContext,
useSchemaContext,
} from '@graphiql/react';
import GraphiQLExplorer, { GraphiQLExplorerProps } from 'graphiql-explorer';
import React, { useCallback, useRef } from 'react';
import './graphiql-explorer.d.ts';
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 = (
<svg
viewBox="0 -4 13 15"
style={{
color: 'hsla(var(--color-neutral), var(--alpha-tertiary, 0.4))',
marginRight: 'var(--px-4)',
height: 'var(--px-16)',
width: 'var(--px-16)',
}}
>
<path
d="M3.35355 6.85355L6.14645 9.64645C6.34171 9.84171 6.65829 9.84171 6.85355 9.64645L9.64645 6.85355C9.96143 6.53857 9.73835 6 9.29289 6L3.70711 6C3.26165 6 3.03857 6.53857 3.35355 6.85355Z"
fill="currentColor"
/>
</svg>
);
const arrowClosed = (
<svg
viewBox="0 -2 13 15"
style={{
color: 'hsla(var(--color-neutral), var(--alpha-tertiary, 0.4))',
marginRight: 'var(--px-4)',
height: 'var(--px-16)',
width: 'var(--px-16)',
}}
>
<path
d="M6.35355 11.1464L9.14645 8.35355C9.34171 8.15829 9.34171 7.84171 9.14645 7.64645L6.35355 4.85355C6.03857 4.53857 5.5 4.76165 5.5 5.20711V10.7929C5.5 11.2383 6.03857 11.4614 6.35355 11.1464Z"
fill="currentColor"
/>
</svg>
);
const checkboxUnchecked = (
<svg
viewBox="0 0 15 15"
style={{
color: 'hsla(var(--color-neutral), var(--alpha-tertiary, 0.4))',
marginRight: 'var(--px-4)',
height: 'var(--px-16)',
width: 'var(--px-16)',
}}
>
<circle cx="7.5" cy="7.5" r="6" stroke="currentColor" fill="none" />
</svg>
);
const checkboxChecked = (
<svg
viewBox="0 0 15 15"
style={{
color: 'hsl(var(--color-info))',
marginRight: 'var(--px-4)',
height: 'var(--px-16)',
width: 'var(--px-16)',
}}
>
<circle cx="7.5" cy="7.5" r="7.5" fill="currentColor" />
<path
d="M4.64641 7.00106L6.8801 9.23256L10.5017 5.61325"
fill="none"
stroke="white"
strokeWidth="1.5"
/>
</svg>
);
const styles = {
buttonStyle: {
backgroundColor: 'transparent',
border: 'none',
color: 'hsla(var(--color-neutral), var(--alpha-secondary, 0.6))',
cursor: 'pointer',
fontSize: '1em',
},
explorerActionsStyle: {
padding: 'var(--px-8) var(--px-4)',
},
actionButtonStyle: {
backgroundColor: 'transparent',
border: 'none',
color: 'hsla(var(--color-neutral), var(--alpha-secondary, 0.6))',
cursor: 'pointer',
fontSize: '1em',
},
};
function ExplorerPlugin(props: GraphiQLExplorerProps) {
const { setOperationName } = useEditorContext({ nonNull: true });
const { schema } = useSchemaContext({ nonNull: true });
const { run } = useExecutionContext({ nonNull: true });
const handleRunOperation = useCallback(
(operationName: string | null) => {
if (operationName) {
setOperationName(operationName);
}
run();
},
[run, setOperationName],
);
return (
<GraphiQLExplorer
schema={schema}
onRunOperation={handleRunOperation}
explorerIsOpen
colors={colors}
arrowOpen={arrowOpen}
arrowClosed={arrowClosed}
checkboxUnchecked={checkboxUnchecked}
checkboxChecked={checkboxChecked}
styles={styles}
{...props}
/>
);
}
export function useExplorerPlugin(props: GraphiQLExplorerProps) {
const propsRef = useRef(props);
propsRef.current = props;
const pluginRef = useRef<GraphiQLPlugin>();
pluginRef.current ||= {
title: 'GraphiQL Explorer',
icon: () => (
<svg height="1em" strokeWidth="1.5" viewBox="0 0 24 24" fill="none">
<path
d="M18 6H20M22 6H20M20 6V4M20 6V8"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M21.4 20H2.6C2.26863 20 2 19.7314 2 19.4V11H21.4C21.7314 11 22 11.2686 22 11.6V19.4C22 19.7314 21.7314 20 21.4 20Z"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
/>
<path
d="M2 11V4.6C2 4.26863 2.26863 4 2.6 4H8.77805C8.92127 4 9.05977 4.05124 9.16852 4.14445L12.3315 6.85555C12.4402 6.94876 12.5787 7 12.722 7H14"
stroke="currentColor"
strokeLinecap="round"
strokeLinejoin="round"
/>
</svg>
),
content: () => <ExplorerPlugin {...propsRef.current} />,
};
return pluginRef.current;
}