@@ -81,7 +81,12 @@ function Get-GQL
81
81
# `-Refresh` implies `-Cache` (it just will not return an uncached value).
82
82
[Parameter (ValueFromPipelineByPropertyName )]
83
83
[switch ]
84
- $Refresh
84
+ $Refresh ,
85
+
86
+ [Parameter (ValueFromPipelineByPropertyName )]
87
+ [ValidatePattern (' \.json$' )]
88
+ [string []]
89
+ $OutputPath
85
90
)
86
91
87
92
process {
@@ -131,7 +136,9 @@ function Get-GQL
131
136
# endregion Prepare the REST Parameters
132
137
133
138
# region Handle Each Query
139
+ $queryNumber = -1
134
140
:nextQuery foreach ($gqlQuery in $Query ) {
141
+ $queryNumber ++
135
142
$queryLines = @ ($gqlQuery -split ' (?>\r\n|\n)' )
136
143
# region Check for File or Cached Query
137
144
if ($queryLines.Length -eq 1 ) {
@@ -146,9 +153,9 @@ function Get-GQL
146
153
} elseif ($query -match ' [\\/]' ) {
147
154
$psCmdlet.WriteError (
148
155
[Management.Automation.ErrorRecord ]::new(
149
- [Exception ]::new(" Query file not found: '$gqlQuery '" ),
150
- ' NotFound' ,
151
- ' ObjectNotFound' ,
156
+ [Exception ]::new(" Query file not found: '$gqlQuery '" ),
157
+ ' NotFound' ,
158
+ ' ObjectNotFound' ,
152
159
$gqlQuery
153
160
)
154
161
)
@@ -165,14 +172,25 @@ function Get-GQL
165
172
$script :GraphQLOutputCache = [Ordered ]@ {}
166
173
}
167
174
168
- if ($script :GraphQLOutputCache .$gqlQuery -and
169
- -not $Parameter.Count -and
175
+ if ($script :GraphQLOutputCache .$gqlQuery -and
176
+ -not $Parameter.Count -and
170
177
-not $Refresh
171
178
) {
172
179
$script :GraphQLOutputCache .$gqlQuery
173
180
continue nextQuery
174
181
}
175
182
183
+ $queryOutPath =
184
+ if ($OutputPath ) {
185
+ if ($OutputPath [$queryNumber ]) {
186
+ $OutputPath [$queryNumber ]
187
+ } else {
188
+ $OutputPath [-1 ]
189
+ }
190
+ }
191
+
192
+
193
+
176
194
# region Run the Query
177
195
$invokeSplat.Body = [Ordered ]@ {query = $gqlQuery }
178
196
if ($Parameter ) {
@@ -184,16 +202,17 @@ function Get-GQL
184
202
continue nextQuery
185
203
}
186
204
$invokeSplat.Body = ConvertTo-Json - InputObject $invokeSplat.Body - Depth 10
187
- $shouldProcessMessage = " Querying $GraphQLUri with $gqlQuery "
205
+ $shouldProcessMessage = " Querying $GraphQLUri with $gqlQuery "
188
206
if (-not $PSCmdlet.ShouldProcess ($shouldProcessMessage )) {
189
207
continue nextQuery
190
208
}
191
209
$gqlOutput = Invoke-RestMethod @invokeSplat * > & 1
192
210
if ($gqlOutput -is [Management.Automation.ErrorRecord ]) {
193
211
$PSCmdlet.WriteError ($gqlOutput )
194
212
continue nextQuery
195
- }
196
- elseif ($gqlOutput.errors ) {
213
+ }
214
+
215
+ if ($gqlOutput.errors ) {
197
216
foreach ($gqlError in $gqlOutput.errors ) {
198
217
$psCmdlet.WriteError ((
199
218
[Management.Automation.ErrorRecord ]::new(
@@ -205,33 +224,33 @@ function Get-GQL
205
224
}
206
225
continue nextQuery
207
226
}
208
- elseif ($gqlOutput.data ) {
209
- if ($PSTypeName ) {
210
- $gqlOutput.data.pstypenames.clear ()
211
- for ($goBackwards = $pstypename.Length - 1 ; $goBackwards -ge 0 ; $goBackwards -- ) {
212
- $gqlOutput.data.pstypenames.add ($pstypename [$goBackwards ])
213
- }
214
- }
215
- if ($Cache ) {
216
- $script :GraphQLOutputCache [$gqlQuery ] = $gqlOutput.data
217
- }
218
- $gqlOutput.data
227
+
228
+ if ($gqlOutput.data ) {
229
+ $gqlOutput = $gqlOutput.data
219
230
}
220
- elseif ( $gqlOutput ) {
221
- if ($PSTypeName ) {
222
- $gqlOutput .pstypenames.clear ()
223
- for ( $goBackwards = $pstypename .Length - 1 ; $goBackwards -ge 0 ; $goBackwards -- ) {
224
- $gqlOutput .pstypenames.add ( $pstypename [ $goBackwards ])
225
- }
226
- }
227
- if ($Cache ) {
228
- $script :GraphQLOutputCache [ $gqlQuery ] = $gqlOutput
231
+
232
+ if (-not $gqlOutput ) {
233
+ continue nextQuery
234
+ }
235
+
236
+ if ( $PSTypeName ) {
237
+ $gqlOutput .pstypenames.clear ()
238
+ for ($goBackwards = $pstypename .Length - 1 ; $goBackwards -ge 0 ; $goBackwards -- ) {
239
+ $gqlOutput .pstypenames.add ( $pstypename [ $goBackwards ])
229
240
}
230
- $gqlOutput
231
241
}
242
+ if ($Cache ) {
243
+ $script :GraphQLOutputCache [$gqlQuery ] = $gqlOutput
244
+ }
245
+ if ($queryOutPath ) {
246
+ New-Item - ItemType File - Path $queryOutPath - Force - Value (
247
+ ConvertTo-Json - Depth 100 - InputObject $gqlOutput
248
+ )
249
+ } else {
250
+ $gqlOutput
251
+ }
232
252
# endregion Run the Query
233
253
# endregion Handle Each Query
234
-
235
254
}
236
255
}
237
256
}
0 commit comments