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

Improve paging functionality #2095

Open
mkomko opened this issue Jul 24, 2024 · 1 comment
Open

Improve paging functionality #2095

mkomko opened this issue Jul 24, 2024 · 1 comment
Labels
type:feature New experience request

Comments

@mkomko
Copy link

mkomko commented Jul 24, 2024

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

@mkomko mkomko added status:waiting-for-triage An issue that is yet to be reviewed or assigned type:feature New experience request labels Jul 24, 2024
@rkodev rkodev removed their assignment Aug 23, 2024
@rkodev
Copy link

rkodev commented Aug 23, 2024

@mkomko thanks for using the Java SDK. We will be considering the proposal to improve the experience for Page iteration

@andrueastman andrueastman removed the status:waiting-for-triage An issue that is yet to be reviewed or assigned label Aug 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type:feature New experience request
Projects
None yet
Development

No branches or pull requests

3 participants