-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.jsx
56 lines (51 loc) · 1.48 KB
/
index.jsx
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
import React from 'react'
import { MiniGraphiQL as MiniGraphiQLComponent } from './MiniGraphiQL'
import { buildClientSchema, getIntrospectionQuery } from 'graphql'
import { usePromise } from 'react-extra-hooks'
import { getSchemaFormUrl } from './instrospectSchema.js'
export const MiniGraphiQL = ({ url = '', height = '200px', ...rest }) => {
const fetchShema = () => {
return getSchemaFormUrl({ url })
}
const { result, loading, error } = usePromise(url ? fetchShema : null, {
promizeId: url,
cache: true,
})
if (loading) {
return <Container height={height}>loading...</Container>
}
if (error) {
return (
<Container height={height} color='red'>
error: {error?.message}
</Container>
)
}
return (
<Container height={height}>
<MiniGraphiQLComponent
styles={{ editor: { height } }}
schema={result}
{...rest}
/>
</Container>
)
}
const Container = ({ height, color = 'black', ...props }) => {
return (
<div
style={{
height,
borderRadius: '10px',
overflow: 'hideden',
width: '100%',
color,
background: '#eee',
justifyContent: 'center',
alignItems: 'center',
display: 'flex',
}}
{...props}
/>
)
}