Custom GraphQL Resolver with Pagination Strapi v4

Hey there,

So I’m trying to get a collection type (in this case, called “alerts” and a custom resolver called “GetMyAlerts”) with pagination.

I’ve sort’ve got something going, it takes in a pagination arg and returns the collection data with toEntityResponseCollection, which I thought might magically transform pagination somehow, but alas, I’m not sure where to go from here.

nexus.extendType({
          type: "Query",
          definition(t) {
            t.field("getMyAlerts", {

              type: "AlertEntityResponseCollection",
              args: {
                pagination: nexus.arg({ type: "PaginationArg" }),
              },

              resolve: async (parent, args, ctx) => {
                const { toEntityResponseCollection } = strapi
                  .plugin("graphql")
                  .service("format").returnTypes;

                let alerts = await strapi.entityService.findMany(
                  "api::alert.alert",
                  {
                    filters: {
                      User: ctx.state.user.id,
                    },
                    sort: [
                      {
                        createdAt: "desc",
                      },
                    ],
                    start: args.pagination.page,
                    limit: args.pagination.pageSize,
                  }
                );

                let returnTo = toEntityResponseCollection(alerts, {
                  args: args,
                  resourceUID: "api::alert.alert",
                });

                return returnTo;
              },
            });
          },
        }),

Any ideas or could you point me in the right direction?

1 Like