Skip to content

Commit

Permalink
[Fix] variables_default_value_extraction sets type as not nullable wh…
Browse files Browse the repository at this point in the history
…en variable is provided
  • Loading branch information
soonick committed Oct 23, 2023
1 parent 4053758 commit b2ead43
Show file tree
Hide file tree
Showing 9 changed files with 188 additions and 86 deletions.
3 changes: 2 additions & 1 deletion examples/federation/products/graph/entity.resolvers.go

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

2 changes: 1 addition & 1 deletion examples/federation/products/graph/generated/federation.go

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

59 changes: 16 additions & 43 deletions examples/federation/products/graph/generated/generated.go

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

4 changes: 2 additions & 2 deletions examples/federation/products/graph/schema.graphqls
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
extend type Query {
topProducts(first: Int = 5): [Product]
topProducts(first: Int! = 5): [Product]
}

extend type Subscription {
Expand All @@ -13,4 +13,4 @@ type Product @key(fields: "upc") {
name: String!
price: Int!
inStock: Int!
}
}
2 changes: 1 addition & 1 deletion examples/federation/products/graph/schema.resolvers.go

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

27 changes: 17 additions & 10 deletions pkg/astnormalization/variables_default_value_extraction.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/buger/jsonparser"
"github.com/tidwall/sjson"

