Easier way to handle batching with the new Graph SDK?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 25/100
- Issue type
- Feature
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- typescript
- Domain
- api, developer-experience
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.
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-typespackage (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();
- Dominant language
- TypeScript
- Stars
- 44
- Forks
- 7
- Avg merge
- 12h 40m
- Merged PRs (30d)
- 5
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from microsoftgraph/msgraph-sdk-typescript
-
ToTriage type:bug
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
microsoftgraph/msgraph-sdk-typescript#917 · 6 comments ·
-
status:waiting-for-triage ToTriage
Difficulty 4/5 3-5 days Newbie friendliness 48/100
microsoftgraph/msgraph-sdk-typescript#974 · 1 comment ·
-
status:waiting-for-triage ToTriage type:bug
Difficulty 4/5 3-5 days Newbie friendliness 25/100
microsoftgraph/msgraph-sdk-typescript#953 · 1 comment · 1 reaction ·
-
status:waiting-for-triage ToTriage type:feature
Difficulty 3/5 1-2 days Newbie friendliness 48/100
-
status:waiting-for-triage ToTriage type:feature
Difficulty 4/5 3-5 days Newbie friendliness 45/100
All issues in microsoftgraph/msgraph-sdk-typescript
Similar issues
-
comp/desktop P3 type/bug
Difficulty 1/5 Under an hour Newbie friendliness 92/100
NousResearch/hermes-agent#118866 ·
-
Browser Waiting for: Product Owner
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
getsentry/sentry-javascript#24577 · 1 comment ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
agilepathway/label-checker#640 ·
-
Plugin stuck at "loading" on DSH 0.1.6-alpha.2 — turnTail list slot registration missing options.id Open
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
chrisparsons83/flexspotff#153 ·