Simplify your stack and build anything. Or everything.
Build tomorrow’s web with a modern solution you truly own.
Code-based nature means you can build on top of it to power anything.
It’s time to take back your content infrastructure.

Custom graphql resolever hasMore

default discord avatar
wakinki2 years ago
8

We've made this graphql resolever for payload, is there any way we could implement a hasMore return variable so we can know if a collection has more data / it doesnt ?



import { GraphQLExtension } from "payload/config";

const RtaGalleryImagesByCategories: GraphQLExtension = (GraphQL, payload) => {
  return {
    type: new GraphQL.GraphQLObjectType({
      name: "RtaGalleryImagesByCategories",
      fields: {
        docs: {
          type: new GraphQL.GraphQLList(
            payload.collections["rtaGalleryImages"].graphQL.type
          ),
        },
      },
    }),
    args: {
      categoryIds: {
        type: new GraphQL.GraphQLList(GraphQL.GraphQLInt),
      },
      locale: {
        type: GraphQL.GraphQLString,
      },
      fallbackLocale: {
        type: GraphQL.GraphQLString,
      },
      sort: {
        type: GraphQL.GraphQLString,
      },
      limit: {
        type: GraphQL.GraphQLInt,
      },
    },
    resolve: async (
      _: unknown,
      {
        categoryIds = [],
        locale = payload.config.localization &&
          payload.config.localization.defaultLocale,
        fallbackLocale = payload.config.localization &&
          payload.config.localization.defaultLocale,
        sort = "id",
        limit = 10,
      }: {
        categoryIds: number[];
        locale: string;
        fallbackLocale: string;
        sort: string;
        limit: number;
      }
    ) => {
      const data = await payload.find({
        collection: "rtaGalleryImages",
        depth: 0,
        locale: locale,
        fallbackLocale: fallbackLocale,
        sort: sort,
        limit: limit,
        where: {
          and: [
            {
              "categories.id": {
                in: categoryIds,
              },
            },
          ],
        },
      });

      return data;
    },
  };
};

export default RtaGalleryImagesByCategories;
  • default discord avatar
    notchr2 years ago

    Hmm, wouldnt you check the index of the last returned item versus the total?

  • default discord avatar
    wakinki2 years ago

    And how do I get the total ?



    In all the queries where I need this reslover I limit the number of queries and I filter by category



    I looked that the default payload resolver has something called hasNextPage but I have no idea how to implement that

  • default discord avatar
    notchr2 years ago
    @426393836739231754

    All find queries have pagination



    Which should provide a hasNextPage prop



    should be on your data object

  • default discord avatar
    wakinki2 years ago

    This is the query that uses our custom resolver



    With the payload resolver it works but I cant filter the categories thats why we made our own resolver

  • default discord avatar
    notchr2 years ago

    hmm



    Ill have to revisit this one because im not great with graphql. I'm going to ping

    @858693520012476436

    when they are on they may provide better insight

  • default discord avatar
    wakinki2 years ago

    Thanks

  • default discord avatar
    paulpopus2 years ago

    Ohh i see



    graphql is really finnicky



    but here in your type


    type: new GraphQL.GraphQLObjectType({
          name: "RtaGalleryImagesByCategories",
          fields: {
            docs: {
              type: new GraphQL.GraphQLList(
                payload.collections["rtaGalleryImages"].graphQL.type
              ),
            },
          },
        }),


    where you have

    docs

    next to it add any other fields you want and give it

    type

    from the GraphQL constructor...ill be honest that its not easy so you'll need to do a bit of trial and error to get it right



    then in your resolver when you return

    docs

    also return in the same object the other fields and they need to be of compatible types

  • default discord avatar
    wakinki2 years ago

    Figured it out, this is what we have now, thanks



     type: new GraphQL.GraphQLObjectType({
          name: "RtaGalleryImagesByCategories",
          fields: {
            docs: {
              type: new GraphQL.GraphQLList(
                payload.collections["rtaGalleryImages"].graphQL.type
              ),
            },
            totalDocs: {
              type: GraphQL.GraphQLInt,
            },
            totalPages: {
              type: GraphQL.GraphQLInt,
            },
            hasPrevPage: {
              type: GraphQL.GraphQLBoolean,
            },
            hasNextPage: {
              type: GraphQL.GraphQLBoolean,
            },
          },
        }),
Star on GitHub

Star

Chat on Discord

Discord

online

Can't find what you're looking for?

Get dedicated engineering support directly from the Payload team.