Skip to content

Commit

Permalink
Merge pull request #26 from tokopedia/idoej/skip-auto-field
Browse files Browse the repository at this point in the history
feat(query): Bypass auto duplicate fields
  • Loading branch information
yudiharibowo authored Nov 14, 2024
2 parents 572d326 + c968294 commit a266469
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 31 deletions.
14 changes: 9 additions & 5 deletions graphql.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ type Response struct {
Extensions map[string]interface{} `json:"extensions,omitempty"`
}

//Validate validates the given query with the schema.
// Validate validates the given query with the schema.
func (s *Schema) Validate(queryString string) ([]string, bool, []*errors.QueryError) {
return s.ValidateWithVariables(queryString, nil)
}
Expand Down Expand Up @@ -266,17 +266,21 @@ func (s *Schema) exec(ctx context.Context, queryString string, operationName str
}
}

// check for auto int 64 duplication
check := variables[types.DUPLICATION_FLAG]
enabled, ok := check.(bool)
r := &exec.Request{
Request: selected.Request{
Doc: doc,
Vars: variables,
Schema: s.schema,
DisableIntrospection: s.disableIntrospection,
},
Limiter: make(chan struct{}, s.maxParallelism),
Tracer: s.tracer,
Logger: s.logger,
PanicHandler: s.panicHandler,
Limiter: make(chan struct{}, s.maxParallelism),
Tracer: s.tracer,
Logger: s.logger,
PanicHandler: s.panicHandler,
EnableInt64Duplication: ok && enabled,
}
varTypes := make(map[string]*introspection.Type)
for _, v := range op.Vars {
Expand Down
58 changes: 33 additions & 25 deletions internal/exec/exec.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ type Request struct {
Logger log.Logger
PanicHandler errors.PanicHandler
SubscribeResolverTimeout time.Duration
EnableInt64Duplication bool // Specific flag to indicate duplication of int64 fields to string
}

func (r *Request) handlePanic(ctx context.Context, sels ...[]selected.Selection) {
Expand Down Expand Up @@ -117,34 +118,36 @@ func (r *Request) execSelections(ctx context.Context, sels []selected.Selection,
out.WriteByte(':')
bb := f.out.Bytes()
out.Write(bb)
needStrCounterpart, isArray := isNeedStrCounterpart(f.field)
if needStrCounterpart {
out.WriteByte(',')
out.WriteByte('"')
out.WriteString(f.field.Alias + "_str_auto_")
out.WriteByte('"')
out.WriteByte(':')
if isArray {
var arrStr []int64
err := json.Unmarshal(bb, &arrStr)
if err != nil {
out.Write(bb)
} else {
out.WriteByte('[')
for j, v := range arrStr {
if j > 0 {
out.WriteByte(',')
if r.EnableInt64Duplication {
needStrCounterpart, isArray := isNeedStrCounterpart(f.field)
if needStrCounterpart {
out.WriteByte(',')
out.WriteByte('"')
out.WriteString(f.field.Alias + types.DUPLICATION_SUFFIX)
out.WriteByte('"')
out.WriteByte(':')
if isArray {
var arrStr []int64
err := json.Unmarshal(bb, &arrStr)
if err != nil {
out.Write(bb)
} else {
out.WriteByte('[')
for j, v := range arrStr {
if j > 0 {
out.WriteByte(',')
}
out.WriteByte('"')
out.WriteString(strconv.FormatInt(v, 10))
out.WriteByte('"')
}
out.WriteByte('"')
out.WriteString(strconv.FormatInt(v, 10))
out.WriteByte('"')
out.WriteByte(']')
}
out.WriteByte(']')
} else {
out.WriteByte('"')
out.Write(bb)
out.WriteByte('"')
}
} else {
out.WriteByte('"')
out.Write(bb)
out.WriteByte('"')
}
}
}
Expand All @@ -155,10 +158,15 @@ func (r *Request) execSelections(ctx context.Context, sels []selected.Selection,
// currently limited to these fields only as per Toko-TTS requirement
var (
targetFields = map[string]bool{
"id": true,
"product_id": true,
"productid": true,
"product_ids": true,
"productids": true,
"shop_id": true,
"shopid": true,
"shop_ids": true,
"shopids": true,
}
singularInt64Maps = map[string]bool{
"Int": true,
Expand Down
11 changes: 10 additions & 1 deletion internal/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package query

import (
"fmt"
"strings"
"text/scanner"

"github.com/tokopedia/graphql-go/errors"
Expand Down Expand Up @@ -99,7 +100,15 @@ func parseSelectionSet(l *common.Lexer) []types.Selection {
var sels []types.Selection
l.ConsumeToken('{')
for l.Peek() != '}' {
sels = append(sels, parseSelection(l))
f := parseSelection(l)
switch sel := f.(type) {
case *types.Field:
if !strings.HasSuffix(sel.Alias.Name, types.DUPLICATION_SUFFIX) {
sels = append(sels, f)
}
default:
sels = append(sels, f)
}
}
l.ConsumeToken('}')
return sels
Expand Down
6 changes: 6 additions & 0 deletions types/const.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package types

const (
DUPLICATION_SUFFIX = "_str_auto_"
DUPLICATION_FLAG = "enable_str_auto_duplication"
)

0 comments on commit a266469

Please sign in to comment.