@@ -81,7 +81,12 @@ function Get-GQL
8181 # `-Refresh` implies `-Cache` (it just will not return an uncached value).
8282 [Parameter (ValueFromPipelineByPropertyName )]
8383 [switch ]
84- $Refresh
84+ $Refresh ,
85+
86+ [Parameter (ValueFromPipelineByPropertyName )]
87+ [ValidatePattern (' \.json$' )]
88+ [string []]
89+ $OutputPath
8590 )
8691
8792 process {
@@ -131,7 +136,9 @@ function Get-GQL
131136 # endregion Prepare the REST Parameters
132137
133138 # region Handle Each Query
139+ $queryNumber = -1
134140 :nextQuery foreach ($gqlQuery in $Query ) {
141+ $queryNumber ++
135142 $queryLines = @ ($gqlQuery -split ' (?>\r\n|\n)' )
136143 # region Check for File or Cached Query
137144 if ($queryLines.Length -eq 1 ) {
@@ -146,9 +153,9 @@ function Get-GQL
146153 } elseif ($query -match ' [\\/]' ) {
147154 $psCmdlet.WriteError (
148155 [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' ,
152159 $gqlQuery
153160 )
154161 )
@@ -165,14 +172,25 @@ function Get-GQL
165172 $script :GraphQLOutputCache = [Ordered ]@ {}
166173 }
167174
168- if ($script :GraphQLOutputCache .$gqlQuery -and
169- -not $Parameter.Count -and
175+ if ($script :GraphQLOutputCache .$gqlQuery -and
176+ -not $Parameter.Count -and
170177 -not $Refresh
171178 ) {
172179 $script :GraphQLOutputCache .$gqlQuery
173180 continue nextQuery
174181 }
175182
183+ $queryOutPath =
184+ if ($OutputPath ) {
185+ if ($OutputPath [$queryNumber ]) {
186+ $OutputPath [$queryNumber ]
187+ } else {
188+ $OutputPath [-1 ]
189+ }
190+ }
191+
192+
193+
176194 # region Run the Query
177195 $invokeSplat.Body = [Ordered ]@ {query = $gqlQuery }
178196 if ($Parameter ) {
@@ -184,16 +202,17 @@ function Get-GQL
184202 continue nextQuery
185203 }
186204 $invokeSplat.Body = ConvertTo-Json - InputObject $invokeSplat.Body - Depth 10
187- $shouldProcessMessage = " Querying $GraphQLUri with $gqlQuery "
205+ $shouldProcessMessage = " Querying $GraphQLUri with $gqlQuery "
188206 if (-not $PSCmdlet.ShouldProcess ($shouldProcessMessage )) {
189207 continue nextQuery
190208 }
191209 $gqlOutput = Invoke-RestMethod @invokeSplat * > & 1
192210 if ($gqlOutput -is [Management.Automation.ErrorRecord ]) {
193211 $PSCmdlet.WriteError ($gqlOutput )
194212 continue nextQuery
195- }
196- elseif ($gqlOutput.errors ) {
213+ }
214+
215+ if ($gqlOutput.errors ) {
197216 foreach ($gqlError in $gqlOutput.errors ) {
198217 $psCmdlet.WriteError ((
199218 [Management.Automation.ErrorRecord ]::new(
@@ -205,33 +224,33 @@ function Get-GQL
205224 }
206225 continue nextQuery
207226 }
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
219230 }
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 ])
229240 }
230- $gqlOutput
231241 }
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+ }
232252 # endregion Run the Query
233253 # endregion Handle Each Query
234-
235254 }
236255 }
237256}
0 commit comments