-
-
Notifications
You must be signed in to change notification settings - Fork 529
/
Copy pathindex.tsx
134 lines (118 loc) · 4.17 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
import * as React from 'react';
import { render } from 'react-dom';
import Button from '@material-ui/core/Button';
import { MuiThemeProvider } from '@material-ui/core/styles';
import { theme } from '../src/components/MUITheme';
import { GraphQLVoyager } from '../src';
import LogoIcon from './icons/logo-small.svg';
import { IntrospectionModal } from './IntrospectionModal';
import { defaultPreset } from './presets';
import './components.css';
export default class Demo extends React.Component {
state = {
changeSchemaModalOpen: false,
introspection: defaultPreset,
};
// not part of state because we parse it in constructor and just use in render
displayOptions = {};
constructor(props) {
super(props);
const { url, withCredentials, ...restQueryParams } = getQueryParams();
// parse rest of query params we expect them to be in JSON format
// we might pick also some options that are not valid displayOptions but they will be ignored
// by Voyager component
for (const [key, value] of Object.entries(restQueryParams)) {
try {
this.displayOptions[key] = JSON.parse(value);
} catch (_) {
console.log(
`Not expected value for key "${key}" or value >${value}< is not in json format so just ignoring it`,
);
}
}
if (url) {
this.state.introspection = introspectionQuery =>
fetch(url, {
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: introspectionQuery }),
...(withCredentials === 'true' ? { credentials: 'include', mode: 'cors' } : {}),
}).then(response => response.json());
}
}
public render() {
const { changeSchemaModalOpen, introspection } = this.state;
const openChangeSchema = () => this.setState({ changeSchemaModalOpen: true });
const closeChangeSchema = () => this.setState({ changeSchemaModalOpen: false });
return (
<MuiThemeProvider theme={theme}>
<GraphQLVoyager
introspection={introspection}
displayOptions={this.displayOptions}
onDisplayOptionsChange={setQueryParamsWithoutRefresh}
>
<GraphQLVoyager.PanelHeader>
<div className="voyager-panel">
<Logo />
<Button
color="primary"
style={{ color: 'white' }}
variant="contained"
className="choosebutton"
onClick={openChangeSchema}
>
Change Schema
</Button>
</div>
</GraphQLVoyager.PanelHeader>
</GraphQLVoyager>
<IntrospectionModal
open={changeSchemaModalOpen}
onClose={closeChangeSchema}
onChange={introspection => this.setState({ introspection })}
/>
</MuiThemeProvider>
);
}
}
function setQueryParamsWithoutRefresh(displayOptions) {
if ('URLSearchParams' in window) {
const searchParams = new URLSearchParams(window.location.search);
for (const [key, value] of Object.entries(displayOptions)) {
searchParams.set(key, JSON.stringify(value));
}
var newRelativePathQuery = window.location.pathname + '?' + searchParams.toString();
history.pushState(null, '', newRelativePathQuery);
} else {
console.log('Missing URLSearchParams so just printing new displayOptions', displayOptions);
}
}
function getQueryParams(): { [key: string]: string } {
const query = window.location.search.substring(1);
const params = {};
for (const param of query.split('&')) {
const [key, value] = param.split('=');
params[key] = value ? decodeURIComponent(value.replace(/\+/g, ' ')) : '';
}
return params;
}
class Logo extends React.Component {
render() {
return (
<div className="voyager-logo">
<a href="https://github.com/APIs-guru/graphql-voyager" target="_blank">
<div className="logo">
<LogoIcon />
<h2 className="title">
<strong>GraphQL</strong> Voyager
</h2>
</div>
</a>
</div>
);
}
}
render(<Demo />, document.getElementById('root'));