beerpong-elo/internal/web/gqlSchema.go

246 lines
5.6 KiB
Go

package web
import (
"log"
"git.kapelle.org/niklas/beerpong-elo/internal/model"
"git.kapelle.org/niklas/beerpong-elo/internal/repo"
"github.com/graphql-go/graphql"
)
func createShema(repo repo.Repo) graphql.Schema {
player := graphql.NewObject(graphql.ObjectConfig{
Name: "Player",
Description: "A player. Can also be authors of games",
Fields: graphql.Fields{
"ID": &graphql.Field{
Type: graphql.NewNonNull(graphql.ID),
},
},
})
game := graphql.NewObject(graphql.ObjectConfig{
Name: "Game",
Description: "A game played",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.ID),
},
"added": &graphql.Field{
Type: graphql.NewNonNull(graphql.String),
},
"overtime": &graphql.Field{
Type: graphql.NewNonNull(graphql.Boolean),
},
"score": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
},
"team0player0": &graphql.Field{
Type: graphql.NewNonNull(player),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
game, ok := p.Source.(*model.Game)
if !ok {
return nil, nil
}
player, err := repo.GetPlayer(game.Team0Player0)
if err != nil {
return nil, err
}
return player, nil
},
},
"team0player1": &graphql.Field{
Type: graphql.NewNonNull(player),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
game, ok := p.Source.(*model.Game)
if !ok {
return nil, nil
}
player, err := repo.GetPlayer(game.Team0Player1)
if err != nil {
return nil, err
}
return player, nil
},
},
"team1player0": &graphql.Field{
Type: graphql.NewNonNull(player),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
game, ok := p.Source.(*model.Game)
if !ok {
return nil, nil
}
player, err := repo.GetPlayer(game.Team1Player0)
if err != nil {
return nil, err
}
return player, nil
},
},
"team1player1": &graphql.Field{
Type: graphql.NewNonNull(player),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
game, ok := p.Source.(*model.Game)
if !ok {
return nil, nil
}
player, err := repo.GetPlayer(game.Team1Player1)
if err != nil {
return nil, err
}
return player, nil
},
},
},
})
gameResult := graphql.NewObject(graphql.ObjectConfig{
Name: "GameResult",
Description: "The ELO change for a player in a game",
Fields: graphql.Fields{
"id": &graphql.Field{
Type: graphql.NewNonNull(graphql.ID),
},
"player": &graphql.Field{
Type: player,
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
gameResult, ok := p.Source.(*model.GameResult)
if !ok {
return nil, nil
}
player, err := repo.GetPlayer(gameResult.Player)
if err != nil {
return nil, err
}
return player, nil
},
},
"game": &graphql.Field{
Type: graphql.NewNonNull(game),
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
gameResult, ok := p.Source.(*model.GameResult)
if !ok {
return nil, nil
}
game, err := repo.GetGame(gameResult.Game)
if err != nil {
return nil, err
}
return game, nil
},
},
"startElo": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
},
"endElo": &graphql.Field{
Type: graphql.NewNonNull(graphql.Int),
},
},
})
queryFields := graphql.Fields{
"players": &graphql.Field{
Type: graphql.NewList(player),
Description: "All players",
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
return repo.GetAllPlayers()
},
},
"player": &graphql.Field{
Type: player,
Description: "Get player by ID",
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.ID),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
id, ok := p.Args["id"].(string)
if !ok {
log.Println("Failed to parse ID at player: %v", p.Args["id"])
return nil, nil
}
player, err := repo.GetPlayer(model.PlayerID(id))
if err != nil {
return nil, err
}
return player, nil
},
},
"game": &graphql.Field{
Type: game,
Description: "Get game by ID",
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.ID),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
id, ok := p.Args["id"].(string)
if !ok {
log.Println("Failed to parse ID at game: %v", p.Args["id"])
return nil, nil
}
game, err := repo.GetGame(model.GameID(id))
if err != nil {
return nil, err
}
return game, nil
},
},
"gameResult": &graphql.Field{
Type: gameResult,
Description: "Result of a game on a players ELO",
Args: graphql.FieldConfigArgument{
"id": &graphql.ArgumentConfig{
Type: graphql.NewNonNull(graphql.ID),
},
},
Resolve: func(p graphql.ResolveParams) (interface{}, error) {
id, ok := p.Args["id"].(string)
if !ok {
log.Println("Failed to parse ID at gameResult: %v", p.Args["id"])
return nil, nil
}
gameResult, err := repo.GetGameResult(model.GameResultID(id))
if err != nil {
return nil, err
}
return gameResult, nil
},
},
}
querySchema := graphql.ObjectConfig{
Name: "Query",
Fields: queryFields,
}
schemaConfig := graphql.SchemaConfig{Query: graphql.NewObject(querySchema)}
schema, err := graphql.NewSchema(schemaConfig)
if err != nil {
log.Fatalf("failed to create new schema, error: %v", err)
}
return schema
}