Compare commits
No commits in common. "0d2135178e4d3756a16137677ca50bb69ee0f0af" and "c3a9b25af4a8ecf5f274da93c5c5e6b0fe205b3e" have entirely different histories.
0d2135178e
...
c3a9b25af4
4
.gitignore
vendored
4
.gitignore
vendored
@ -1,5 +1 @@
|
||||
.vscode/
|
||||
|
||||
go.work
|
||||
go.work.sum
|
||||
|
||||
|
@ -13,8 +13,8 @@ import (
|
||||
|
||||
func main() {
|
||||
|
||||
token := os.Getenv("JWTTOKEN")
|
||||
audience := os.Getenv("JWTAUDIENCE")
|
||||
token := os.Getenv("SORARE_TOKEN")
|
||||
audience := os.Getenv("SORARE_AUDIENCE")
|
||||
if token == "" {
|
||||
log.Fatal("No token provided")
|
||||
}
|
||||
|
@ -17,7 +17,6 @@ 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]
|
||||
@ -78,9 +77,6 @@ func NewFootball(c *graphql.Client) *Football {
|
||||
"game",
|
||||
[]string{"football"},
|
||||
),
|
||||
GamesFormation: NewGamesFormationQuery(
|
||||
c,
|
||||
),
|
||||
MyLiveGames: graphql.NewQuery[[]Game, graphql.EmptyParams](
|
||||
c,
|
||||
"myLiveGames",
|
||||
|
@ -1,74 +0,0 @@
|
||||
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
|
||||
}
|
3
go.mod
3
go.mod
@ -15,8 +15,6 @@ require (
|
||||
golang.org/x/time v0.5.0
|
||||
)
|
||||
|
||||
require golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||
|
||||
require (
|
||||
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
|
||||
github.com/klauspost/compress v1.16.7 // indirect
|
||||
@ -24,5 +22,6 @@ require (
|
||||
github.com/mattn/go-isatty v0.0.19 // indirect
|
||||
golang.org/x/net v0.21.0 // indirect
|
||||
golang.org/x/sys v0.18.0 // indirect
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
|
||||
nhooyr.io/websocket v1.8.7 // indirect
|
||||
)
|
||||
|
@ -80,21 +80,6 @@ func (c *Client) Query(
|
||||
return c.gql.Query(ctx, q, variables, options...)
|
||||
}
|
||||
|
||||
func (c *Client) QueryRaw(
|
||||
ctx context.Context,
|
||||
q interface{},
|
||||
variables interface{},
|
||||
options ...graphql.Option,
|
||||
) ([]byte, error) {
|
||||
err := c.rl.Wait(ctx)
|
||||
if err != nil {
|
||||
return nil, errors.Wrap(err, "waiting for rate limit")
|
||||
}
|
||||
c.lock.Lock()
|
||||
defer c.lock.Unlock()
|
||||
return c.gql.QueryRaw(ctx, q, variables, options...)
|
||||
}
|
||||
|
||||
func (c *Client) Mutate(
|
||||
ctx context.Context,
|
||||
q interface{},
|
||||
|
@ -41,7 +41,7 @@ func (m *Mutation[Payload, Params]) WithQueryParam(
|
||||
|
||||
func (m *Mutation[Payload, Params]) Execute(ctx context.Context, params Params) (Payload, error) {
|
||||
|
||||
paramsMap := convertParamsToMap(params)
|
||||
paramsMap := convertParamsToMap[Params](params)
|
||||
for k, v := range m.additionalPayloadParams {
|
||||
paramsMap[k] = v
|
||||
}
|
||||
@ -53,7 +53,7 @@ func (m *Mutation[Payload, Params]) Execute(ctx context.Context, params Params)
|
||||
}
|
||||
gqltype = fmt.Sprintf("%s(%s)", gqltype, strings.Join(keys, ","))
|
||||
}
|
||||
q := NewPayload[Payload](gqltype)
|
||||
q := NewPayload[Payload, Params](gqltype)
|
||||
for k, v := range m.additionalQueryParams {
|
||||
paramsMap[k] = v
|
||||
}
|
||||
|
@ -1,10 +1,10 @@
|
||||
package graphql
|
||||
|
||||
type Payload[T any] struct {
|
||||
OutermostLayer ContainerLayer[T]
|
||||
type Payload[T any, P any] struct {
|
||||
OutermostLayer ContainerLayer[T, P]
|
||||
}
|
||||
|
||||
func (q *Payload[T]) GetValue() T {
|
||||
func (q *Payload[T, P]) GetValue() T {
|
||||
if q.OutermostLayer == nil {
|
||||
var res T
|
||||
return res
|
||||
@ -16,74 +16,74 @@ func (q *Payload[T]) GetValue() T {
|
||||
return layer.GetValue()
|
||||
}
|
||||
|
||||
func NewPayload[T any](gqlType string, containerLayers ...string) *Payload[T] {
|
||||
func NewPayload[T any, P any](gqlType string, containerLayers ...string) *Payload[T, P] {
|
||||
if len(containerLayers) == 0 {
|
||||
return &Payload[T]{
|
||||
OutermostLayer: NewWrapper[T](gqlType),
|
||||
return &Payload[T, P]{
|
||||
OutermostLayer: NewWrapper[T, P](gqlType),
|
||||
}
|
||||
}
|
||||
|
||||
var buildLayer func(index int) ContainerLayer[T]
|
||||
buildLayer = func(index int) ContainerLayer[T] {
|
||||
var buildLayer func(index int) ContainerLayer[T, P]
|
||||
buildLayer = func(index int) ContainerLayer[T, P] {
|
||||
if index == len(containerLayers) {
|
||||
return NewWrapper[T](gqlType)
|
||||
return NewWrapper[T, P](gqlType)
|
||||
}
|
||||
return &NestedLayer[T]{
|
||||
return &NestedLayer[T, P]{
|
||||
gqlType: containerLayers[index],
|
||||
InnerLayer: buildLayer(index + 1),
|
||||
}
|
||||
}
|
||||
|
||||
return &Payload[T]{OutermostLayer: buildLayer(0)}
|
||||
return &Payload[T, P]{OutermostLayer: buildLayer(0)}
|
||||
}
|
||||
|
||||
type ContainerLayer[T any] interface {
|
||||
GetInnerLayer() ContainerLayer[T]
|
||||
type ContainerLayer[T any, P any] interface {
|
||||
GetInnerLayer() ContainerLayer[T, P]
|
||||
GetValue() T
|
||||
GetGraphQLType() string
|
||||
}
|
||||
|
||||
type NestedLayer[T any] struct {
|
||||
type NestedLayer[T any, P any] struct {
|
||||
gqlType string `graphql:"-"`
|
||||
InnerLayer ContainerLayer[T]
|
||||
InnerLayer ContainerLayer[T, P]
|
||||
}
|
||||
|
||||
func (nl *NestedLayer[T]) GetInnerLayer() ContainerLayer[T] {
|
||||
func (nl *NestedLayer[T, P]) GetInnerLayer() ContainerLayer[T, P] {
|
||||
return nl.InnerLayer
|
||||
}
|
||||
|
||||
func (nl *NestedLayer[T]) GetValue() T {
|
||||
func (nl *NestedLayer[T, P]) GetValue() T {
|
||||
var res T
|
||||
return res
|
||||
}
|
||||
|
||||
func (nl *NestedLayer[T]) GetGraphQLType() string {
|
||||
func (nl *NestedLayer[T, P]) GetGraphQLType() string {
|
||||
return nl.gqlType
|
||||
}
|
||||
|
||||
type Wrapper[T any] struct {
|
||||
type Wrapper[T any, P any] struct {
|
||||
gqlType string `graphql:"-"`
|
||||
Value T
|
||||
}
|
||||
|
||||
func (w Wrapper[T]) GetInnerLayer() ContainerLayer[T] {
|
||||
func (w Wrapper[T, P]) GetInnerLayer() ContainerLayer[T, P] {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (w Wrapper[T]) GetValue() T {
|
||||
func (w Wrapper[T, P]) GetValue() T {
|
||||
return w.Value
|
||||
}
|
||||
|
||||
func (w Wrapper[T]) GetGraphQLWrapped() T {
|
||||
func (w Wrapper[T, P]) GetGraphQLWrapped() T {
|
||||
return w.Value
|
||||
}
|
||||
|
||||
func (w Wrapper[T]) GetGraphQLType() string {
|
||||
func (w Wrapper[T, P]) GetGraphQLType() string {
|
||||
return w.gqlType
|
||||
}
|
||||
|
||||
func NewWrapper[T any](gqlType string) *Wrapper[T] {
|
||||
return &Wrapper[T]{
|
||||
func NewWrapper[T any, P any](gqlType string) *Wrapper[T, P] {
|
||||
return &Wrapper[T, P]{
|
||||
gqlType: gqlType,
|
||||
}
|
||||
}
|
||||
|
@ -50,7 +50,7 @@ func (r *Query[ReturnType, Params]) WithQueryParam(
|
||||
|
||||
func (r *Query[ReturnType, Params]) Get(ctx context.Context, params Params) (ReturnType, error) {
|
||||
|
||||
paramsMap := convertParamsToMap(params)
|
||||
paramsMap := convertParamsToMap[Params](params)
|
||||
for k, v := range r.additionalPayloadParams {
|
||||
paramsMap[k] = v
|
||||
}
|
||||
@ -65,7 +65,7 @@ func (r *Query[ReturnType, Params]) Get(ctx context.Context, params Params) (Ret
|
||||
for k, v := range r.additionalQueryParams {
|
||||
paramsMap[k] = v
|
||||
}
|
||||
q := NewPayload[ReturnType](gqltype, r.containerLayers...)
|
||||
q := NewPayload[ReturnType, Params](gqltype, r.containerLayers...)
|
||||
err := r.c.Query(ctx, q, paramsMap)
|
||||
if err != nil {
|
||||
var res ReturnType
|
||||
|
Loading…
Reference in New Issue
Block a user