Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Graphql_Query_Cost_Analysis #16

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions .idea/.gitignore

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

9 changes: 9 additions & 0 deletions .idea/graphql-go.iml

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

8 changes: 8 additions & 0 deletions .idea/modules.xml

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

6 changes: 6 additions & 0 deletions .idea/vcs.xml

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

42 changes: 40 additions & 2 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
errlib "errors"
"fmt"
"reflect"
"strconv"
"sync"

"github.com/tokopedia/graphql-go/errors"
Expand Down Expand Up @@ -41,21 +42,55 @@ func makePanicError(value interface{}) *errors.QueryError {
return errors.Errorf("graphql: panic occurred: %v", value)
}

var myMap = make(map[string]string)
var mutex = &sync.RWMutex{}

var objMap = map[string]int{
"String": 0,
"Int": 0,
}
var cost = 0
var firstTime = true

func (r *Request) Execute(ctx context.Context, s *resolvable.Schema, op *query.Operation) ([]byte, []*errors.QueryError) {
var out bytes.Buffer
myMap = make(map[string]string)
cost = 0
func() {
defer r.handlePanic(ctx)
sels := selected.ApplyOperation(&r.Request, s, op)
r.execSelections(ctx, sels, nil, s, s.Resolver, &out, op.Type == query.Mutation)
}()

if err := ctx.Err(); err != nil {
return nil, []*errors.QueryError{errors.Errorf("%s", err)}
}

if !firstTime {
out.WriteByte(',')
out.WriteByte('"')
out.WriteString("QueryCost")
out.WriteByte('"')
out.WriteByte(':')
out.WriteByte([]byte(calculateTheCost())[0])
} else {
firstTime = false
}
return out.Bytes(), r.Errs
}

func calculateTheCost() string {
mutex.Lock()
cost = 0
for _, v := range myMap {
if _, ok := objMap[v]; !ok {
cost += 1
} else {
cost += objMap[v]
}
}
mutex.Unlock()
return strconv.Itoa(cost)
}

type fieldToExec struct {
field *selected.SchemaField
sels []selected.Selection
Expand Down Expand Up @@ -202,6 +237,9 @@ func execFieldSelection(ctx context.Context, r *Request, s *resolvable.Schema, f
in = append(in, f.field.PackedArgs)
}
callOut := f.resolver.Method(f.field.MethodIndex).Call(in)
mutex.Lock()
myMap[f.field.Alias] = f.field.Type.String()
mutex.Unlock()
result = callOut[0]
if f.field.HasError && !callOut[1].IsNil() {
graphQLErr, ok := callOut[1].Interface().(errors.GraphQLError)
Expand Down