Exemplo n.º 1
0
    def cursor_resolver(self,
                        instance,
                        info,
                        parent_resolver=None,
                        before=None,
                        after=None,
                        first=None,
                        last=None,
                        **kwargs):
        if parent_resolver:
            qs = parent_resolver(instance, info, **kwargs)
            qs = apply_ordering(qs, **kwargs)
        else:
            qs = instance.items
        if isinstance(qs, self.type):
            return qs
        page_query = PageQuery(before=before,
                               after=after,
                               first=first,
                               last=last)
        queryset, post_processors = optimize_qs(self.type, qs, info)

        # Filtering - copied from DjangoFilterConnectionField.connection_resolver
        filter_kwargs = {
            k: v
            for k, v in kwargs.items() if k in self.filtering_args
        }
        queryset = self.filterset_class(
            data=filter_kwargs,
            queryset=queryset,
        ).qs
        # End of filtering

        if isinstance(qs, list):
            connection = connection_from_list(
                queryset,
                connection_type=self.type,
                edge_type=self.type.Edge,
                pageinfo_type=graphene.relay.PageInfo,
                args=attr.asdict(page_query),
            )
        else:
            connection = connection_from_cursor_paginated(
                queryset,
                connection_type=self.type,
                edge_type=self.type.Edge,
                pageinfo_type=graphene.relay.PageInfo,
                page_query=page_query,
                total_queryset=queryset,
            )
        # Post processors currently only work when the processor model is the
        # topmost model, behaviour below that is undefined and may break
        for processor in post_processors:
            processor([edge.node for edge in connection.edges], info)
        return connection
Exemplo n.º 2
0
 def connection_resolver(resolver, connection, root, args, context, info):
     iterable = resolver(root, args, context, info)
     assert isinstance(iterable, Iterable), (
         'Resolved value from the connection field have to be iterable. '
         'Received "{}"').format(iterable)
     connection = connection_from_list(iterable,
                                       args,
                                       connection_type=connection,
                                       edge_type=connection.Edge,
                                       pageinfo_type=PageInfo)
     connection.iterable = iterable
     return connection
Exemplo n.º 3
0
    def resolve_connection(cls, connection_type, args, resolved):
        if isinstance(resolved, connection_type):
            return resolved

        assert isinstance(resolved, Iterable), (
            'Resolved value from the connection field have to be iterable or instance of {}. '
            'Received "{}"').format(connection_type, resolved)
        connection = connection_from_list(resolved,
                                          args,
                                          connection_type=connection_type,
                                          edge_type=connection_type.Edge,
                                          pageinfo_type=PageInfo)
        connection.iterable = resolved
        return connection
Exemplo n.º 4
0
    def resolve_connection(cls, connection_type, args, resolved):
        if isinstance(resolved, connection_type):
            return resolved

        assert isinstance(resolved, Iterable), (
            'Resolved value from the connection field have to be iterable or instance of {}. '
            'Received "{}"'
        ).format(connection_type, resolved)
        connection = connection_from_list(
            resolved,
            args,
            connection_type=connection_type,
            edge_type=connection_type.Edge,
            pageinfo_type=PageInfo
        )
        connection.iterable = resolved
        return connection
Exemplo n.º 5
0
    def connection_resolver(cls, resolver, connection, root, args, context,
                            info):
        resolved = resolver(root, args, context, info)

        if isinstance(resolved, connection):
            return resolved

        assert isinstance(resolved, Iterable), (
            'Resolved value from the connection field have to be iterable or instance of {}. '
            'Received "{}"').format(connection, resolved)
        connection = connection_from_list(resolved,
                                          args,
                                          connection_type=connection,
                                          edge_type=connection.Edge,
                                          pageinfo_type=PageInfo)
        connection.iterable = resolved
        return connection
Exemplo n.º 6
0
 def provides_deprecated_connection_from_list():
     with deprecated_call():
         # noinspection PyDeprecation
         c = connection_from_list(
             array_abcde[:1],
             args={},
             connection_type=Connection,
             edge_type=Edge,
             pageinfo_type=PageInfo,
         )
     assert c == Connection(
         edges=[edge_a],
         pageInfo=PageInfo(
             startCursor=cursor_a,
             endCursor=cursor_a,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Exemplo n.º 7
0
    def resolve_connection(cls, connection_type, args, resolved):
        if isinstance(resolved, connection_type):
            return resolved

        if not isinstance(resolved, Iterable):
            raise AssertionError("""
                Resolved value from the connection field have to be iterable or instance of {}.
                Received "{}"
            """.format(connection_type, resolved))

        connection = connection_from_list(
            resolved,
            args,
            connection_type=connection_type,
            edge_type=connection_type.Edge,
            pageinfo_type=PageInfo,
        )
        connection.iterable = resolved
        return connection
 def provides_deprecated_connection_from_list():
     with deprecated_call():
         # noinspection PyDeprecation
         c = connection_from_list(
             letters[:1],
             args={},
             connection_type=Connection,
             edge_type=Edge,
             pageinfo_type=PageInfo,
         )
     assert c == Connection(
         edges=[Edge(node="A", cursor="YXJyYXljb25uZWN0aW9uOjA=")],
         pageInfo=PageInfo(
             startCursor="YXJyYXljb25uZWN0aW9uOjA=",
             endCursor="YXJyYXljb25uZWN0aW9uOjA=",
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )