4

I have used connection helper for implementing pagination in graphql apis.I want to implement connection more than once for same model but it gives me duplicate type error. Can anyone suggest solution for supporting pagination for more than one apis of same model. I have used below code.

connection :employees_index, function: Queries::Employees::Index.new

connection :employees_user_index, function: Queries::Employees::UserIndex.new

darshi kothari
  • 642
  • 1
  • 5
  • 12

1 Answers1

2

I know it's late and you might already get a solution but recently I faced the same issue and searched for the solution but couldn't find it. So I resolved it by my way. I think it might be useful for others:

Solution:

You can resolve it by having a connection class; inside "graphql/connection" directory within app directory; like this:

class Connections::EmployeesConnection < GraphQL::Function
   description 'Employees Connection'

   type Types::EmployeeType.define_connection
end

Now use this class as a superclass for your query/mutation class like this:

# Query class
class Queries::Employees::Index < Connections::EmployeesConnection
   def call(obj, args, ctx)
      # Do stuff here
   end
end

Same for user_index:

# Query class
class Queries::Employees::UserIndex < Connections::EmployeesConnection
   def call(obj, args, ctx)
      # Do stuff here
   end
end

Similarly, you can use the same connection class for other employee queries and mutations; it won't give you an error for a duplicate definition of connection.

Ajay Babar
  • 21
  • 3