TriPSs/nestjs-query

Cannot use 2 offset and cursor pagination at the same time

Closed this issue · 8 comments

Describe the bug

I have an entity todo-item, and I'm trying to create 2 resolvers:

  • An offset paginated one
  • A cursor paginated one

Have you read the Contributing Guidelines?

Yes

To Reproduce
Steps to reproduce the behavior:

  1. Follow the steps to install the dependencies and create the example from https://tripss.github.io/nestjs-query/docs/introduction/example (or checkout https://github.com/ValentinVignal/reproducible/tree/ptc-org/nestjs-query/cursor-and-offset-pagination)
  2. Update the todo item module resolvers to :
      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',
            },
          },
        },
      ],
  1. run npm run start

Expected behavior

I would want to have 2 resolvers, one with cursor pagination and one with offset pagination.

Current behavior

Instead I get 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)

Desktop (please complete the following information):

  • Node Version: v18.18.2
  • Nestjs-query Version
├── @ptc-org/nestjs-query-core@4.3.1
├─┬ @ptc-org/nestjs-query-graphql@4.3.1
│ └── @ptc-org/nestjs-query-core@4.3.1 deduped
└─┬ @ptc-org/nestjs-query-typeorm@4.3.1
  └── @ptc-org/nestjs-query-core@4.3.1 deduped

I tried to see if there was a way to give a custom name to the connections, but I couldn't find it. Maybe a connectionName parameter could be added somewhere so I could specify connectionName: 'TodoItemCusorConnection' and connectionName: 'TodoItemOffsetConnection'

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

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

but it is being overridden in

const { QueryArgs = QueryArgsType(DTOClass, { ...opts, connectionName: `${baseName}Connection` }) } = opts

For the ones looking for a workaround, I managed to got both by doing:

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

But I think this issue should stay open as connectionName doesn't seem to do anything because it always overridden

Hi @ValentinVignal, thanks for reporting, will try to check this out soon.

@TriPSs I wouldn't mind trying to work on this, I sent you a message for some help/advices

@ValentinVignal that would be awesome, thanks! Let me know if you need any help.

How should I contact you if I have some questions ?

You can comment here or if you have a more general question inside discussions, I receive emails for all notifications and tend to reply when I see them :)

@ValentinVignal FYI: I also created a slack (See README), let's see if that works.