How to have an offset paginated query and a cursor paginated query with Nestjs-query?

39 views Asked by At

I'm using nestjs-query, and I have an entity (TodoItem). I would like to generate 2 resolvers/queries to fetch many todos:

  • A cursor paginated one
  • An offset paginated one.

This is what I did:

      resolvers: [
        {
          DTOClass: TodoItemDTO,
          EntityClass: TodoItemEntity,
          create: { disabled: true },
          update: { disabled: true },
          delete: { disabled: true },
          read: {
            one: { disabled: true },
            many: {
              name: 'todoItemCursor',
            },
          },
        },
        {
          DTOClass: TodoItemDTO,
          EntityClass: TodoItemEntity,
          pagingStrategy: PagingStrategies.OFFSET,
          create: { disabled: true },
          update: { disabled: true },
          delete: { disabled: true },
          read: {
            one: { disabled: true },
            many: {
              name: 'todoItemOffset',
            },
          },
        },
      ],

But it crashes with the error:

/Users/valentin/Perso/Projects/reproducible/node_modules/graphql/type/schema.js:219
        throw new Error(
              ^
Error: Schema must contain uniquely named types but contains multiple types named "TodoItemConnection".
    at new GraphQLSchema (/Users/valentin/Perso/Projects/reproducible/node_modules/graphql/type/schema.js:219:15)
    at GraphQLSchemaFactory.create (/Users/valentin/Perso/Projects/reproducible/node_modules/@nestjs/graphql/dist/schema-builder/graphql-schema.factory.js:38:24)
    at GraphQLSchemaBuilder.generateSchema (/Users/valentin/Perso/Projects/reproducible/node_modules/@nestjs/graphql/dist/graphql-schema.builder.js:35:52)
    at GraphQLSchemaBuilder.build (/Users/valentin/Perso/Projects/reproducible/node_modules/@nestjs/graphql/dist/graphql-schema.builder.js:22:31)
    at GraphQLFactory.generateSchema (/Users/valentin/Perso/Projects/reproducible/node_modules/@nestjs/graphql/dist/graphql.factory.js:27:69)
    at ApolloDriver.generateSchema (/Users/valentin/Perso/Projects/reproducible/node_modules/@nestjs/graphql/dist/drivers/abstract-graphql.driver.js:23:36)
    at GraphQLModule.onModuleInit (/Users/valentin/Perso/Projects/reproducible/node_modules/@nestjs/graphql/dist/graphql.module.js:109:54)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at callModuleInitHook (/Users/valentin/Perso/Projects/reproducible/node_modules/@nestjs/core/hooks/on-module-init.hook.js:51:9)
    at NestApplication.callInitHook (/Users/valentin/Perso/Projects/reproducible/node_modules/@nestjs/core/nest-application-context.js:223:13)

I see it is possible to provide a connectionName to the ReadResolverOpts<DTO> :

          read: {
            connectionName: 'TodoItemOffsetConnection',
            one: { disabled: true },
            many: {
              name: 'todoItemOffset',
            },
          },

But it doesn't seem to do anything.


How can I have both a cursor paginated and an offset paginated query?

1

There are 1 answers

0
Valentin Vignal On BEST ANSWER

The solution was to use dtoName:

      resolvers: [
        {
          DTOClass: TodoItemDTO,
          dtoName: 'TodoItemCursor',
          EntityClass: TodoItemEntity,
          create: { disabled: true },
          update: { disabled: true },
          delete: { disabled: true },
          read: {
            one: { disabled: true },
            many: {
              name: 'todoItemCursor',
            },
          },
        },
        {
          DTOClass: TodoItemDTO,
          dtoName: 'TodoItemOffset',
          EntityClass: TodoItemEntity,
          pagingStrategy: PagingStrategies.OFFSET,
          create: { disabled: true },
          update: { disabled: true },
          delete: { disabled: true },
          read: {
            one: { disabled: true },
            many: {
              name: 'todoItemOffset',
            },
          },
        },
      ],