microsoftgraph / microsoftgraph/msgraph-sdk-typescript

Easier way to handle batching with the new Graph SDK?

Open
#912 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

status:waiting-for-triage ToTriage
Dominant language
TypeScript
Stars
44
Forks
7
Avg merge
12h 40m
Merged PRs (30d)
5

Description

Is there a simple way to do batching with this SDK that I might be missing?

Because right now, this is the solution I landed on — it basically glues together three different libraries:

  • the new SDK (for creating the typesafe request),
  • the old Graph client (for sending the batch request), and
  • the @microsoft/microsoft-graph-types package (to type the output of the batch request).

While this works, it’s not really intuitive. The built-in batching in this SDK returns the body as an ArrayBuffer, so you have to deserialize it — which kind of defeats the whole purpose. Plus, there’s some overhead if you’re dealing with thousands of messages.

I’m just wondering, like I said, is there anything else I can do to make this cleaner or better?

import { MultipartBody } from "@microsoft/kiota-abstractions";
import { authProviderNew, RefreshTokenAuthProvider } from "./auth";
import {
  createGraphServiceClient,
  GraphRequestAdapter,
} from "@microsoft/msgraph-sdk";
import "@microsoft/msgraph-sdk-users";
import { BatchRequestContent } from "@microsoft/msgraph-sdk-core";

import { Client } from "@microsoft/microsoft-graph-client";
import * as MicrosoftGraph from "@microsoft/microsoft-graph-types";

const authProviderKiota = new RefreshTokenAuthProvider();
const adapter = new GraphRequestAdapter(authProviderKiota);
const client = createGraphServiceClient(adapter);

const clientGraph = Client.init({
  defaultVersion: "v1.0",
  debugLogging: false,
  authProvider: authProviderNew,
});

type BatchResponseItem = {
  id: string;
  status: number;
  headers: string[];
  body: MicrosoftGraph.Message;
};

type BatchResponse = { responses: BatchResponseItem[] };

export async function fetchMessagesByBatch(): Promise<
  MicrosoftGraph.Message[]
> {
  const batchLimit = 20;
  const allMessages: MicrosoftGraph.Message[] = [];

  const messageList = await client.me.messages.get({
    queryParameters: {
      select: ["id"],
    },
  });

  const messageIds = messageList?.value ?? [];

  for (let i = 0; i < messageIds.length; i += batchLimit) {
    const batchMessageIds = messageIds.slice(i, i + batchLimit);
    const batchRequestContent = new BatchRequestContent(adapter, {});

    for (const messageId of batchMessageIds) {
      const requestInfo = client.me.messages
        .byMessageId(messageId?.id!)
        .toGetRequestInformation({
          headers: {
            Prefer: 'outlook.body-content-type="text"',
          },
        });

      batchRequestContent.addBatchRequest(requestInfo);
    }

    const batchRequestBody = batchRequestContent.getContent();

    const batchResponse = await clientGraph
      .api("/$batch")
      .post({ requests: batchRequestBody.requests });

    const messages = batchResponse as BatchResponse;
    messages.responses.forEach((v) => allMessages.push(v.body));
  }

  console.dir(allMessages);
  return allMessages;
}

const test = await fetchMessagesByBatch();

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the SDK's built-in batching behavior, especially how it returns an ArrayBuffer, and compare it with the shown BatchRequestContent and old Graph client integration. Done should be a clearly supported batching workflow that preserves typed responses without requiring the old client or microsoft-graph-types package, while addressing the stated overhead for large message sets.

Written by the indexing model from the issue text.

Assessment

Tech stack
typescript
Domain
api, developer-experience
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.