microsoftgraph / microsoftgraph/msgraph-sdk-java

Improve paging functionality

Open
#2,095 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

type:feature
Dominant language
Java
Stars
444
Forks
154
Avg merge
18h 28m
Merged PRs (30d)
4

Description

Is your feature request related to a problem? Please describe the problem.

In my opinion, the code required for getting all paged results of a collection request is not ideal. In v5 I had a generic method that handled it for all types, but I couldn't achieve the same using v6. Mainly because the classes for the original request use a RequestConfiguration object and the classes for the paginated requests use a RequestInformation object and they seem to be wholly incompatible.

This is the best I could come up with:

public List<User> getAllUsers(List<String> selectAttributes, String filter)
{
    UserCollectionResponse userCollectionResponse = graphClient.users().get(
            requestConfig ->
            {
                if (!selectAttributes.isEmpty())
                {
                    requestConfig.queryParameters.select = selectAttributes.toArray(new String[0]);
                    if (selectAttributes.contains("manager"))
                        requestConfig.queryParameters.expand = new String[]{"manager"};
                }

                if (filter != null)
                    requestConfig.queryParameters.filter = filter;
            });

    UnaryOperator<RequestInformation> requestInformation =
            requestInfo ->
            {
                if (!selectAttributes.isEmpty())
                {
                    requestInfo.addQueryParameter("%24select", selectAttributes.toArray(new String[0]));
                    if (selectAttributes.contains("manager"))
                        requestInfo.addQueryParameter("%24expand", new String[]{"manager"});
                }

                if (filter != null)
                    requestInfo.addQueryParameter("%24filter", filter);

                return requestInfo;
            };

    return loadPagedEntities(userCollectionResponse, UserCollectionResponse::createFromDiscriminatorValue, requestInformation);
}

private <T extends Parsable> List<T> loadPagedEntities(
        BaseCollectionPaginationCountResponse baseCollectionPaginationCountResponse,
        Function<ParseNode, BaseCollectionPaginationCountResponse> collectionPageFactoryFunction,
        UnaryOperator<RequestInformation> requestInformation)
{
    try
    {
        List<T> entities = new ArrayList<>();

        PageIterator<T, BaseCollectionPaginationCountResponse> pageIterator =
                new PageIterator.Builder<T, BaseCollectionPaginationCountResponse>()
                        .client(graphClient)
                        .collectionPage(Objects.requireNonNull(baseCollectionPaginationCountResponse))
                        .collectionPageFactory(collectionPageFactoryFunction::apply)
                        .requestConfigurator(requestInformation)
                        .processPageItemCallback(entities::add)
                        .build();

        pageIterator.iterate();
        return entities;
    }
    catch (ReflectiveOperationException e)
    {
        throw new RuntimeException(e);
    }
}

What irks me most about this, is that I have to specify the query parameters twice and using different syntax! Especially the "%24..." is pretty ugly.

Describe the solution you'd like.

The best solution, of course, would be if the SDK handled this transparently:

List<User> allUsers = graphClient.users().getAll(requestConfig -> ...);

The next best solution would be some kind of compatibility between RequestConfiguration and RequestInformation, so the query parameters would only have to specified once.

What do you think?

Additional context?

No response

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 inspecting the Java SDK APIs named in the report: RequestConfiguration, RequestInformation, and PageIterator. Reproduce the sample paging flow and compare how query parameters are configured for the first and subsequent requests. Done means callers can configure paging query parameters without duplicating them or using encoded names such as "%24select" and "%24filter".

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.