Skip to content

Commit 3622960

Browse files
author
James Brundage
committed
feat: Get-GQL -Cache ( Fixes #2, Fixes #31 )
1 parent 61ba116 commit 3622960

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

Commands/Get-GQL.ps1

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,13 @@ function Get-GQL
6868
# * Format the output object any way we want
6969
[Alias('Decorate','Decoration','PSTypeNames','TypeName','TypeNames','Type')]
7070
[string[]]
71-
$PSTypeName
71+
$PSTypeName,
72+
73+
# If set, will cache the results of the query.
74+
# This can be useful for queries that would be run frequently, but change infrequently.
75+
[Parameter(ValueFromPipelineByPropertyName)]
76+
[switch]
77+
$Cache
7278
)
7379

7480
process {
@@ -144,6 +150,17 @@ function Get-GQL
144150
}
145151
#endregion Check for File or Cached Query
146152

153+
if ($Cache -and -not $script:GraphQLOutputCache) {
154+
$script:GraphQLOutputCache = [Ordered]@{}
155+
}
156+
157+
if ($script:GraphQLOutputCache.$gqlQuery -and
158+
-not $Parameter.Count
159+
) {
160+
$script:GraphQLOutputCache.$gqlQuery
161+
continue nextQuery
162+
}
163+
147164
#region Run the Query
148165
$invokeSplat.Body = [Ordered]@{query = $gqlQuery}
149166
if ($Parameter) {
@@ -183,6 +200,9 @@ function Get-GQL
183200
$gqlOutput.data.pstypenames.add($pstypename[$goBackwards])
184201
}
185202
}
203+
if ($Cache) {
204+
$script:GraphQLOutputCache[$gqlQuery] = $gqlOutput.data
205+
}
186206
$gqlOutput.data
187207
}
188208
elseif ($gqlOutput) {
@@ -192,6 +212,9 @@ function Get-GQL
192212
$gqlOutput.pstypenames.add($pstypename[$goBackwards])
193213
}
194214
}
215+
if ($Cache) {
216+
$script:GraphQLOutputCache[$gqlQuery] = $gqlOutput
217+
}
195218
$gqlOutput
196219
}
197220
#endregion Run the Query

0 commit comments

Comments
 (0)