add multi games formation query

This commit is contained in:
Laurent Le Houerou 2024-03-27 08:39:53 +04:00
parent 026bd11ebc
commit 11a67f038c
2 changed files with 78 additions and 0 deletions

View File

@ -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",

View File

@ -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
}