Skip to content

Commit

Permalink
feat: Get-GQL -Cache ( Fixes #2, Fixes #31 )
Browse files Browse the repository at this point in the history
  • Loading branch information
James Brundage committed Dec 18, 2024
1 parent 61ba116 commit 3622960
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion Commands/Get-GQL.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ function Get-GQL
# * Format the output object any way we want
[Alias('Decorate','Decoration','PSTypeNames','TypeName','TypeNames','Type')]
[string[]]
$PSTypeName
$PSTypeName,

# If set, will cache the results of the query.
# This can be useful for queries that would be run frequently, but change infrequently.
[Parameter(ValueFromPipelineByPropertyName)]
[switch]
$Cache
)

process {
Expand Down Expand Up @@ -144,6 +150,17 @@ function Get-GQL
}
#endregion Check for File or Cached Query

if ($Cache -and -not $script:GraphQLOutputCache) {
$script:GraphQLOutputCache = [Ordered]@{}
}

if ($script:GraphQLOutputCache.$gqlQuery -and
-not $Parameter.Count
) {
$script:GraphQLOutputCache.$gqlQuery
continue nextQuery
}

#region Run the Query
$invokeSplat.Body = [Ordered]@{query = $gqlQuery}
if ($Parameter) {
Expand Down Expand Up @@ -183,6 +200,9 @@ function Get-GQL
$gqlOutput.data.pstypenames.add($pstypename[$goBackwards])
}
}
if ($Cache) {
$script:GraphQLOutputCache[$gqlQuery] = $gqlOutput.data
}
$gqlOutput.data
}
elseif ($gqlOutput) {
Expand All @@ -192,6 +212,9 @@ function Get-GQL
$gqlOutput.pstypenames.add($pstypename[$goBackwards])
}
}
if ($Cache) {
$script:GraphQLOutputCache[$gqlQuery] = $gqlOutput
}
$gqlOutput
}
#endregion Run the Query
Expand Down

0 comments on commit 3622960

Please sign in to comment.