-
Notifications
You must be signed in to change notification settings - Fork 730
/
render-playground-page.ts
185 lines (168 loc) 路 4.72 KB
/
render-playground-page.ts
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
178
179
180
181
182
183
184
185
import getLoadingMarkup from './get-loading-markup'
export interface MiddlewareOptions {
endpoint?: string
subscriptionEndpoint?: string
workspaceName?: string
env?: any
config?: any
settings?: ISettings
schema?: IntrospectionResult
tabs?: Tab[]
codeTheme?: EditorColours
}
export type CursorShape = 'line' | 'block' | 'underline'
export type Theme = 'dark' | 'light'
export interface ISettings {
'general.betaUpdates': boolean
'editor.cursorShape': CursorShape
'editor.theme': Theme
'editor.reuseHeaders': boolean
'tracing.hideTracingResponse': boolean
'editor.fontSize': number
'editor.fontFamily': string
'request.credentials': string
}
export interface EditorColours {
property: string
comment: string
punctuation: string
keyword: string
def: string
qualifier: string
attribute: string
number: string
string: string
builtin: string
string2: string
variable: string
meta: string
atom: string
ws: string
selection: string
cursorColor: string
editorBackground: string
resultBackground: string
leftDrawerBackground: string
rightDrawerBackground: string
}
export interface IntrospectionResult {
__schema: any
}
export interface RenderPageOptions extends MiddlewareOptions {
version: string
cdnUrl?: string
env?: any
}
export interface Tab {
endpoint: string
query: string
variables?: string
responses?: string[]
headers?: { [key: string]: string }
}
const loading = getLoadingMarkup()
const getCdnMarkup = ({ version, cdnUrl = '//cdn.jsdelivr.net/npm' }) => `
<link rel="stylesheet" href="${cdnUrl}/graphql-playground-react${
version ? `@${version}` : ''
}/build/static/css/index.css" />
<link rel="shortcut icon" href="${cdnUrl}/graphql-playground-react${
version ? `@${version}` : ''
}/build/favicon.png" />
<script src="${cdnUrl}/graphql-playground-react${
version ? `@${version}` : ''
}/build/static/js/middleware.js"></script>
`
export function renderPlaygroundPage(options: RenderPageOptions) {
const extendedOptions: any = {
...options,
canSaveConfig: false,
}
// for compatibility
if ((options as any).subscriptionsEndpoint) {
extendedOptions.subscriptionEndpoint = (options as any).subscriptionsEndpoint
}
if (options.config) {
extendedOptions.configString = JSON.stringify(options.config, null, 2)
}
if (!extendedOptions.endpoint && !extendedOptions.configString) {
/* tslint:disable-next-line */
console.warn(
`WARNING: You didn't provide an endpoint and don't have a .graphqlconfig. Make sure you have at least one of them.`,
)
}
return `
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<meta name="viewport" content="user-scalable=no, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, minimal-ui">
<link rel="shortcut icon" href="https://graphcool-playground.netlify.com/favicon.png">
<link href="https://fonts.googleapis.com/css?family=Open+Sans:300,400,600,700|Source+Code+Pro:400,700" rel="stylesheet">
<title>${extendedOptions.title || 'GraphQL Playground'}</title>
${
extendedOptions.env === 'react' || extendedOptions.env === 'electron'
? ''
: getCdnMarkup(extendedOptions)
}
</head>
<body>
<style type="text/css">
html {
font-family: "Open Sans", sans-serif;
overflow: hidden;
}
body {
margin: 0;
background: #172a3a;
}
.playgroundIn {
-webkit-animation: playgroundIn 0.5s ease-out forwards;
animation: playgroundIn 0.5s ease-out forwards;
}
@-webkit-keyframes playgroundIn {
from {
opacity: 0;
-webkit-transform: translateY(10px);
-ms-transform: translateY(10px);
transform: translateY(10px);
}
to {
opacity: 1;
-webkit-transform: translateY(0);
-ms-transform: translateY(0);
transform: translateY(0);
}
}
@keyframes playgroundIn {
from {
opacity: 0;
-webkit-transform: translateY(10px);
-ms-transform: translateY(10px);
transform: translateY(10px);
}
to {
opacity: 1;
-webkit-transform: translateY(0);
-ms-transform: translateY(0);
transform: translateY(0);
}
}
</style>
${loading.container}
<div id="root" />
<script type="text/javascript">
window.addEventListener('load', function (event) {
${loading.script}
const root = document.getElementById('root');
root.classList.add('playgroundIn');
GraphQLPlayground.init(root, ${JSON.stringify(
extendedOptions,
null,
2,
)})
})
</script>
</body>
</html>
`
}