From 7da57ec6eebf02326f40158584bce8d3b3659346 Mon Sep 17 00:00:00 2001 From: Charles OuGuo Date: Wed, 28 Aug 2024 23:57:56 -0400 Subject: [PATCH] In Ark GraphQL API, expose field returning all the cards (#447) --- ark_nova_stats/api/gql/schema.py | 2 ++ ark_nova_stats/api/gql/types/game_log.py | 17 ++++++++++++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/ark_nova_stats/api/gql/schema.py b/ark_nova_stats/api/gql/schema.py index 296448d9..79dd535d 100644 --- a/ark_nova_stats/api/gql/schema.py +++ b/ark_nova_stats/api/gql/schema.py @@ -3,6 +3,7 @@ from ark_nova_stats.api.gql.types.game_log import ( fetch_card_field, + fetch_cards_field, fetch_user_field, game_log_field, game_logs_field, @@ -27,6 +28,7 @@ def Schema(app: flask.Flask): "stats": stats_field(GameLog, User), "user": fetch_user_field(User), "card": fetch_card_field(Card), + "cards": fetch_cards_field(Card), }, ), mutation=GraphQLObjectType( diff --git a/ark_nova_stats/api/gql/types/game_log.py b/ark_nova_stats/api/gql/types/game_log.py index 0c55140d..e61c0d96 100644 --- a/ark_nova_stats/api/gql/types/game_log.py +++ b/ark_nova_stats/api/gql/types/game_log.py @@ -10,7 +10,7 @@ GraphQLObjectType, GraphQLString, ) -from sqlalchemy import desc +from sqlalchemy import asc, desc from ark_nova_stats.bga_log_parser.game_log import GameLog as ParsedGameLog from ark_nova_stats.config import app, db @@ -532,3 +532,18 @@ def fetch_card_field( args=fetch_card_filters, resolve=lambda root, info, **args: fetch_card(card_model, args), ) + + +def fetch_cards(card_model: Type[CardModel]) -> list[CardModel]: + return card_model.query.order_by(asc(card_model.name)).all() + + +def fetch_cards_field( + card_model: Type[CardModel], +) -> GraphQLField: + return GraphQLField( + GraphQLNonNull(GraphQLList(card_type)), + description="List cards.", + args={}, + resolve=lambda root, info, **args: fetch_cards(card_model), + )