Skip to content

Commit 9ced3b8

Browse files
committed
faster graph response times for cached data
1 parent bdf545d commit 9ced3b8

2 files changed

Lines changed: 43 additions & 1 deletion

File tree

Modules/CIPPCore/Public/GraphRequests/Get-GraphRequestList.ps1

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,8 @@ function Get-GraphRequestList {
8080
[string]$ReverseTenantLookupProperty = 'tenantId',
8181
[boolean]$AsApp = $false,
8282
[string]$Caller = 'Get-GraphRequestList',
83-
[switch]$UseBatchExpand
83+
[switch]$UseBatchExpand,
84+
[switch]$RawJsonArray
8485
)
8586

8687
$SingleTenantThreshold = 8000
@@ -423,6 +424,22 @@ function Get-GraphRequestList {
423424
}
424425
}
425426
} else {
427+
if ($RawJsonArray.IsPresent) {
428+
# Fast path: concatenate raw JSON strings without deserialization. This is much faster and uses less memory when no post-processing is needed, especially for large datasets.
429+
$JsonParts = [System.Collections.Generic.List[string]]::new()
430+
foreach ($Row in $Rows) {
431+
if ($Row.Data) {
432+
$d = $Row.Data.Trim()
433+
if ($d.Length -gt 2 -and $d[0] -eq '[' -and $d[-1] -eq ']') {
434+
$JsonParts.Add($d.Substring(1, $d.Length - 2))
435+
} elseif ($d.Length -gt 0 -and $d -ne '[]') {
436+
$JsonParts.Add($d)
437+
}
438+
}
439+
}
440+
return '[' + ($JsonParts -join ',') + ']'
441+
}
442+
426443
foreach ($Row in $Rows) {
427444
if ($Row.Data) {
428445
try {

Modules/CIPPHTTP/Public/Entrypoints/HTTP Functions/CIPP/Core/Invoke-ListGraphRequest.ps1

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,13 +122,38 @@ function Invoke-ListGraphRequest {
122122

123123
$Metadata = $GraphRequestParams
124124

125+
# Use raw JSON passthrough for AllTenants cached results when no post-processing is needed.
126+
$UseRawJson = $Request.Query.TenantFilter -eq 'AllTenants' -and
127+
-not $Request.Query.ListProperties -and
128+
-not $Request.Query.Sort -and
129+
-not $Request.Query.QueueId
130+
125131
try {
132+
if ($UseRawJson) {
133+
$GraphRequestParams.RawJsonArray = $true
134+
}
126135
$Results = Get-GraphRequestList @GraphRequestParams
127136

128137
if ($script:LastGraphResponseHeaders) {
129138
$Metadata.GraphHeaders = $script:LastGraphResponseHeaders
130139
}
131140

141+
# RawJsonArray returns a JSON string directly — skip object-level processing
142+
if ($UseRawJson -and $Results -is [string] -and $Results.StartsWith('[')) {
143+
if ($Request.Headers.'x-ms-coldstart' -eq 1) {
144+
$Metadata.ColdStart = $true
145+
}
146+
$MetadataJson = ConvertTo-Json -InputObject $Metadata -Depth 5 -Compress
147+
$GraphRequestData = '{"Results":' + $Results + ',"Metadata":' + $MetadataJson + '}'
148+
$StatusCode = [HttpStatusCode]::OK
149+
150+
return ([HttpResponseContext]@{
151+
StatusCode = $StatusCode
152+
ContentType = 'application/json'
153+
Body = $GraphRequestData
154+
})
155+
}
156+
132157
if ($Results | Where-Object { $_.PSObject.Properties.Name -contains 'nextLink' }) {
133158
if (![string]::IsNullOrEmpty($Results.nextLink) -and $Request.Query.TenantFilter -ne 'AllTenants') {
134159
Write-Host "NextLink: $($Results.nextLink | Where-Object { $_ } | Select-Object -Last 1)"

0 commit comments

Comments
 (0)