~xenrox/10man-api

34e890073834dadfdb446ef18277eef00e1b2688 — Thorben Günther 10 months ago 59bb320
Move getTeams from mutation to query
3 files changed, 98 insertions(+), 88 deletions(-)

M graph/generated/generated.go
M graph/schema.graphqls
M graph/schema.resolvers.go
M graph/generated/generated.go => graph/generated/generated.go +62 -52
@@ 8,6 8,7 @@ import (
	"errors"
	"strconv"
	"sync"
	"sync/atomic"

	"git.xenrox.net/~xenrox/10man-api/graph/model"
	"github.com/99designs/gqlgen/graphql"


@@ 49,12 50,12 @@ type ComplexityRoot struct {
		CreateUser  func(childComplexity int, input model.NewUser) int
		DeleteUser  func(childComplexity int, id int) int
		FinishMatch func(childComplexity int, winner string) int
		GetTeams    func(childComplexity int, id *int) int
		StartQueue  func(childComplexity int, teamspeakID string) int
		UpdateUser  func(childComplexity int, id int, input model.UserInput) int
	}

	Query struct {
		GetTeams    func(childComplexity int, id *int) int
		UserBySteam func(childComplexity int, steamID string) int
		UserByTs    func(childComplexity int, teamspeakID string) int
	}


@@ 84,11 85,11 @@ type MutationResolver interface {
	CreateMatch(ctx context.Context) (int, error)
	CancelMatch(ctx context.Context) (string, error)
	FinishMatch(ctx context.Context, winner string) (string, error)
	GetTeams(ctx context.Context, id *int) (*model.Teams, error)
}
type QueryResolver interface {
	UserBySteam(ctx context.Context, steamID string) (*model.User, error)
	UserByTs(ctx context.Context, teamspeakID string) (*model.User, error)
	GetTeams(ctx context.Context, id *int) (*model.Teams, error)
}

type executableSchema struct {


@@ 168,18 169,6 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in

		return e.complexity.Mutation.FinishMatch(childComplexity, args["winner"].(string)), true

	case "Mutation.getTeams":
		if e.complexity.Mutation.GetTeams == nil {
			break
		}

		args, err := ec.field_Mutation_getTeams_args(context.TODO(), rawArgs)
		if err != nil {
			return 0, false
		}

		return e.complexity.Mutation.GetTeams(childComplexity, args["id"].(*int)), true

	case "Mutation.startQueue":
		if e.complexity.Mutation.StartQueue == nil {
			break


@@ 204,6 193,18 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in

		return e.complexity.Mutation.UpdateUser(childComplexity, args["id"].(int), args["input"].(model.UserInput)), true

	case "Query.getTeams":
		if e.complexity.Query.GetTeams == nil {
			break
		}

		args, err := ec.field_Query_getTeams_args(context.TODO(), rawArgs)
		if err != nil {
			return 0, false
		}

		return e.complexity.Query.GetTeams(childComplexity, args["id"].(*int)), true

	case "Query.userBySteam":
		if e.complexity.Query.UserBySteam == nil {
			break


@@ 373,6 374,7 @@ type Teams {
type Query {
    userBySteam(steamID: String!): User
    userByTS(teamspeakID: String!): User
    getTeams(id: Int): Teams!
}

input NewUser {


@@ 398,7 400,6 @@ type Mutation {
    createMatch: Int!
    cancelMatch: String!
    finishMatch(winner: String!): String!
    getTeams(id: Int): Teams!
}
`, BuiltIn: false},
}


@@ 468,21 469,6 @@ func (ec *executionContext) field_Mutation_finishMatch_args(ctx context.Context,
	return args, nil
}

func (ec *executionContext) field_Mutation_getTeams_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
	var err error
	args := map[string]interface{}{}
	var arg0 *int
	if tmp, ok := rawArgs["id"]; ok {
		ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id"))
		arg0, err = ec.unmarshalOInt2ᚖint(ctx, tmp)
		if err != nil {
			return nil, err
		}
	}
	args["id"] = arg0
	return args, nil
}

func (ec *executionContext) field_Mutation_startQueue_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
	var err error
	args := map[string]interface{}{}


@@ 537,6 523,21 @@ func (ec *executionContext) field_Query___type_args(ctx context.Context, rawArgs
	return args, nil
}

func (ec *executionContext) field_Query_getTeams_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
	var err error
	args := map[string]interface{}{}
	var arg0 *int
	if tmp, ok := rawArgs["id"]; ok {
		ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id"))
		arg0, err = ec.unmarshalOInt2ᚖint(ctx, tmp)
		if err != nil {
			return nil, err
		}
	}
	args["id"] = arg0
	return args, nil
}

func (ec *executionContext) field_Query_userBySteam_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
	var err error
	args := map[string]interface{}{}


@@ 924,7 925,7 @@ func (ec *executionContext) _Mutation_finishMatch(ctx context.Context, field gra
	return ec.marshalNString2string(ctx, field.Selections, res)
}

func (ec *executionContext) _Mutation_getTeams(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
func (ec *executionContext) _Query_userBySteam(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
	defer func() {
		if r := recover(); r != nil {
			ec.Error(ctx, ec.Recover(ctx, r))


@@ 932,7 933,7 @@ func (ec *executionContext) _Mutation_getTeams(ctx context.Context, field graphq
		}
	}()
	fc := &graphql.FieldContext{
		Object:     "Mutation",
		Object:     "Query",
		Field:      field,
		Args:       nil,
		IsMethod:   true,


@@ 941,7 942,7 @@ func (ec *executionContext) _Mutation_getTeams(ctx context.Context, field graphq

	ctx = graphql.WithFieldContext(ctx, fc)
	rawArgs := field.ArgumentMap(ec.Variables)
	args, err := ec.field_Mutation_getTeams_args(ctx, rawArgs)
	args, err := ec.field_Query_userBySteam_args(ctx, rawArgs)
	if err != nil {
		ec.Error(ctx, err)
		return graphql.Null


@@ 949,24 950,21 @@ func (ec *executionContext) _Mutation_getTeams(ctx context.Context, field graphq
	fc.Args = args
	resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
		ctx = rctx // use context from middleware stack in children
		return ec.resolvers.Mutation().GetTeams(rctx, args["id"].(*int))
		return ec.resolvers.Query().UserBySteam(rctx, args["steamID"].(string))
	})
	if err != nil {
		ec.Error(ctx, err)
		return graphql.Null
	}
	if resTmp == nil {
		if !graphql.HasFieldError(ctx, fc) {
			ec.Errorf(ctx, "must not be null")
		}
		return graphql.Null
	}
	res := resTmp.(*model.Teams)
	res := resTmp.(*model.User)
	fc.Result = res
	return ec.marshalNTeams2ᚖgitᚗxenroxᚗnetᚋאxenroxᚋ10manᚑapiᚋgraphᚋmodelᚐTeams(ctx, field.Selections, res)
	return ec.marshalOUser2ᚖgitᚗxenroxᚗnetᚋאxenroxᚋ10manᚑapiᚋgraphᚋmodelᚐUser(ctx, field.Selections, res)
}

func (ec *executionContext) _Query_userBySteam(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
func (ec *executionContext) _Query_userByTS(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
	defer func() {
		if r := recover(); r != nil {
			ec.Error(ctx, ec.Recover(ctx, r))


@@ 983,7 981,7 @@ func (ec *executionContext) _Query_userBySteam(ctx context.Context, field graphq

	ctx = graphql.WithFieldContext(ctx, fc)
	rawArgs := field.ArgumentMap(ec.Variables)
	args, err := ec.field_Query_userBySteam_args(ctx, rawArgs)
	args, err := ec.field_Query_userByTS_args(ctx, rawArgs)
	if err != nil {
		ec.Error(ctx, err)
		return graphql.Null


@@ 991,7 989,7 @@ func (ec *executionContext) _Query_userBySteam(ctx context.Context, field graphq
	fc.Args = args
	resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
		ctx = rctx // use context from middleware stack in children
		return ec.resolvers.Query().UserBySteam(rctx, args["steamID"].(string))
		return ec.resolvers.Query().UserByTs(rctx, args["teamspeakID"].(string))
	})
	if err != nil {
		ec.Error(ctx, err)


@@ 1005,7 1003,7 @@ func (ec *executionContext) _Query_userBySteam(ctx context.Context, field graphq
	return ec.marshalOUser2ᚖgitᚗxenroxᚗnetᚋאxenroxᚋ10manᚑapiᚋgraphᚋmodelᚐUser(ctx, field.Selections, res)
}

func (ec *executionContext) _Query_userByTS(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
func (ec *executionContext) _Query_getTeams(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {
	defer func() {
		if r := recover(); r != nil {
			ec.Error(ctx, ec.Recover(ctx, r))


@@ 1022,7 1020,7 @@ func (ec *executionContext) _Query_userByTS(ctx context.Context, field graphql.C

	ctx = graphql.WithFieldContext(ctx, fc)
	rawArgs := field.ArgumentMap(ec.Variables)
	args, err := ec.field_Query_userByTS_args(ctx, rawArgs)
	args, err := ec.field_Query_getTeams_args(ctx, rawArgs)
	if err != nil {
		ec.Error(ctx, err)
		return graphql.Null


@@ 1030,18 1028,21 @@ func (ec *executionContext) _Query_userByTS(ctx context.Context, field graphql.C
	fc.Args = args
	resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
		ctx = rctx // use context from middleware stack in children
		return ec.resolvers.Query().UserByTs(rctx, args["teamspeakID"].(string))
		return ec.resolvers.Query().GetTeams(rctx, args["id"].(*int))
	})
	if err != nil {
		ec.Error(ctx, err)
		return graphql.Null
	}
	if resTmp == nil {
		if !graphql.HasFieldError(ctx, fc) {
			ec.Errorf(ctx, "must not be null")
		}
		return graphql.Null
	}
	res := resTmp.(*model.User)
	res := resTmp.(*model.Teams)
	fc.Result = res
	return ec.marshalOUser2ᚖgitᚗxenroxᚗnetᚋאxenroxᚋ10manᚑapiᚋgraphᚋmodelᚐUser(ctx, field.Selections, res)
	return ec.marshalNTeams2ᚖgitᚗxenroxᚗnetᚋאxenroxᚋ10manᚑapiᚋgraphᚋmodelᚐTeams(ctx, field.Selections, res)
}

func (ec *executionContext) _Query___type(ctx context.Context, field graphql.CollectedField) (ret graphql.Marshaler) {


@@ 2700,11 2701,6 @@ func (ec *executionContext) _Mutation(ctx context.Context, sel ast.SelectionSet)
			if out.Values[i] == graphql.Null {
				invalids++
			}
		case "getTeams":
			out.Values[i] = ec._Mutation_getTeams(ctx, field)
			if out.Values[i] == graphql.Null {
				invalids++
			}
		default:
			panic("unknown field " + strconv.Quote(field.Name))
		}


@@ 2753,6 2749,20 @@ func (ec *executionContext) _Query(ctx context.Context, sel ast.SelectionSet) gr
				res = ec._Query_userByTS(ctx, field)
				return res
			})
		case "getTeams":
			field := field
			out.Concurrently(i, func() (res graphql.Marshaler) {
				defer func() {
					if r := recover(); r != nil {
						ec.Error(ctx, ec.Recover(ctx, r))
					}
				}()
				res = ec._Query_getTeams(ctx, field)
				if res == graphql.Null {
					atomic.AddUint32(&invalids, 1)
				}
				return res
			})
		case "__type":
			out.Values[i] = ec._Query___type(ctx, field)
		case "__schema":

M graph/schema.graphqls => graph/schema.graphqls +1 -1
@@ 16,6 16,7 @@ type Teams {
type Query {
    userBySteam(steamID: String!): User
    userByTS(teamspeakID: String!): User
    getTeams(id: Int): Teams!
}

input NewUser {


@@ 41,5 42,4 @@ type Mutation {
    createMatch: Int!
    cancelMatch: String!
    finishMatch(winner: String!): String!
    getTeams(id: Int): Teams!
}

M graph/schema.resolvers.go => graph/schema.resolvers.go +35 -35
@@ 388,7 388,41 @@ func (r *mutationResolver) FinishMatch(ctx context.Context, winner string) (stri
	return "FinishSuccess", nil
}

func (r *mutationResolver) GetTeams(ctx context.Context, id *int) (*model.Teams, error) {
func (r *queryResolver) UserBySteam(ctx context.Context, steamID string) (*model.User, error) {
	var user model.User
	user.SteamID = steamID

	query := `
		SELECT id, teamspeak_id, Elo, admin, avatar, name
		FROM "User"
		WHERE steam_id = $1`
	err := database.DB.QueryRow(query, steamID).Scan(&user.ID,
		&user.TeamspeakID, &user.Elo, &user.Admin, &user.Avatar, &user.Name)
	if err != nil {
		return nil, database.CheckErrorCode(err)
	}

	return &user, nil
}

func (r *queryResolver) UserByTs(ctx context.Context, teamspeakID string) (*model.User, error) {
	var user model.User
	user.TeamspeakID = teamspeakID

	query := `
		SELECT id, steam_id, Elo, admin, avatar, name
		FROM "User"
		WHERE teamspeak_id = $1`
	err := database.DB.QueryRow(query, teamspeakID).Scan(&user.ID,
		&user.SteamID, &user.Elo, &user.Admin, &user.Avatar, &user.Name)
	if err != nil {
		return nil, database.CheckErrorCode(err)
	}

	return &user, nil
}

func (r *queryResolver) GetTeams(ctx context.Context, id *int) (*model.Teams, error) {
	var teams model.Teams
	var team1, team2 []*model.User
	var err error


@@ 468,40 502,6 @@ func (r *mutationResolver) GetTeams(ctx context.Context, id *int) (*model.Teams,
	return &teams, nil
}

func (r *queryResolver) UserBySteam(ctx context.Context, steamID string) (*model.User, error) {
	var user model.User
	user.SteamID = steamID

	query := `
		SELECT id, teamspeak_id, Elo, admin, avatar, name
		FROM "User"
		WHERE steam_id = $1`
	err := database.DB.QueryRow(query, steamID).Scan(&user.ID,
		&user.TeamspeakID, &user.Elo, &user.Admin, &user.Avatar, &user.Name)
	if err != nil {
		return nil, database.CheckErrorCode(err)
	}

	return &user, nil
}

func (r *queryResolver) UserByTs(ctx context.Context, teamspeakID string) (*model.User, error) {
	var user model.User
	user.TeamspeakID = teamspeakID

	query := `
		SELECT id, steam_id, Elo, admin, avatar, name
		FROM "User"
		WHERE teamspeak_id = $1`
	err := database.DB.QueryRow(query, teamspeakID).Scan(&user.ID,
		&user.SteamID, &user.Elo, &user.Admin, &user.Avatar, &user.Name)
	if err != nil {
		return nil, database.CheckErrorCode(err)
	}

	return &user, nil
}

// Mutation returns generated.MutationResolver implementation.
func (r *Resolver) Mutation() generated.MutationResolver { return &mutationResolver{r} }