Skip to content

Response and pagination metadata

Mechanism

AzureResourceGraph keeps its continuation state on the table. GetPage attaches meta [SkipToken = nextSkipToken] to its result (line 83).1 The ARGImpl callback reads that value with Value.Metadata(previous)[SkipToken]?. The first call starts from an empty string, and a null read ends paging (lines 56–59).2

GetPage sets its local resultTruncated to true when the option is absent or null, making nextSkipToken null. The default therefore stops after one page. Any non-null option value makes that local flag false and reads the continuation token from the body, including an explicit false. The type's sample {true} is an example value, not a declared default. Test omission, null, true, and false separately when adapting this contract.13

The HTTP status is a different contract. WaitForResult reads Value.Metadata(result)[Response.Status] on a Web.Contents result (lines 96–98).4 That is response state, not page state, and the presence of Value.Metadata alone does not show which contract is in use.

Selection and dependencies

Attach page state as metadata when it is not part of the item rows and the next call can read it. The input is the continuation field from the response body, read by GetSkipToken. The output is a SkipToken value on the table. A later transform can lose metadata; check the actual returned value rather than assuming every transform preserves or drops it.5

For header or status handling, DeltaSharing's SendRequest is the contrast. It reads Value.Metadata(resp) before buffering, then passes the captured record to HandleError. HandleError maps 400, 403, and 404 to errors and returns other responses unchanged.7 Use pagination for the callback shape and web requests for request options.

Adaptation and failure paths

Keep attachment and retrieval together. If an intermediate step loses SkipToken metadata, the callback's optional read produces null and ends paging without an error. Test that boundary after changing the table transforms.1

GetSkipToken uses Record.FieldOrDefault: a missing token field or null body yields null. A non-null body of the wrong type can raise instead. A changed response shape can therefore resemble a final page; validate the body contract before treating a null token as successful completion.5

The first request sends an empty token. nextToken is "" on the first call (line 56). AddFieldOrNull adds $skipToken whenever the value is not null (line 137), and "" is not null, so the first body includes the field.6 Check that the API accepts an empty initial token before you copy the pattern.

Not every option value is a page setting. resultTruncated is tested only for presence (line 73), so false enables continuation too. A caller who passes false to mean "no paging" gets the opposite behavior.1

Validation checklist

  • Decide the initial token: send "" as this source does, or omit the field.
  • Confirm the token is a top-level field of the body (GetSkipToken, line 153).
  • Confirm which option value enables continuation, and document the default.
  • Confirm that the table returned by the page function still carries its metadata.
  • Check the HTTP statuses that reach your code. ManualStatusHandling = {500, 400} (line 94) is the list this source reads manually.

Examples

Page state from GetPage, AzureResourceGraph lines 64–83:

AzureResourceGraph/AzureResourceGraph.pq · lines 64–83 Open original source ↗

GetPage = (query as text, skipToken as text, optional scope as nullable text, optional subscription as text, optional managementGroup as text, optional options as record) as table =>
    let
        scopeValue = if scope = null then "Tenant" else scope,
        subscription =  if subscription = null then "" else subscription,
        managementGroup =  if managementGroup = null then "" else managementGroup,
        scopeCheck = if scopeValue = "Tenant" and ((subscription <> "") or (managementGroup <> "")) then error Extension.LoadString("scopeCheckErrorMessage") else null,
        emptyScopeCheck = if scopeValue <> "Tenant" and ((subscription = "") and (managementGroup = "")) then error Extension.LoadString("expectedScopeParameters") else null,
        subscriptionValueCheck = if scopeValue = "Subscription" and managementGroup <> "" then error Extension.LoadString("ScopeSubscriptionMismatch") else null,
        managementgroupValueCheck = if scopeValue = "Management group" and subscription <> "" then error Extension.LoadString("ScopeManagementGroupMismatch") else null,
        resultTruncated = if options[resultTruncated]? = null then true else false,
        postData = BuildPostContent(query, skipToken, scopeValue, subscription, managementGroup, resultTruncated),
        response = WaitForResult(postData),
        body = if response[IsError] = false then response[Content] else null, 
        errorCheck = if response[IsError] then error response[Content] else null,
        nextSkipToken = if resultTruncated = true then null else GetSkipToken(body),
        resultTable = if errorCheck = null then Table.FromList(body[data]?, Splitter.SplitByNothing(), null, null, ExtraValues.Error) else null,
        processedResult = if errorCheck = null and not Table.IsEmpty(resultTable) then Table.ExpandRecordColumn(resultTable, "Column1", Record.FieldNames(Table.Column(resultTable, "Column1"){0})) else null,
        output = if processedResult = null then #table({"Results"}, {{"No results"}}) else processedResult
    in
        if scopeCheck <> null then scopeCheck else if emptyScopeCheck <> null then emptyScopeCheck else if subscriptionValueCheck <> null then subscriptionValueCheck else if managementgroupValueCheck <> null then managementgroupValueCheck else if errorCheck <> null then errorCheck else output meta [SkipToken = nextSkipToken];

