Skip to content

Commit 878f1c1

Browse files
author
abhif22
committed
Logging queries for which input validation fails with reason.
1 parent 87522cc commit 878f1c1

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

graphql.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import (
1616
"github.com/tokopedia/graphql-go/internal/validation"
1717
"github.com/tokopedia/graphql-go/introspection"
1818
"github.com/tokopedia/graphql-go/log"
19+
golog "log"
1920
"github.com/tokopedia/graphql-go/trace"
2021
)
2122

@@ -136,6 +137,12 @@ func (s *Schema) Exec(ctx context.Context, queryString string, operationName str
136137
}
137138

138139
func (s *Schema) exec(ctx context.Context, queryString string, operationName string, variables map[string]interface{}, res *resolvable.Schema) *Response {
140+
141+
var (
142+
logFailedInputValidationQueries, anyOtherValidationError bool
143+
errMessage string
144+
)
145+
139146
doc, qErr := query.Parse(queryString)
140147
if qErr != nil {
141148
return &Response{Errors: []*errors.QueryError{qErr}}
@@ -145,7 +152,20 @@ func (s *Schema) exec(ctx context.Context, queryString string, operationName str
145152
errs := validation.Validate(s.schema, doc, variables, s.maxDepth)
146153
validationFinish(errs)
147154
if len(errs) != 0 {
148-
return &Response{Errors: errs}
155+
for _, err := range errs {
156+
if err.Rule == "VariablesOfCorrectType" {
157+
logFailedInputValidationQueries = true
158+
errMessage = fmt.Sprintln(errMessage, "\n", err.Message)
159+
} else if err.Rule != "" {
160+
anyOtherValidationError = true
161+
}
162+
}
163+
if anyOtherValidationError {
164+
return &Response{Errors: errs}
165+
}
166+
if logFailedInputValidationQueries {
167+
golog.Println("**************\n",errMessage, "\n", queryString, "\n**************")
168+
}
149169
}
150170

151171
op, err := getOperation(doc, operationName)

0 commit comments

Comments
 (0)