"github.com/wundergraph/graphql-go-tools/internal/pkg/quotes"
"github.com/wundergraph/graphql-go-tools/internal/pkg/unsafebytes"
"github.com/wundergraph/graphql-go-tools/pkg/ast"
"github.com/wundergraph/graphql-go-tools/pkg/astimport"
Expand Down Expand Up @@ -81,25 +82,31 @@ func (v *variablesDefaultValueExtractionVisitor) EnterVariableDefinition(ref int
// remove variable DefaultValue from operation
v.operation.VariableDefinitions[ref].DefaultValue.IsDefined = false

// skip when variable was provided
_, _, _, err := jsonparser.Get(v.operation.Input.Variables, variableName)
if err == nil {
return
}

// store extracted variable ref
v.extractedVariablesRefs = append(v.extractedVariablesRefs, ref)

valueBytes, err := v.operation.ValueToJSON(v.operation.VariableDefinitionDefaultValue(ref))
if err != nil {
return
// Use the provided value for the variable if present, otherwise, use the default value
valueBytes, dataType, _, _:= jsonparser.Get(v.operation.Input.Variables, variableName)

Check failure on line 89 in pkg/astnormalization/variables_default_value_extraction.go

View workflow job for this annotation

GitHub Actions / Linters

File is not `gofmt`-ed with `-s` (gofmt)
if valueBytes == nil {
jsonVal, err := v.operation.ValueToJSON(v.operation.VariableDefinitionDefaultValue(ref))
if err != nil {
return
}

valueBytes = jsonVal
} else {
if dataType == jsonparser.String {
valueBytes = quotes.WrapBytes(valueBytes)
}
}

v.operation.Input.Variables, err = sjson.SetRawBytes(v.operation.Input.Variables, variableName, valueBytes)
inputVars, err := sjson.SetRawBytes(v.operation.Input.Variables, variableName, valueBytes)
if err != nil {
v.StopWithInternalErr(err)
return
}

v.operation.Input.Variables = inputVars
}

func (v *variablesDefaultValueExtractionVisitor) EnterOperationDefinition(ref int) {
Expand Down
73 changes: 65 additions & 8 deletions pkg/astnormalization/variables_default_value_extraction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ const (
objectInNestedList(input: [[Nested]]): String
stringInNestedList(input: [[String!]]): String
nullableStringInNestedList(input: [[String]]): String
notNullableInt(input: Int! = 5): String
notNullableString(input: String! = "DefaultInSchema"): String
}
input Nested {
NotNullable: String!
Expand Down Expand Up @@ -182,14 +184,14 @@ func TestVariablesDefaultValueExtraction(t *testing.T) {
runWithVariablesDefaultValues(t, extractVariablesDefaultValue, variablesDefaultValueExtractionDefinition, `
query q(
$nullable: String = "a",
$notNullable: String = "b",
$notNullable: String = "b",
) {
objectInList(input: [{NotNullable: $notNullable, Nullable: $nullable}])
}`, "", `
query q(
query q(
$nullable: String,
$notNullable: String!,
) {
) {
objectInList(input: [{NotNullable: $notNullable, Nullable: $nullable}])
}`, ``, `{"notNullable":"b","nullable":"a"}`)
})
Expand All @@ -198,14 +200,14 @@ func TestVariablesDefaultValueExtraction(t *testing.T) {
runWithVariablesDefaultValues(t, extractVariablesDefaultValue, variablesDefaultValueExtractionDefinition, `
query q(
$nullable: String = "a",
$notNullable: String = "b",
$notNullable: String = "b",
) {
objectInNestedList(input: [[{NotNullable: $notNullable, Nullable: $nullable}]])
}`, "", `
query q(
query q(
$nullable: String,
$notNullable: String!,
) {
) {
objectInNestedList(input: [[{NotNullable: $notNullable, Nullable: $nullable}]])
}`, ``, `{"notNullable":"b","nullable":"a"}`)
})
Expand All @@ -219,7 +221,7 @@ func TestVariablesDefaultValueExtraction(t *testing.T) {
}`, "", `
query q(
$notNullable: String!,
) {
) {
stringInNestedList(input: [["a", $notNullable]])
}`, ``, `{"notNullable":"foo"}`)
})
Expand All @@ -233,7 +235,7 @@ func TestVariablesDefaultValueExtraction(t *testing.T) {
}`, "", `
query q(
$nullable: String,
) {
) {
nullableStringInNestedList(input: [["a", null, $nullable]])
}`, ``, `{"nullable":"foo"}`)
})
Expand Down Expand Up @@ -264,6 +266,61 @@ func TestVariablesDefaultValueExtraction(t *testing.T) {
mutation simple($a: String, $b: String, $c: String, $d: String!) {
mixed(a: $a, b: $b, input: $c, nonNullInput: $d)
}`, `{"a":"aaa"}`, `{"d":"bar","c":"foo","b":"bazz","a":"aaa"}`)
})

t.Run("Not nullable int with default value", func(t *testing.T) {
runWithVariablesDefaultValues(t, extractVariablesDefaultValue, variablesDefaultValueExtractionDefinition, `
query q(
$input: Int = 4,
) {
notNullableInt(input: $input)
}`, "", `
query q(
$input: Int!,
) {
notNullableInt(input: $input)
}`, ``, `{"input":4}`)
})

t.Run("not nullable int with default value and variable overwrite", func(t *testing.T) {
runWithVariablesDefaultValues(t, extractVariablesDefaultValue, variablesDefaultValueExtractionDefinition, `
query q(
$input: Int = 4,
) {
notNullableInt(input: $input)
}`, "", `
query q(
$input: Int!,
) {
notNullableInt(input: $input)
}`, `{"input":6}`, `{"input":6}`)
})

t.Run("not nullable string with default value", func(t *testing.T) {
runWithVariablesDefaultValues(t, extractVariablesDefaultValue, variablesDefaultValueExtractionDefinition, `
query q(
$input: String = "DefaultInOperation",
) {
notNullableString(input: $input)
}`, "", `
query q(
$input: String!,
) {
notNullableString(input: $input)
}`, ``, `{"input":"DefaultInOperation"}`)
})

t.Run("not nullable string with default value and variable overwrite", func(t *testing.T) {
runWithVariablesDefaultValues(t, extractVariablesDefaultValue, variablesDefaultValueExtractionDefinition, `
query q(
$input: String = "DefaultInOperation",
) {
notNullableString(input: $input)
}`, "", `
query q(
$input: String!,
) {
notNullableString(input: $input)
}`, `{"input":"ValueInVariable"}`, `{"input":"ValueInVariable"}`)
})
}
Loading

0 comments on commit b2ead43

Please sign in to comment.