Callback that reads the token, lines 52–62:

AzureResourceGraph/AzureResourceGraph.pq · lines 52–62 Open original source ↗

ARGImpl = (query as text, optional scope as nullable text, optional subscription as text, optional managementGroup as text, optional options as record) as table =>
    Table.GenerateByPage((previous) => 
        let
            // if previous is null, then this is our first page of data
            nextToken = if (previous = null) then "" else Value.Metadata(previous)[SkipToken]?,
            // if NextToken was set to null by the previous call, we know we have no more 
            delay = Number.RandomBetween(1, 3),
            page = if (nextToken <> null) then Function.InvokeAfter(() => GetPage(query, nextToken, scope, subscription, managementGroup, options), #duration(0,0,0,delay)) else null
        in
            page
    );

Status read before buffering, DeltaSharing lines 462–484:

DeltaSharing/DeltaSharing.pq · lines 462–484 Open original source ↗

SendRequest = (url as text, options as record) as binary =>
    let
        credential = Extension.CurrentCredential(),
        AuthenticationMode =  credential[AuthenticationKind],
        token = if AuthenticationMode = "Key" then credential[Key] else credential[access_token],
        headers = [#"Authorization" = "Bearer " & token] & (if options[Headers]? = null then [] else options[Headers]),
        resp = RetryWithBackoff(
            (isRetry as logical) => Web.Contents(url, options & [Headers=headers] & [ManualStatusHandling={400, 403, 404, 429, 503}] & [IsRetry = isRetry])
        ), 
        respMetadata = Value.Metadata(resp)
    in
        HandleError(Binary.Buffer(resp), respMetadata);

HandleError = (resp, respMetadata) =>
    let
        respCode = respMetadata[Response.Status],
        finalResp = 
            if respCode = 400 then error Error.Record("DataSource.Error", Text.Format(Extension.LoadString("ErrorInvalidConnectionString"), {Text.FromBinary(resp)}))
            else if respCode = 403 then error Extension.CredentialError(Credential.AccessForbidden, Text.Format(Extension.LoadString("ErrorInvalidCredentials"), {Text.FromBinary(resp)}))
            else if respCode = 404 then error Error.Record("DataSource.Error", Text.Format(Extension.LoadString("ErrorResourceNotFound"), {Text.FromBinary(resp)}))
            else resp
    in
        finalResp;

  1. AzureResourceGraph/AzureResourceGraph.pq lines 64–83 (GetPage): option check (line 73), token selection (78), request body (74), error branches, and meta [SkipToken = nextSkipToken] (83). ↩↩↩↩

  2. AzureResourceGraph/AzureResourceGraph.pq lines 52–62 (ARGImpl): the Table.GenerateByPage callback reads Value.Metadata(previous)[SkipToken]? (line 56) and gates the next page (line 59). ↩

  3. AzureResourceGraph/AzureResourceGraph.pq lines 32–41: the options type declares resultTruncated with Documentation.SampleValues = {true} (line 37). ↩

  4. AzureResourceGraph/AzureResourceGraph.pq lines 85–108 (WaitForResult): reads Response.Status from a Web.Contents result (lines 96–98) and handles 500 and 400 manually (line 94). ↩

  5. AzureResourceGraph/AzureResourceGraph.pq lines 151–153 (GetSkipToken): Record.FieldOrDefault(response, "$skipToken") on the parsed body. ↩↩

  6. AzureResourceGraph/AzureResourceGraph.pq lines 120–142 (BuildPostContent): AddFieldOrNull adds $skipToken when the value is not null (line 137). ↩

  7. DeltaSharing/DeltaSharing.pq lines 462–484: SendRequest captures Value.Metadata(resp) (line 471) before Binary.Buffer (line 473), and HandleError maps status codes (lines 475–484). ↩