diff --git a/graphql.go b/graphql.go index 9a1643a4..022d172b 100644 --- a/graphql.go +++ b/graphql.go @@ -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) } @@ -266,6 +266,9 @@ 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, @@ -273,10 +276,11 @@ func (s *Schema) exec(ctx context.Context, queryString string, operationName str 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 { diff --git a/internal/exec/exec.go b/internal/exec/exec.go index ce3bde4e..cc7dca55 100644 --- a/internal/exec/exec.go +++ b/internal/exec/exec.go @@ -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) { @@ -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('"') } } } @@ -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, diff --git a/internal/query/query.go b/internal/query/query.go index 0d270ebc..7289b2e6 100644 --- a/internal/query/query.go +++ b/internal/query/query.go @@ -2,6 +2,7 @@ package query import ( "fmt" + "strings" "text/scanner" "github.com/tokopedia/graphql-go/errors" @@ -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 diff --git a/types/const.go b/types/const.go new file mode 100644 index 00000000..209293d1 --- /dev/null +++ b/types/const.go @@ -0,0 +1,6 @@ +package types + +const ( + DUPLICATION_SUFFIX = "_str_auto_" + DUPLICATION_FLAG = "enable_str_auto_duplication" +)