Ejemplo n.º 1
0
        def returns_all_elements_if_cursors_are_on_the_outside():
            all_edges = Connection(
                edges=[edge_a, edge_b, edge_c, edge_d, edge_e],
                pageInfo=PageInfo(
                    startCursor=cursor_a,
                    endCursor=cursor_e,
                    hasPreviousPage=False,
                    hasNextPage=False,
                ),
            )

            assert (connection_from_array(
                array_abcde, dict(before=offset_to_cursor(6))) == all_edges)
            assert (connection_from_array(
                array_abcde, dict(before=offset_to_cursor(-1))) == all_edges)
            assert (connection_from_array(
                array_abcde, dict(after=offset_to_cursor(6))) == all_edges)
            assert (connection_from_array(
                array_abcde, dict(after=offset_to_cursor(-1))) == all_edges)
Ejemplo n.º 2
0
 def does_not_require_args():
     c = connection_from_array(array_abcde)
     assert c == Connection(
         edges=[edge_a, edge_b, edge_c, edge_d, edge_e],
         pageInfo=PageInfo(
             startCursor=cursor_a,
             endCursor=cursor_e,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 3
0
 def returns_all_elements_without_filters():
     c = connection_from_array(array_abcde, {})
     assert c == Connection(
         edges=[edge_a, edge_b, edge_c, edge_d, edge_e],
         pageInfo=PageInfo(
             startCursor=cursor_a,
             endCursor=cursor_e,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 4
0
 def respects_a_smaller_last():
     c = connection_from_array(array_abcde, dict(last=2))
     assert c == Connection(
         edges=[edge_d, edge_e],
         pageInfo=PageInfo(
             startCursor=cursor_d,
             endCursor=cursor_e,
             hasPreviousPage=True,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 5
0
 def respects_an_overly_large_last():
     c = connection_from_array(array_abcde, dict(last=10))
     assert c == Connection(
         edges=[edge_a, edge_b, edge_c, edge_d, edge_e],
         pageInfo=PageInfo(
             startCursor=cursor_a,
             endCursor=cursor_e,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 6
0
 def respects_last_and_before_with_long_last():
     c = connection_from_array(array_abcde,
                               dict(last=10, before=cursor_d))
     assert c == Connection(
         edges=[edge_a, edge_b, edge_c],
         pageInfo=PageInfo(
             startCursor=cursor_a,
             endCursor=cursor_c,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 7
0
 def returns_all_elements_if_cursors_are_invalid():
     c = connection_from_array(array_abcde,
                               dict(before="invalid", after="invalid"))
     assert c == Connection(
         edges=[edge_a, edge_b, edge_c, edge_d, edge_e],
         pageInfo=PageInfo(
             startCursor=cursor_a,
             endCursor=cursor_e,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 8
0
 def respects_first_and_after_with_long_first():
     c = connection_from_array(array_abcde,
                               dict(first=10, after=cursor_b))
     assert c == Connection(
         edges=[edge_c, edge_d, edge_e],
         pageInfo=PageInfo(
             startCursor=cursor_c,
             endCursor=cursor_e,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 9
0
 def respects_first_and_after_and_before_too_few():
     c = connection_from_array(
         array_abcde,
         dict(first=2, after=cursor_a, before=cursor_e),
     )
     assert c == Connection(
         edges=[edge_b, edge_c],
         pageInfo=PageInfo(
             startCursor=cursor_b,
             endCursor=cursor_c,
             hasPreviousPage=False,
             hasNextPage=True,
         ),
     )
 def respects_a_smaller_last():
     c = connection_from_array(letters, dict(last=2))
     assert c == Connection(
         edges=[
             Edge(node="D", cursor="YXJyYXljb25uZWN0aW9uOjM="),
             Edge(node="E", cursor="YXJyYXljb25uZWN0aW9uOjQ="),
         ],
         pageInfo=PageInfo(
             startCursor="YXJyYXljb25uZWN0aW9uOjM=",
             endCursor="YXJyYXljb25uZWN0aW9uOjQ=",
             hasPreviousPage=True,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 11
0
 def returns_no_elements_if_cursors_cross():
     c = connection_from_array(
         array_abcde,
         dict(before=cursor_c, after=cursor_e),
     )
     assert c == Connection(
         edges=[],
         pageInfo=PageInfo(
             startCursor=None,
             endCursor=None,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 12
0
 def respects_last_and_after_and_before_exactly_right():
     c = connection_from_array(
         array_abcde,
         dict(last=3, after=cursor_a, before=cursor_e),
     )
     assert c == Connection(
         edges=[edge_b, edge_c, edge_d],
         pageInfo=PageInfo(
             startCursor=cursor_b,
             endCursor=cursor_d,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 13
0
 def uses_default_connection_types():
     connection = connection_from_array(array_abcde[:1])
     assert isinstance(connection, Connection)
     edge = connection.edges[0]
     assert isinstance(edge, Edge)
     assert len(connection.edges) == 1
     assert edge == edge_a
     page_info = connection.pageInfo
     assert isinstance(page_info, PageInfo)
     assert page_info == PageInfo(
         startCursor=cursor_a,
         endCursor=cursor_a,
         hasPreviousPage=False,
         hasNextPage=False,
     )
 def respects_last_and_before():
     c = connection_from_array(
         letters, dict(last=2, before="YXJyYXljb25uZWN0aW9uOjM="))
     assert c == Connection(
         edges=[
             Edge(node="B", cursor="YXJyYXljb25uZWN0aW9uOjE="),
             Edge(node="C", cursor="YXJyYXljb25uZWN0aW9uOjI="),
         ],
         pageInfo=PageInfo(
             startCursor="YXJyYXljb25uZWN0aW9uOjE=",
             endCursor="YXJyYXljb25uZWN0aW9uOjI=",
             hasPreviousPage=True,
             hasNextPage=False,
         ),
     )
 def uses_default_connection_types():
     connection = connection_from_array(letters[:1])
     assert isinstance(connection, Connection)
     edge = connection.edges[0]
     assert isinstance(edge, Edge)
     assert len(connection.edges) == 1
     assert edge == Edge(node="A", cursor="YXJyYXljb25uZWN0aW9uOjA=")
     page_info = connection.pageInfo
     assert isinstance(page_info, PageInfo)
     assert page_info == PageInfo(
         startCursor="YXJyYXljb25uZWN0aW9uOjA=",
         endCursor="YXJyYXljb25uZWN0aW9uOjA=",
         hasPreviousPage=False,
         hasNextPage=False,
     )
 def returns_no_elements_if_cursors_cross():
     c = connection_from_array(
         letters,
         dict(before="YXJyYXljb25uZWN0aW9uOjI=",
              after="YXJyYXljb25uZWN0aW9uOjQ="),
     )
     assert c == Connection(
         edges=[],
         pageInfo=PageInfo(
             startCursor=None,
             endCursor=None,
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
 def respects_first_and_after_with_long_first():
     c = connection_from_array(
         letters, dict(first=10, after="YXJyYXljb25uZWN0aW9uOjE="))
     assert c == Connection(
         edges=[
             Edge(node="C", cursor="YXJyYXljb25uZWN0aW9uOjI="),
             Edge(node="D", cursor="YXJyYXljb25uZWN0aW9uOjM="),
             Edge(node="E", cursor="YXJyYXljb25uZWN0aW9uOjQ="),
         ],
         pageInfo=PageInfo(
             startCursor="YXJyYXljb25uZWN0aW9uOjI=",
             endCursor="YXJyYXljb25uZWN0aW9uOjQ=",
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 18
0
    def resolve_connection(cls, connection_type, args, resolved):
        if isinstance(resolved, connection_type):
            return resolved

        assert isinstance(resolved, Iterable), (
            f"Resolved value from the connection field has to be an iterable or instance of {connection_type}. "
            f'Received "{resolved}"')
        connection = connection_from_array(
            resolved,
            args,
            connection_type=partial(connection_adapter, connection_type),
            edge_type=connection_type.Edge,
            page_info_type=page_info_adapter,
        )
        connection.iterable = resolved
        return connection
 def returns_all_elements_without_filters():
     c = connection_from_array(letters, {})
     assert c == Connection(
         edges=[
             Edge(node="A", cursor="YXJyYXljb25uZWN0aW9uOjA="),
             Edge(node="B", cursor="YXJyYXljb25uZWN0aW9uOjE="),
             Edge(node="C", cursor="YXJyYXljb25uZWN0aW9uOjI="),
             Edge(node="D", cursor="YXJyYXljb25uZWN0aW9uOjM="),
             Edge(node="E", cursor="YXJyYXljb25uZWN0aW9uOjQ="),
         ],
         pageInfo=PageInfo(
             startCursor="YXJyYXljb25uZWN0aW9uOjA=",
             endCursor="YXJyYXljb25uZWN0aW9uOjQ=",
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
 def respects_an_overly_large_last():
     c = connection_from_array(letters, dict(last=10))
     assert c == Connection(
         edges=[
             Edge(node="A", cursor="YXJyYXljb25uZWN0aW9uOjA="),
             Edge(node="B", cursor="YXJyYXljb25uZWN0aW9uOjE="),
             Edge(node="C", cursor="YXJyYXljb25uZWN0aW9uOjI="),
             Edge(node="D", cursor="YXJyYXljb25uZWN0aW9uOjM="),
             Edge(node="E", cursor="YXJyYXljb25uZWN0aW9uOjQ="),
         ],
         pageInfo=PageInfo(
             startCursor="YXJyYXljb25uZWN0aW9uOjA=",
             endCursor="YXJyYXljb25uZWN0aW9uOjQ=",
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
 def does_not_require_args():
     c = connection_from_array(letters)
     assert c == Connection(
         edges=[
             Edge(node="A", cursor="YXJyYXljb25uZWN0aW9uOjA="),
             Edge(node="B", cursor="YXJyYXljb25uZWN0aW9uOjE="),
             Edge(node="C", cursor="YXJyYXljb25uZWN0aW9uOjI="),
             Edge(node="D", cursor="YXJyYXljb25uZWN0aW9uOjM="),
             Edge(node="E", cursor="YXJyYXljb25uZWN0aW9uOjQ="),
         ],
         pageInfo=PageInfo(
             startCursor="YXJyYXljb25uZWN0aW9uOjA=",
             endCursor="YXJyYXljb25uZWN0aW9uOjQ=",
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
 def returns_all_elements_if_cursors_are_invalid():
     c = connection_from_array(letters,
                               dict(before="invalid", after="invalid"))
     assert c == Connection(
         edges=[
             Edge(node="A", cursor="YXJyYXljb25uZWN0aW9uOjA="),
             Edge(node="B", cursor="YXJyYXljb25uZWN0aW9uOjE="),
             Edge(node="C", cursor="YXJyYXljb25uZWN0aW9uOjI="),
             Edge(node="D", cursor="YXJyYXljb25uZWN0aW9uOjM="),
             Edge(node="E", cursor="YXJyYXljb25uZWN0aW9uOjQ="),
         ],
         pageInfo=PageInfo(
             startCursor="YXJyYXljb25uZWN0aW9uOjA=",
             endCursor="YXJyYXljb25uZWN0aW9uOjQ=",
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
        def accepts_custom_connection_type():
            class CustomConnection:
                # noinspection PyPep8Naming
                def __init__(self, edges, pageInfo):
                    self.edges = edges
                    self.page_info = pageInfo

            connection = connection_from_array(
                letters[:1], connection_type=CustomConnection)
            assert isinstance(connection, CustomConnection)
            edge = connection.edges[0]
            assert isinstance(edge, Edge)
            assert len(connection.edges) == 1
            assert edge == Edge(node="A", cursor="YXJyYXljb25uZWN0aW9uOjA=")
            page_info = connection.page_info
            assert isinstance(page_info, PageInfo)
            assert page_info == PageInfo(
                startCursor="YXJyYXljb25uZWN0aW9uOjA=",
                endCursor="YXJyYXljb25uZWN0aW9uOjA=",
                hasPreviousPage=False,
                hasNextPage=False,
            )
Ejemplo n.º 24
0
        def accepts_custom_connection_type():
            class CustomConnection:
                # noinspection PyPep8Naming
                def __init__(self, edges, pageInfo):
                    self.edges = edges
                    self.page_info = pageInfo

            connection = connection_from_array(
                array_abcde[:1], connection_type=CustomConnection)
            assert isinstance(connection, CustomConnection)
            edge = connection.edges[0]
            assert isinstance(edge, Edge)
            assert len(connection.edges) == 1
            assert edge == edge_a
            page_info = connection.page_info
            assert isinstance(page_info, PageInfo)
            assert page_info == PageInfo(
                startCursor=cursor_a,
                endCursor=cursor_a,
                hasPreviousPage=False,
                hasNextPage=False,
            )
 def respects_first_and_after_and_before_exactly_right():
     c = connection_from_array(
         letters,
         dict(
             first=3,
             after="YXJyYXljb25uZWN0aW9uOjA=",
             before="YXJyYXljb25uZWN0aW9uOjQ=",
         ),
     )
     assert c == Connection(
         edges=[
             Edge(node="B", cursor="YXJyYXljb25uZWN0aW9uOjE="),
             Edge(node="C", cursor="YXJyYXljb25uZWN0aW9uOjI="),
             Edge(node="D", cursor="YXJyYXljb25uZWN0aW9uOjM="),
         ],
         pageInfo=PageInfo(
             startCursor="YXJyYXljb25uZWN0aW9uOjE=",
             endCursor="YXJyYXljb25uZWN0aW9uOjM=",
             hasPreviousPage=False,
             hasNextPage=False,
         ),
     )
Ejemplo n.º 26
0
        def accepts_custom_edge_type():
            class CustomEdge:
                def __init__(self, node, cursor):
                    self.node = node
                    self.cursor = cursor

            connection = connection_from_array(array_abcde[:1],
                                               edge_type=CustomEdge)
            assert isinstance(connection, Connection)
            assert isinstance(connection.edges, list)
            assert len(connection.edges) == 1
            edge = connection.edges[0]
            assert isinstance(edge, CustomEdge)
            assert edge.node == "A"
            assert edge.cursor == cursor_a
            page_info = connection.pageInfo
            assert isinstance(page_info, PageInfo)
            assert page_info == PageInfo(
                startCursor=cursor_a,
                endCursor=cursor_a,
                hasPreviousPage=False,
                hasNextPage=False,
            )
Ejemplo n.º 27
0
        def accepts_custom_page_info_type():
            class CustomPageInfo:
                # noinspection PyPep8Naming
                def __init__(self, startCursor, endCursor, hasPreviousPage,
                             hasNextPage):
                    self.startCursor = startCursor
                    self.endCursor = endCursor
                    self.hasPreviousPage = hasPreviousPage
                    self.hasNextPage = hasNextPage

            connection = connection_from_array(array_abcde[:1],
                                               page_info_type=CustomPageInfo)
            assert isinstance(connection, Connection)
            assert isinstance(connection.edges, list)
            assert len(connection.edges) == 1
            edge = connection.edges[0]
            assert isinstance(edge, Edge)
            assert edge == edge_a
            page_info = connection.pageInfo
            assert isinstance(page_info, CustomPageInfo)
            assert page_info.startCursor == cursor_a
            assert page_info.endCursor == cursor_a
            assert page_info.hasPreviousPage is False
            assert page_info.hasNextPage is False
        def accepts_custom_page_info_type():
            class CustomPageInfo:
                # noinspection PyPep8Naming
                def __init__(self, startCursor, endCursor, hasPreviousPage,
                             hasNextPage):
                    self.startCursor = startCursor
                    self.endCursor = endCursor
                    self.hasPreviousPage = hasPreviousPage
                    self.hasNextPage = hasNextPage

            connection = connection_from_array(letters[:1],
                                               page_info_type=CustomPageInfo)
            assert isinstance(connection, Connection)
            assert isinstance(connection.edges, list)
            assert len(connection.edges) == 1
            edge = connection.edges[0]
            assert isinstance(edge, Edge)
            assert edge == Edge(node="A", cursor="YXJyYXljb25uZWN0aW9uOjA=")
            page_info = connection.pageInfo
            assert isinstance(page_info, CustomPageInfo)
            assert page_info.startCursor == "YXJyYXljb25uZWN0aW9uOjA="
            assert page_info.endCursor == "YXJyYXljb25uZWN0aW9uOjA="
            assert page_info.hasPreviousPage is False
            assert page_info.hasNextPage is False
Ejemplo n.º 29
0
 def throws_an_error_if_last_smaller_than_zero():
     with raises(ValueError) as exc_info:
         connection_from_array(array_abcde, dict(last=-1))
     assert str(exc_info.value) == (
         "Argument 'last' must be a non-negative integer.")
Ejemplo n.º 30
0
    User(name="Joe", friends=[0, 1, 2, 4]),
    User(name="Tim", friends=[0, 1, 2, 3]),
]

friend_connection: GraphQLObjectType
user_connection: GraphQLObjectType

user_type = GraphQLObjectType(
    "User",
    fields=lambda: {
        "name": GraphQLField(GraphQLString),
        "friends": GraphQLField(
            friend_connection,
            args=connection_args,
            resolve=lambda user, _info, **args: connection_from_array(
                user.friends, args
            ),
        ),
        "friendsForward": GraphQLField(
            user_connection,
            args=forward_connection_args,
            resolve=lambda user, _info, **args: connection_from_array(
                user.friends, args
            ),
        ),
        "friendsBackward": GraphQLField(
            user_connection,
            args=backward_connection_args,
            resolve=lambda user, _info, **args: connection_from_array(
                user.friends, args
            ),