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

Feature request: Modify final result of crud #1012

Open
Akxe opened this issue Jan 5, 2021 · 1 comment
Open

Feature request: Modify final result of crud #1012

Akxe opened this issue Jan 5, 2021 · 1 comment

Comments

@Akxe
Copy link

Akxe commented Jan 5, 2021

Hi, I would like to discuss the ability to change the final result of the crud via resolve.

The example below is simple, but the query I am trying to solve is composed of 10 nested object types and multiple computed fields. I also don't want to provide the user with these filters, I need to apply them to filter out, parts that don't make sense, or even should not be seen by him!

export const ClientOffer = objectType({
    name: 'ClientOffer',
    definition(t) {
        const model = t.model('Offer');
        model.offerID();
        model.title();
        model.carriersOfOffer({
            pagination: false,
        });
    },
});

export extendType({
    type: 'Query',
    definition(t) {
        t.crud.offer({
            alias: 'clientOffer',
            type: 'ClientOffer',
            async resolve(root, args, ctx, info, originalResolve) {
                const resolved = await originalResolve(root, args, ctx, info);

                //    This won't work, since `carriersOfOffer` will be undefined. It is part of other `objectType`
                const replacementCategoryID = 4;
                resolve.carriersOfOffer.filter(carreir => {
                    return carrier.periods.some(period => {
                        return period.selected 
                            || period.groups.some(group => group.categoryID == replacementCategoryID);
                    });
                });

                return resolved;
            },
        });
    },
});

Above is a very simple example of data, that needs to be filtered, it is part of business logic, no need to send client data that he will never see.

In my actual code, I need to filter multiple properties, that are nested on multiple different levels.


I would like to propose a solution that I have in mind to solve this. A method that can manipulate data after it has been processed from DB.

definition(t) {
    t.crud.offer({
        alias: 'clientOffer',
        type: 'ClientOffer',
        async onceResolved(root, args, ctx, info, resolved) {
            return {
                ...resolved,
                carriersOfOffer: resolve.carriersOfOffer.filter(carreir => {
                    return carrier.periods.some(period => {
                        return period.selected 
                            || period.groups.some(group => group.categoryID == replacementCategoryID);
                    });
                }),
            };
        },
    });
},
@Akxe
Copy link
Author

Akxe commented Jan 21, 2021

@Weakky I would like to ask if there are any plans to support this kind of thing. I need it quite a bit, but I am not sure what to do about it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant