Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting drive items requires 2 separate requests in Microsoft Graph SDK v6 #2275

Closed
ernar23 opened this issue Jan 14, 2025 · 6 comments
Closed
Assignees
Labels
type:bug A broken experience

Comments

@ernar23
Copy link

ernar23 commented Jan 14, 2025

In Microsoft Graph SDK v6, the way to get drive items has changed a bit (based on https://github.com/microsoftgraph/msgraph-sdk-java/blob/main/docs/upgrade-to-v6.md#drive-item-paths), and now getting items by userId requires 2 separate requests:

Get drive by userId:

Drive drive = graphClient
         .users()
         .byUserId(userId)
         .drive()
         .get();

Get items:

DriveItem = graphClient
    .drives()
    .byDriveId(drive.getId())
    .items()
    .byDriveItemId(itemId)
    .get();

In Microsoft Graph SDK Java v5, it is possible to get a drive item in one request:

DriveItem driveItem = graphClient
        .users(userId)
        .drive()
        .items(itemId)
        .buildRequest()
        .get();

2 questions:

  1. Is it still possible to make only 1 request in the example above using Microsoft Graph SDK v6.
  2. If there is no way to do that, is it possible to use batch requests for the case above, where the second request will be dependent on the response of the first one?

This is concerning because we will reach throttling 2x faster if we use Microsoft Graph SDK v6.

@ernar23 ernar23 added the status:waiting-for-triage An issue that is yet to be reviewed or assigned label Jan 14, 2025
@Ndiritu Ndiritu self-assigned this Jan 23, 2025
@Ndiritu Ndiritu added type:enhancement Enhancement request targeting an existing experience type:bug A broken experience and removed status:waiting-for-triage An issue that is yet to be reviewed or assigned type:enhancement Enhancement request targeting an existing experience labels Jan 23, 2025
@Ndiritu
Copy link
Contributor

Ndiritu commented Jan 23, 2025

Thanks for reaching out @ernar23.

Yes, batch requests should be possible however I found a bug when trying to implement a sample for this. Should have this resolved in the next couple of days.

@ernar23
Copy link
Author

ernar23 commented Jan 24, 2025

Thanks for your response, @Ndiritu.

I will reiterate my first question: Is there a way to send such requests as one single request instead of a batch request using SDK request and model classes?

I found a way how I can do it using com.microsoft.kiota.RequestInformation as below:

RequestInformation requestInformation = new RequestInformation();
requestInformation.httpMethod = HttpMethod.GET;
requestInformation.urlTemplate = "{+baseurl}/users/{userId}/drive/items/{itemId}";
requestInformation.headers.add("Accept", "application/json");
Map<String, Object> pathParameters = new HashMap<>();
pathParameters.put("baseurl", graphClient.getRequestAdapter().getBaseUrl());
pathParameters.put("userId", userId);
pathParameters.put("itemId", itemId);
requestInformation.pathParameters = pathParameters;
HashMap<String, ParsableFactory<? extends Parsable>> errorMapping = new HashMap<>();
errorMapping.put("XXX", ODataError::createFromDiscriminatorValue);
DriveItem driveItem = graphClient.getRequestAdapter().send(requestInformation, errorMapping, DriveItem::createFromDiscriminatorValue);

This approach is too manual and requires boilerplate code, which essentially nullifies the benefits of using the SDK.

@Ndiritu
Copy link
Contributor

Ndiritu commented Jan 24, 2025

@ernar23 yes it's possible without executing a batch request.
You can over-write the request URL using the withUrl() method. You'd only need to be careful to call it on a path that returns the deserialized type that you expect. In this case:

DriveItem driveItem = graphServiceClient.drives().byDriveId("").items().byDriveItemId("").withUrl(
    "https://graph.microsoft.com/v1.0/users/{user-id}/drives/{drive-id}/items/{item-id}"
).get();

@ernar23
Copy link
Author

ernar23 commented Jan 27, 2025

Thanks @Ndiritu , it seems to work.

What was the reason for removing those paths from v6? They exist in v5 and were very helpful in creating requests by userId.

@Ndiritu
Copy link
Contributor

Ndiritu commented Jan 28, 2025

@ernar23 we did this to reduce the size of the generated SDK by generating only the canonical paths to resources e.g. to access drives etc using /drive as the root.

@andrueastman andrueastman assigned andrueastman and unassigned Ndiritu Feb 6, 2025
@andrueastman
Copy link
Member

As guided above, including all the possible paths for drive would drastically increase the size of the SDK due the increase in redundant paths. The way forward would be to use the WithUrl method to override the path to avoid the making multiple calls as guided by @Ndiritu

We'll close this one for now. Please feel free to create a new issue incase of anything.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:bug A broken experience
Projects
None yet
Development

No branches or pull requests

3 participants