Skip to content

Add pagination args to generated Connections #236

@jptrindade

Description

@jptrindade

Feature Request Type

  • Core functionality
  • Alteration (enhancement/optimization) of existing feature(s)
  • New behavior

Description

When we generate the Connections for a given sqlalchemy relationship we are not including pagination arguments. I saw a TODO that might be related but there was no ticket for it yet.
Below is an example of how my models/schema looks like:

class Base(DeclarativeBase):
    pass


class Tree(Base):
    __tablename__ = "tree"
    __table_args__ = (
        PrimaryKeyConstraint("id", name="tree_pkey"),
        UniqueConstraint("name", name="tree_name_key"),
    )

    id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True)
    name: Mapped[str] = mapped_column(String)

    fruit: Mapped[List["Fruit"]] = relationship(
        "Fruit", back_populates="tree_"
    )

class Fruit(Base):
    __tablename__ = "fruit"
    __table_args__ = (
        ForeignKeyConstraint(
            ["tree"],
            ["tree.id"],
            ondelete="CASCADE",
            name="fruit_tree_fkey",
        ),
        PrimaryKeyConstraint("id", name="fruit_pkey"),
        Index("fruit_name_tree_index", "tree", "name", unique=True),
    )

    id: Mapped[uuid.UUID] = mapped_column(UUID, primary_key=True)
    name: Mapped[str] = mapped_column(String)
    tree: Mapped[uuid.UUID] = mapped_column(UUID)

    tree_: Mapped["Tree"] = relationship("Tree", back_populates="fruit")
....
@mapper.type(models.Fruit)
class Fruit(relay.Node):
    id: relay.NodeID[uuid.UUID]

@mapper.type(models.Tree)
class Tree(relay.Node):
    id: relay.NodeID[uuid.UUID]


@strawberry.type
class Query:

    # I have some extra logic (filtering/sorting) before pagination
    @relay.connection(relay.ListConnection[Tree])
    async def trees(
        self,
        order_by: TreeOrder | None = None,
        filter: TreeFilter | None = None,
    ) -> typing.Iterable[Tree]:
        async with get_async_session() as session:
            stmt = select(models.Tree)
            stmt = add_filter_and_sort(stmt, filter, order_by)
            _trees = await session.scalars(stmt)
            return _trees.all()

I would like to be able to do a query like this:

{
  trees{
    edges{
      node{
        name
        fruit(first:2){
          pageInfo{
            hasNextPage
          }
          edges{
            node{
              name
            }
          }
        }
      }
    }
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions