-
Notifications
You must be signed in to change notification settings - Fork 179
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: RealyQueryType now inherits from RelayObjectType to give query …
…the option to use connections, add unit tests
- Loading branch information
Showing
7 changed files
with
627 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
from base64 import b64decode | ||
|
||
import pytest | ||
from ariadne.contrib.relay import ( | ||
ConnectionArguments, | ||
GlobalIDTuple, | ||
RelayConnection, | ||
RelayNodeInterfaceType, | ||
RelayQueryType, | ||
) | ||
|
||
|
||
@pytest.fixture | ||
def relay_type_defs(): | ||
return """\ | ||
interface Node { | ||
bid: ID! | ||
} | ||
type Faction implements Node { | ||
bid: ID! | ||
name: String | ||
ships(first: Int!, after: ID): ShipConnection | ||
} | ||
type Ship implements Node { | ||
bid: ID! | ||
name: String | ||
} | ||
type ShipConnection { | ||
edges: [ShipEdge] | ||
pageInfo: PageInfo! | ||
ships: [Ship] | ||
totalCount: Int | ||
} | ||
type ShipEdge { | ||
cursor: String! | ||
node: Ship | ||
} | ||
type PageInfo { | ||
hasNextPage: Boolean! | ||
hasPreviousPage: Boolean! | ||
startCursor: String | ||
endCursor: String | ||
} | ||
type Query { | ||
rebels: Faction | ||
empire: Faction | ||
node(bid: ID!): Node | ||
} | ||
""" | ||
|
||
|
||
@pytest.fixture | ||
def global_id_decoder(): | ||
return lambda kwargs: GlobalIDTuple(*b64decode(kwargs["bid"]).decode().split(":")) | ||
|
||
|
||
@pytest.fixture | ||
def relay_node_interface(global_id_decoder): | ||
return RelayNodeInterfaceType(global_id_decoder=global_id_decoder) | ||
|
||
|
||
@pytest.fixture | ||
def relay_query(factions, relay_node_interface): | ||
query = RelayQueryType( | ||
node=relay_node_interface, | ||
) | ||
query.set_field("rebels", lambda *_: factions[0]) | ||
query.set_field("empire", lambda *_: factions[1]) | ||
query.node.set_field("bid", lambda obj, *_: obj["id"]) | ||
return query | ||
|
||
|
||
@pytest.fixture | ||
def ships(): | ||
return [ | ||
{ | ||
"id": "U2hpcDox", | ||
"name": "X-Wing", | ||
"factionId": "RmFjdGlvbjox", | ||
}, | ||
{ | ||
"id": "U2hpcDoy", | ||
"name": "Y-Wing", | ||
"factionId": "RmFjdGlvbjox", | ||
}, | ||
{ | ||
"id": "U2hpcDoz", | ||
"name": "A-Wing", | ||
"factionId": "RmFjdGlvbjox", | ||
}, | ||
{ | ||
"id": "U2hpcDo0", | ||
"name": "Millennium Falcon", | ||
"factionId": "RmFjdGlvbjox", | ||
}, | ||
{ | ||
"id": "U2hpcDo1", | ||
"name": "Home One", | ||
"factionId": "RmFjdGlvbjox", | ||
}, | ||
{ | ||
"id": "U2hpcDo2", | ||
"name": "TIE Fighter", | ||
"factionId": "RmFjdGlvbjoy", | ||
}, | ||
{ | ||
"id": "U2hpcDo3", | ||
"name": "TIE Bomber", | ||
"factionId": "RmFjdGlvbjoy", | ||
}, | ||
{ | ||
"id": "U2hpcDo4", | ||
"name": "TIE Interceptor", | ||
"factionId": "RmFjdGlvbjoy", | ||
}, | ||
{ | ||
"id": "U2hpcDo5", | ||
"name": "Darth Vader's TIE Advanced", | ||
"factionId": "RmFjdGlvbjoy", | ||
}, | ||
] | ||
|
||
|
||
@pytest.fixture | ||
def factions(): | ||
return [ | ||
{ | ||
"id": "RmFjdGlvbjox", | ||
"name": "Alliance to Restore the Republic", | ||
}, | ||
{"id": "RmFjdGlvbjoy", "name": "Galactic Empire"}, | ||
] | ||
|
||
|
||
@pytest.fixture | ||
def relay_query_with_node_resolvers(relay_query, ships, factions): | ||
relay_query.node.node_resolver("Faction")( | ||
lambda *_, bid: [ | ||
{"__typename": "Faction", **faction} | ||
for faction in factions | ||
if faction["id"] == bid | ||
][0] | ||
) | ||
relay_query.node.node_resolver("Ship")( | ||
lambda *_, bid: [ | ||
{"__typename": "Ship", **ship} for ship in ships if ship["id"] == bid | ||
][0] | ||
) | ||
return relay_query | ||
|
||
|
||
@pytest.fixture | ||
def ship_slice_resolver(ships): | ||
# pylint: disable=unused-argument | ||
def resolver( | ||
faction_obj, info, connection_arguments: ConnectionArguments, **kwargs | ||
): | ||
faction_ships = [ | ||
ship for ship in ships if ship["factionId"] == faction_obj["id"] | ||
] | ||
total = len(faction_ships) | ||
if connection_arguments.after: | ||
after_index = ( | ||
faction_ships.index( | ||
next( | ||
ship | ||
for ship in faction_ships | ||
if ship["id"] == connection_arguments.after | ||
) | ||
) | ||
+ 1 | ||
) | ||
else: | ||
after_index = 0 | ||
ships_slice = faction_ships[ | ||
after_index : after_index + connection_arguments.first | ||
] | ||
|
||
return RelayConnection( | ||
edges=ships_slice, | ||
total=total, | ||
has_next_page=after_index + connection_arguments.first < total, | ||
has_previous_page=after_index > 0, | ||
) | ||
|
||
return resolver |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
from ariadne.contrib.relay.arguments import ( | ||
BackwardConnectionArguments, | ||
ConnectionArguments, | ||
ForwardConnectionArguments, | ||
) | ||
|
||
|
||
def test_connection_arguments(): | ||
connection_arguments = ConnectionArguments( | ||
first=10, after="cursor", last=5, before="cursor" | ||
) | ||
assert connection_arguments.first == 10 | ||
assert connection_arguments.after == "cursor" | ||
assert connection_arguments.last == 5 | ||
assert connection_arguments.before == "cursor" | ||
|
||
|
||
def test_forward_connection_arguments(): | ||
connection_arguments = ForwardConnectionArguments(first=10, after="cursor") | ||
assert connection_arguments.first == 10 | ||
assert connection_arguments.after == "cursor" | ||
|
||
|
||
def test_backward_connection_arguments(): | ||
connection_arguments = BackwardConnectionArguments(last=5, before="cursor") | ||
assert connection_arguments.last == 5 | ||
assert connection_arguments.before == "cursor" |
Oops, something went wrong.