2019-08-18 00:07:53 +03:00
|
|
|
from pytest import mark
|
2018-08-31 20:41:20 +03:00
|
|
|
|
|
|
|
from graphql_relay.utils import base64
|
|
|
|
|
|
|
|
from graphene.types import ObjectType, Schema, String
|
|
|
|
from graphene.relay.connection import Connection, ConnectionField, PageInfo
|
|
|
|
from graphene.relay.node import Node
|
|
|
|
|
|
|
|
letter_chars = ["A", "B", "C", "D", "E"]
|
|
|
|
|
|
|
|
|
|
|
|
class Letter(ObjectType):
|
|
|
|
class Meta:
|
|
|
|
interfaces = (Node,)
|
|
|
|
|
|
|
|
letter = String()
|
|
|
|
|
|
|
|
|
|
|
|
class LetterConnection(Connection):
|
|
|
|
class Meta:
|
|
|
|
node = Letter
|
|
|
|
|
|
|
|
|
|
|
|
class Query(ObjectType):
|
|
|
|
letters = ConnectionField(LetterConnection)
|
|
|
|
connection_letters = ConnectionField(LetterConnection)
|
2019-08-18 00:07:53 +03:00
|
|
|
async_letters = ConnectionField(LetterConnection)
|
2018-08-31 20:41:20 +03:00
|
|
|
|
|
|
|
node = Node.Field()
|
|
|
|
|
|
|
|
def resolve_letters(self, info, **args):
|
|
|
|
return list(letters.values())
|
|
|
|
|
2019-08-18 00:07:53 +03:00
|
|
|
async def resolve_async_letters(self, info, **args):
|
2018-08-31 20:41:20 +03:00
|
|
|
return list(letters.values())
|
|
|
|
|
|
|
|
def resolve_connection_letters(self, info, **args):
|
|
|
|
return LetterConnection(
|
|
|
|
page_info=PageInfo(has_next_page=True, has_previous_page=False),
|
|
|
|
edges=[
|
|
|
|
LetterConnection.Edge(node=Letter(id=0, letter="A"), cursor="a-cursor")
|
|
|
|
],
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
schema = Schema(Query)
|
|
|
|
|
2019-08-18 00:07:53 +03:00
|
|
|
letters = {letter: Letter(id=i, letter=letter) for i, letter in enumerate(letter_chars)}
|
2018-08-31 20:41:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
def edges(selected_letters):
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
"node": {"id": base64("Letter:%s" % l.id), "letter": l.letter},
|
|
|
|
"cursor": base64("arrayconnection:%s" % l.id),
|
|
|
|
}
|
|
|
|
for l in [letters[i] for i in selected_letters]
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
def cursor_for(ltr):
|
|
|
|
letter = letters[ltr]
|
|
|
|
return base64("arrayconnection:%s" % letter.id)
|
|
|
|
|
|
|
|
|
|
|
|
def execute(args=""):
|
|
|
|
if args:
|
|
|
|
args = "(" + args + ")"
|
|
|
|
|
|
|
|
return schema.execute(
|
|
|
|
"""
|
|
|
|
{
|
|
|
|
letters%s {
|
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
letter
|
|
|
|
}
|
|
|
|
cursor
|
|
|
|
}
|
|
|
|
pageInfo {
|
|
|
|
hasPreviousPage
|
|
|
|
hasNextPage
|
|
|
|
startCursor
|
|
|
|
endCursor
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
"""
|
|
|
|
% args
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2019-08-18 00:07:53 +03:00
|
|
|
@mark.asyncio
|
|
|
|
async def test_connection_async():
|
|
|
|
result = await schema.execute_async(
|
2018-08-31 20:41:20 +03:00
|
|
|
"""
|
|
|
|
{
|
2019-08-18 00:07:53 +03:00
|
|
|
asyncLetters(first:1) {
|
2018-08-31 20:41:20 +03:00
|
|
|
edges {
|
|
|
|
node {
|
|
|
|
id
|
|
|
|
letter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pageInfo {
|
|
|
|
hasPreviousPage
|
|
|
|
hasNextPage
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-08-18 00:07:53 +03:00
|
|
|
"""
|
2018-08-31 20:41:20 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
assert not result.errors
|
|
|
|
assert result.data == {
|
2019-08-18 00:07:53 +03:00
|
|
|
"asyncLetters": {
|
2018-08-31 20:41:20 +03:00
|
|
|
"edges": [{"node": {"id": "TGV0dGVyOjA=", "letter": "A"}}],
|
|
|
|
"pageInfo": {"hasPreviousPage": False, "hasNextPage": True},
|
|
|
|
}
|
|
|
|
}
|