Skip to content

Commit

Permalink
fix gqlgen
Browse files Browse the repository at this point in the history
  • Loading branch information
YZ775 committed Sep 20, 2024
1 parent 7805d53 commit 0f27125
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 72 deletions.
8 changes: 5 additions & 3 deletions sabakan/mock/gqlgen.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
# Generate test GraphQL server.

schema:
- schema.graphql
- schema.graphql
exec:
filename: generated.go
package: mock
model:
filename: models.go
resolver:
filename: resolver.go
type: Resolver
layout: follow-schema
dir: .
package: mock
75 changes: 7 additions & 68 deletions sabakan/mock/resolver.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package mock

import (
"context"
"errors"
"fmt"
"time"
)
// This file will not be regenerated automatically.
//
// It serves as dependency injection for your app, add any dependencies you require here.

// THIS CODE IS A STARTING POINT ONLY. IT WILL NOT BE UPDATED WITH SCHEMA CHANGES.
import "time"

var machines = map[string]*Machine{
"m1": {
Expand Down Expand Up @@ -69,68 +66,10 @@ var machines = map[string]*Machine{
},
}

type Resolver struct{}

type mockResolver struct{}

func (r mockResolver) Query() QueryResolver {
return queryResolver{}
}

type queryResolver struct{}

func (r queryResolver) Machine(ctx context.Context, serial string) (*Machine, error) {
if m, ok := machines[serial]; ok {
return m, nil
}
return nil, errors.New("not found")
}
func (r queryResolver) SearchMachines(ctx context.Context, having *MachineParams, notHaving *MachineParams) ([]*Machine, error) {
if having == nil {
if notHaving == nil {
return []*Machine{machines["m1"], machines["m2"], machines["m3"]}, nil
}
return []*Machine{machines["m1"], machines["m3"]}, nil
}

if len(having.Labels) == 1 {
if having.Labels[0].Name != "foo" {
return nil, errors.New("wrong label name: " + having.Labels[0].Name)
}
if having.Labels[0].Value != "bar" {
return nil, errors.New("wrong label value: " + having.Labels[0].Value)
}
return []*Machine{machines["m3"]}, nil
}

if len(having.Racks) == 1 {
if having.Racks[0] != 1 {
return nil, fmt.Errorf("wrong rack number: %d", having.Racks[0])
}
return []*Machine{machines["m1"]}, nil
}

if len(having.Roles) == 1 {
if having.Roles[0] != "worker" {
return nil, errors.New("wrong role: " + having.Roles[0])
}
return []*Machine{machines["m2"]}, nil
}

if len(having.States) == 1 {
if string(having.States[0]) == "bad" {
return nil, errors.New("bad state value")
}
if having.States[0] != MachineStateUninitialized {
return nil, errors.New("unexpected state: " + string(having.States[0]))
}
return []*Machine{machines["m3"]}, nil
}

if having.MinDaysBeforeRetire != nil {
if *having.MinDaysBeforeRetire != 90 {
return nil, fmt.Errorf("unexpected days: %d", *having.MinDaysBeforeRetire)
}
return []*Machine{machines["m2"]}, nil
}

return nil, nil
return &queryResolver{}
}
77 changes: 77 additions & 0 deletions sabakan/mock/schema.resolvers.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion sabakan/mock/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
// Server creates a mock server that implements sabakan GraphQL API.
func Server() *httptest.Server {
h := handler.New(NewExecutableSchema(Config{
Resolvers: queryResolver{},
Resolvers: mockResolver{},
}))
h.AddTransport(transport.GET{})
h.AddTransport(transport.POST{})
Expand Down

0 comments on commit 0f27125

Please sign in to comment.