-
-
Notifications
You must be signed in to change notification settings - Fork 21
/
Show-GraphQLVoyager.ps1
206 lines (178 loc) · 4.59 KB
/
Show-GraphQLVoyager.ps1
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
<#PSScriptInfo
.VERSION 2.0.1
.AUTHOR Roman Kuzmin
.COPYRIGHT (c) Roman Kuzmin
.TAGS GraphQL Voyager
.GUID 9308d778-bfa3-4301-9ef8-db273bda2357
.LICENSEURI http://www.apache.org/licenses/LICENSE-2.0
.PROJECTURI https://github.com/nightroman/PowerShelf
#>
<#
.Synopsis
Shows GraphQL schema using https://github.com/graphql-kit/graphql-voyager
.Description
This command generates and opens HTML which renders GraphQL Voyager
with the specified GraphQL schema file or URL for introspection and
several display options.
.Parameter Schema
Specifies the GraphQL schema file or URL for introspection.
.Parameter RootType
The root type name.
.Parameter Output
Tells to output HTML to the specified file. If this parameter is
omitted then the temp file "GraphQLVoyager.html" is used and on
Windows automatically opened.
.Parameter HideDocs
Tells to hide the docs sidebar.
.Parameter HideLeafFields
Tells to hide all scalars and enums.
.Parameter HideSettings
Tells to hide the settings panel.
.Parameter HideRoot
Tells to hide the root type.
.Parameter ShowDeprecated
Tells to show deprecated entities.
.Parameter ShowRelay
Tells to show relay types literally.
.Parameter SortByAlphabet
Tells to sort type fields by alphabet.
.Link
https://github.com/nightroman/PowerShelf
#>
[CmdletBinding()]
param(
[Parameter(Position=0, Mandatory=1)]
[string]$Schema
,
[Parameter(Position=1)]
[string]$RootType
,
[string]$Output
,
[switch]$HideDocs
,
[switch]$HideLeafFields
,
[switch]$HideSettings
,
[switch]$HideRoot
,
[switch]$ShowDeprecated
,
[switch]$ShowRelay
,
[switch]$SortByAlphabet
)
$ErrorActionPreference = 1
### Schema
if ($Schema -match '^\w\w+://') {
$sdl = $null
}
else {
$sdl = [System.IO.File]::ReadAllText($PSCmdlet.GetUnresolvedProviderPathFromPSPath($Schema))
$sdl = $sdl.Replace('\', '\\').Replace('${', '\${').Replace('`', '\`')
}
### RootType
if ($RootType) {
if ($RootType -notmatch '^\w+$') {
throw "Invalid parameter RootType: '$RootType'."
}
$title = "$RootType $Schema"
}
else {
$title = $Schema
}
### Output
if ($Output) {
$toShow = $false
$Output = $PSCmdlet.GetUnresolvedProviderPathFromPSPath($Output)
}
else {
$toShow = $PSVersionTable['Platform'] -ne 'Unix'
$Output = [System.IO.Path]::GetTempPath() + 'GraphQLVoyager.html'
}
### make HTML
function Get-Boolean($Value) {
if ($Value) {'true'} else {'false'}
}
$html = $(
@"
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>$title</title>
<style>
body {
height: 100%;
margin: 0;
width: 100%;
overflow: hidden;
}
#voyager {
height: 100vh;
}
</style>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.css"/>
<script src="https://cdn.jsdelivr.net/npm/graphql-voyager/dist/voyager.standalone.js"></script>
</head>
<body>
<div id="voyager">Loading...</div>
<script type="module">
const escapeHtml = (html) => {
return html.replaceAll('&', '&').replaceAll('<', '<').replaceAll('>', '>').replaceAll('"', '"').replaceAll("'", ''');
}
const voyager = document.getElementById('voyager');
try {
"@
if ($sdl) {
@"
const sdl = ``$sdl``
const introspection = GraphQLVoyager.sdlToSchema(sdl);
"@
}
else {
@"
const response = await fetch(
'$($Schema.Replace("'", "\'"))',
{
method: 'post',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({ query: GraphQLVoyager.voyagerIntrospectionQuery }),
credentials: 'omit',
},
);
const introspection = await response.json();
"@
}
@"
GraphQLVoyager.renderVoyager(voyager, {
introspection,
hideDocs: $(Get-Boolean $HideDocs),
hideSettings: $(Get-Boolean $HideSettings),
displayOptions: {
rootType: $(if ($RootType) {"'$RootType'"} else {'null'}),
hideRoot: $(Get-Boolean $HideRoot),
showLeafFields: $(Get-Boolean (!$HideLeafFields)),
skipDeprecated: $(Get-Boolean (!$ShowDeprecated)),
skipRelay: $(Get-Boolean (!$ShowRelay)),
sortByAlphabet: $(Get-Boolean $SortByAlphabet),
},
});
}
catch(error) {
voyager.innerHTML = ``<pre>`${escapeHtml(error.toString())}</pre>``;
}
</script>
</body>
</html>
"@
) -join "`n"
### save and open HTML
[System.IO.File]::WriteAllText($Output, $html)
if ($toShow) {
Invoke-Item -LiteralPath $Output
}