From 11a67f038c447325f99c42bdc5871e62d8ec20b6 Mon Sep 17 00:00:00 2001 From: Laurent Le Houerou Date: Wed, 27 Mar 2024 08:39:53 +0400 Subject: [PATCH] add multi games formation query --- football/football.go | 4 ++ football/games_formation_query.go | 74 +++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+) create mode 100644 football/games_formation_query.go diff --git a/football/football.go b/football/football.go index fc392ac..4a97a9c 100644 --- a/football/football.go +++ b/football/football.go @@ -17,6 +17,7 @@ type Football struct { ClubsReady *graphql.Query[[]Club, graphql.EmptyParams] Competition *graphql.Query[Competition, graphql.SlugParams] Game *graphql.Query[GameWithFormation, graphql.IdParams] + GamesFormation *GamesFormationQuery MyLiveGames *graphql.Query[[]Game, graphql.EmptyParams] NationalTeam *graphql.Query[NationalTeam, graphql.SlugParams] NationalTeams *graphql.Query[[]NationalTeam, graphql.SlugsParams] @@ -77,6 +78,9 @@ func NewFootball(c *graphql.Client) *Football { "game", []string{"football"}, ), + GamesFormation: NewGamesFormationQuery( + c, + ), MyLiveGames: graphql.NewQuery[[]Game, graphql.EmptyParams]( c, "myLiveGames", diff --git a/football/games_formation_query.go b/football/games_formation_query.go new file mode 100644 index 0000000..fdd8083 --- /dev/null +++ b/football/games_formation_query.go @@ -0,0 +1,74 @@ +package football + +import ( + "context" + "fmt" + + "github.com/pkg/errors" + + "git.lehouerou.net/laurent/sorare/graphql" +) + +type GamesFormationQuery struct { + c *graphql.Client +} + +type GameFormation struct { + Id graphql.Id `graphql:"id"` + AwayTeam struct { + Team struct { + Slug string `graphql:"slug"` + } `graphql:"... on TeamInterface"` + } `graphql:"awayTeam"` + AwayFormation struct { + Bench []struct { + Slug string `graphql:"slug"` + } `graphql:"bench"` + StartingLineup [][]struct { + Slug string `graphql:"slug"` + } `graphql:"startingLineup"` + } `graphql:"awayFormation"` + HomeTeam struct { + Team struct { + Slug string `graphql:"slug"` + } `graphql:"... on TeamInterface"` + } `graphql:"homeTeam"` + + HomeFormation struct { + Bench []struct { + Slug string `graphql:"slug"` + } `graphql:"bench"` + StartingLineup [][]struct { + Slug string `graphql:"slug"` + } `graphql:"startingLineup"` + } `graphql:"homeFormation"` +} + +func NewGamesFormationQuery(c *graphql.Client) *GamesFormationQuery { + return &GamesFormationQuery{ + c: c, + } +} + +func (r *GamesFormationQuery) Get(ctx context.Context, gameIds []string) ([]GameFormation, error) { + var q struct { + Football [][2]interface{} `graphql:"football"` + } + + q.Football = make([][2]interface{}, len(gameIds)) + + for i, id := range gameIds { + var game GameFormation + q.Football[i] = [2]interface{}{fmt.Sprintf("game%d:game(id:\"%s\")", i, id), &game} + } + + err := r.c.Query(ctx, &q, nil) + if err != nil { + return nil, errors.Wrap(err, "querying games") + } + var res []GameFormation + for _, g := range q.Football { + res = append(res, *g[1].(*GameFormation)) + } + return res, nil +}