Skip to content

Make a Relay ConnectionField optimized to work with Query #89

Closed
@Fedalto

Description

@Fedalto

Graphene has the graphene.relay.ConnectionField, which slices a list/iterable to build the edges inside the Relay connection.
But it doesn't play well with DB queries. It calls len() with Query doesn't have.

graphene-sqlalchemy should have a implementation of the ConnectionField that is aware of the sqlalchemy's Query and use it to slice directly in the DB.

For now, I'm using this workaround:

class RelayConnectionField(graphene.relay.ConnectionField):
    @classmethod
    def resolve_connection(cls, connection_type, args, resolved):
        if isinstance(resolved, Query):
            len_ = resolved.count()
            connection = connection_from_list_slice(
                resolved,
                args,
                connection_type=connection_type,
                edge_type=connection_type.Edge,
                pageinfo_type=PageInfo,
                slice_start=0,
                list_length=len_,
                list_slice_length=len_,
            )
            connection.iterable = resolved
            return connection
        else:
            return super().resolve_connection(connection_type, args, resolved)

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