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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
|
/// import
import { filterXSS } from "npm:xss@1.0.14";
/// util
import { getLoadingMarkup } from "./markup.ts";
const CONFIG_ID = "playground-config";
const loading = getLoadingMarkup();
const filter = (val: string) => {
return filterXSS(val, {
stripIgnoreTag: true,
stripIgnoreTagBody: ["script"],
whiteList: {}
});
}
const getCdnMarkup = ({ cdnUrl = "//cdn.jsdelivr.net/npm", faviconUrl, version }: {
cdnUrl?: string
faviconUrl?: string | null
version?: string
}) => {
const buildCDNUrl = (packageName: string, suffix: string) =>
filter(`${cdnUrl}/${packageName}${version ? `@${version}` : ""}/${suffix}` || "");
return `
<link rel="stylesheet" href="${buildCDNUrl("graphql-playground-react", "build/static/css/index.css")}"/>
${typeof faviconUrl === "string" ? `<link rel="shortcut icon" href="${filter(faviconUrl || "")}" />` : ""}
${faviconUrl === undefined ? `<link rel="shortcut icon" href="${buildCDNUrl("graphql-playground-react", "build/favicon.png")}" />` : ""}
<script src="${buildCDNUrl("graphql-playground-react", "build/static/js/middleware.js")}"></script>
`;
}
const renderConfig = (config: unknown) => {
return filterXSS(`<div id="${CONFIG_ID}">${JSON.stringify(config)}</div>`, {
whiteList: { div: ["id"] }
});
};
/// export
export interface MiddlewareOptions {
codeTheme?: EditorColours;
config?: any;
endpoint?: string;
env?: any;
schema?: IntrospectionResult;
settings?: ISettings;
subscriptionEndpoint?: string;
tabs?: Tab[];
workspaceName?: string;
}
export type CursorShape = "line" | "block" | "underline";
export type Theme = "dark" | "light";
export interface ISettings {
"editor.cursorShape": CursorShape;
"editor.fontFamily": string;
"editor.fontSize": number;
"editor.reuseHeaders": boolean;
"editor.theme": Theme;
"general.betaUpdates": boolean;
"request.credentials": string;
"request.globalHeaders": { [key: string]: string };
"schema.polling.enable": boolean;
"schema.polling.endpointFilter": string;
"schema.polling.interval": number;
"tracing.hideTracingResponse": boolean;
"tracing.tracingSupported": boolean;
}
export interface EditorColours {
atom: string;
attribute: string;
builtin: string;
comment: string;
cursorColor: string;
def: string;
editorBackground: string;
keyword: string;
leftDrawerBackground: string;
meta: string;
number: string;
property: string;
punctuation: string;
qualifier: string;
resultBackground: string;
rightDrawerBackground: string;
selection: string;
string: string;
string2: string;
variable: string;
ws: string;
}
export interface IntrospectionResult {
__schema: any;
}
export interface RenderPageOptions extends MiddlewareOptions {
cdnUrl?: string;
env?: any;
faviconUrl?: string | null;
title?: string;
version?: string;
}
export interface Tab {
endpoint: string;
headers?: { [key: string]: string };
name?: string;
query: string;
responses?: string[];
variables?: string;
}
export function renderPlaygroundPage(options: RenderPageOptions) {
const extendedOptions:
& Partial<{
canSaveConfig: boolean
configString: string
}>
& RenderPageOptions = {
...options,
canSaveConfig: false
};
if (options.config)
extendedOptions.configString = JSON.stringify(options.config, null, 2);
if (!extendedOptions.endpoint && !extendedOptions.configString)
console.warn("WARNING: You did not provide an endpoint and do not have a .graphqlconfig. Make sure you have at least one of them.");
else if (extendedOptions.endpoint)
extendedOptions.endpoint = filter(extendedOptions.endpoint || "");
return `
<!DOCTYPE html>
<html lang="en">
<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 href="https://brick.a.ssl.fastly.net/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>
html {
font-family: "Open Sans", sans-serif;
overflow: hidden;
}
body {
background-color: #172a3a;
margin: 0;
}
#${CONFIG_ID} {
display: none;
}
.playgroundIn {
animation: playgroundIn 0.5s ease-out forwards;
}
@keyframes playgroundIn {
from {
opacity: 0;
transform: translateY(10px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>
${loading.container}
${renderConfig(extendedOptions)}
<div id="root"/>
<script>
window.addEventListener("load", () => {
${loading.script}
const root = document.getElementById("root");
root.classList.add("playgroundIn");
const configText = document.getElementById("${CONFIG_ID}").innerText;
if (configText && configText.length) {
try {
GraphQLPlayground.init(root, JSON.parse(configText));
} catch(_) {
console.error("could not find config");
}
} else {
GraphQLPlayground.init(root);
}
});
</script>
</body>
</html>
`;
}
|