Skip to content

Add pagination args to generated Connections #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
1 of 3 tasks
jptrindade opened this issue Mar 6, 2025 · 1 comment
Open
1 of 3 tasks

Add pagination args to generated Connections #236

jptrindade opened this issue Mar 6, 2025 · 1 comment

Comments

@jptrindade
Copy link

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
            }
          }
        }
      }
    }
  }
}
@Ckk3
Copy link
Contributor

Ckk3 commented Mar 10, 2025

Thanks so much for your code example, @jptrindade , this feature is already in our radar but no one is working on it now.
Please fell free to start a PR if you want 😊

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants