Skip to content

Commit

Permalink
fix: issue-717: proper set default value for list variables
Browse files Browse the repository at this point in the history
  • Loading branch information
zaicheng.wang authored and zaicheng.wang committed Jan 9, 2024
1 parent 4fc743b commit a65e53f
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 0 deletions.
2 changes: 2 additions & 0 deletions v2/pkg/astnormalization/inject_input_default_values.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ func (v *inputFieldDefaultInjectionVisitor) jsonWalker(fieldType int, defaultVal
}
if replaced {
*finalVal, err = jsonparser.Set(defaultValue, newVal, fmt.Sprintf("[%d]", i))
defaultValue = *finalVal
if err != nil {
return
}
Expand All @@ -217,6 +218,7 @@ func (v *inputFieldDefaultInjectionVisitor) jsonWalker(fieldType int, defaultVal
}
if replaced {
*finalVal, err = jsonparser.Set(defaultValue, newVal, fmt.Sprintf("[%d]", i))
defaultValue = *finalVal
if err != nil {
return
}
Expand Down
13 changes: 13 additions & 0 deletions v2/pkg/astnormalization/inject_input_default_values_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,19 @@ func TestInputDefaultValueExtraction(t *testing.T) {
}`, `{"a":[{"thirdField":1}]}`, `{"a":[{"thirdField":1,"firstField":"firstField","secondField":1}]}`)
})

t.Run("simple list nested input with more than 2 elements", func(t *testing.T) {
runWithVariablesAssert(t, func(walker *astvisitor.Walker) {
injectInputFieldDefaults(walker)
}, testInputDefaultSchema, `
mutation mutationSimpleInputList($a: [SimpleTestInput]) {
mutationSimpleInputList(data: $a)
}`, "", `
mutation mutationSimpleInputList($a: [SimpleTestInput]) {
mutationSimpleInputList(data: $a)
}`, `{"a":[{"thirdField":1}, {"thirdField":2}, {"thirdField":3}]}`,
`{"a":[{"thirdField":1,"firstField":"firstField","secondField":1}, {"thirdField":2,"firstField":"firstField","secondField":1}, {"thirdField":3,"firstField":"firstField","secondField":1}]}`)
})

t.Run("use custom scalar variable", func(t *testing.T) {
runWithVariablesAssert(t, func(walker *astvisitor.Walker) {
injectInputFieldDefaults(walker)
Expand Down

0 comments on commit a65e53f

Please sign in to comment.