Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions execution/engine/execution_engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,28 @@ func (e *ExecutionEngine) Execute(ctx context.Context, operation *graphql.Reques
}
}

// Validate user-supplied and extracted variables against the operation.
// Remap operation variables to canonical names. This mirrors what the cosmo
// router does so that downstream code (planner, cost calc, resolver) always
// goes through VariablesView/RemapVariables when reading variables.
var remapVariables map[string]string
if normalize {
var remapReport operationreport.Report
remapVariables = astnormalization.NewVariablesMapper().NormalizeOperation(
operation.Document(), e.config.schema.Document(), &remapReport,
)
if remapReport.HasErrors() {
return remapReport
}
}

// Validate user-supplied and extracted variables against the (remapped) operation.
// ValidateWithRemap translates renamed names back to originals for both JSON lookup
// and error messages, so users still see their declared variable names in errors.
if len(operation.Variables) > 0 && operation.Variables[0] == '{' {
validator := variablesvalidation.NewVariablesValidator(variablesvalidation.VariablesValidatorOptions{
ApolloCompatibilityFlags: e.apolloCompatibilityFlags,
})
if err := validator.Validate(operation.Document(), e.config.schema.Document(), operation.Variables); err != nil {
if err := validator.ValidateWithRemap(operation.Document(), e.config.schema.Document(), operation.Variables, remapVariables); err != nil {
return err
}
}
Expand All @@ -195,6 +211,7 @@ func (e *ExecutionEngine) Execute(ctx context.Context, operation *graphql.Reques
execContext.setContext(ctx)
execContext.setVariables(operation.Variables)
execContext.setRequest(operation.InternalRequest())
execContext.resolveContext.RemapVariables = remapVariables

for i := range options {
options[i](execContext)
Expand All @@ -215,13 +232,14 @@ func (e *ExecutionEngine) Execute(ctx context.Context, operation *graphql.Reques
if report.HasErrors() {
return report
}
varsView := execContext.resolveContext.VariablesView()
if costCalculator != nil {
costCalculator.ValidateSliceArguments(execContext.resolveContext.Variables, &report)
costCalculator.ValidateSliceArguments(varsView, &report)
if report.HasErrors() {
return report
}
}
operation.ComputeEstimatedCost(costCalculator, execContext.resolveContext.Variables)
operation.ComputeEstimatedCost(costCalculator, varsView)

if execContext.resolveContext.TracingOptions.Enable && !execContext.resolveContext.TracingOptions.ExcludePlannerStats {
planningTime := resolve.GetDurationNanoSinceTraceStart(execContext.resolveContext.Context()) - tracePlanStart
Expand All @@ -240,7 +258,7 @@ func (e *ExecutionEngine) Execute(ctx context.Context, operation *graphql.Reques
return err
}
if resp != nil {
operation.ComputeActualCost(costCalculator, execContext.resolveContext.Variables, execContext.resolveContext.ActualListSizes)
operation.ComputeActualCost(costCalculator, varsView, execContext.resolveContext.ActualListSizes)
}
return nil
case *plan.SubscriptionResponsePlan:
Expand Down
12 changes: 6 additions & 6 deletions execution/engine/execution_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1428,7 +1428,7 @@ func TestExecutionEngine_Execute(t *testing.T) {
testNetHttpClient(t, roundTripperTestCase{
expectedHost: "example.com",
expectedPath: "/",
expectedBody: `{"query":"query($heroNames: [String!]!){heroes(names: $heroNames)}","variables":{"heroNames":["Luke Skywalker","R2-D2"]}}`,
expectedBody: `{"query":"query($a: [String!]!){heroes(names: $a)}","variables":{"a":["Luke Skywalker","R2-D2"]}}`,
sendResponseBody: `{"data":{"heroes":["Human","Droid"]}}`,
sendStatusCode: 200,
}),
Expand Down Expand Up @@ -1494,7 +1494,7 @@ func TestExecutionEngine_Execute(t *testing.T) {
testNetHttpClient(t, roundTripperTestCase{
expectedHost: "example.com",
expectedPath: "/",
expectedBody: `{"query":"query($heroNames: [String!], $height: String){heroes(names: $heroNames, height: $height)}","variables":{"height":null}}`,
expectedBody: `{"query":"query($a: [String!], $b: String){heroes(names: $a, height: $b)}","variables":{"b":null}}`,
sendResponseBody: `{"data":{"heroes":[]}}`,
sendStatusCode: 200,
}),
Expand Down Expand Up @@ -1749,7 +1749,7 @@ func TestExecutionEngine_Execute(t *testing.T) {
testNetHttpClient(t, roundTripperTestCase{
expectedHost: "example.com",
expectedPath: "/",
expectedBody: `{"query":"query($ids: [Int]){charactersByIds(ids: $ids){name}}","variables":{"ids":[1]}}`,
expectedBody: `{"query":"query($a: [Int]){charactersByIds(ids: $a){name}}","variables":{"a":[1]}}`,
sendResponseBody: `{"data":{"charactersByIds":[{"name": "Luke"}]}}`,
sendStatusCode: 200,
}),
Expand Down Expand Up @@ -1879,7 +1879,7 @@ func TestExecutionEngine_Execute(t *testing.T) {
testNetHttpClient(t, roundTripperTestCase{
expectedHost: "example.com",
expectedPath: "/",
expectedBody: `{"query":"query($name: String!, $nameOptional: String){hero(name: $name) hero2: hero(name: $nameOptional)}","variables":{"nameOptional":"R2D2","name":"R2D2"}}`,
expectedBody: `{"query":"query($a: String!, $b: String){hero(name: $a) hero2: hero(name: $b)}","variables":{"b":"R2D2","a":"R2D2"}}`,
sendResponseBody: `{"data":{"hero":"R2D2","hero2":"R2D2"}}`,
sendStatusCode: 200,
}),
Expand Down Expand Up @@ -1942,7 +1942,7 @@ func TestExecutionEngine_Execute(t *testing.T) {
testNetHttpClient(t, roundTripperTestCase{
expectedHost: "example.com",
expectedPath: "/",
expectedBody: `{"query":"query($name: String!, $nameOptional: String){hero(name: $name) hero2: hero(name: $nameOptional)}","variables":{"nameOptional":"Skywalker","name":"Luke"}}`,
expectedBody: `{"query":"query($a: String!, $b: String){hero(name: $a) hero2: hero(name: $b)}","variables":{"b":"Skywalker","a":"Luke"}}`,
sendResponseBody: `{"data":{"hero":"R2D2","hero2":"R2D2"}}`,
sendStatusCode: 200,
}),
Expand Down Expand Up @@ -2004,7 +2004,7 @@ func TestExecutionEngine_Execute(t *testing.T) {
testNetHttpClient(t, roundTripperTestCase{
expectedHost: "example.com",
expectedPath: "/",
expectedBody: `{"query":"query($name: String!, $nameOptional: String!){hero: heroDefault(name: $name) hero2: heroDefault(name: $nameOptional) hero3: heroDefaultRequired(name: $name) hero4: heroDefaultRequired(name: $nameOptional)}","variables":{"nameOptional":"R2D2","name":"R2D2"}}`,
expectedBody: `{"query":"query($a: String!, $b: String!){hero: heroDefault(name: $a) hero2: heroDefault(name: $b) hero3: heroDefaultRequired(name: $a) hero4: heroDefaultRequired(name: $b)}","variables":{"b":"R2D2","a":"R2D2"}}`,
sendResponseBody: `{"data":{"hero":"R2D2","hero2":"R2D2","hero3":"R2D2","hero4":"R2D2"}}`,
sendStatusCode: 200,
}),
Expand Down
22 changes: 10 additions & 12 deletions execution/graphql/request.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import (
"io"
"net/http"

"github.com/wundergraph/astjson"

"github.com/wundergraph/graphql-go-tools/v2/pkg/ast"
"github.com/wundergraph/graphql-go-tools/v2/pkg/astparser"
"github.com/wundergraph/graphql-go-tools/v2/pkg/engine/plan"
Expand All @@ -23,10 +21,10 @@ const (
type OperationType ast.OperationType

const (
OperationTypeUnknown OperationType = OperationType(ast.OperationTypeUnknown)
OperationTypeQuery OperationType = OperationType(ast.OperationTypeQuery)
OperationTypeMutation OperationType = OperationType(ast.OperationTypeMutation)
OperationTypeSubscription OperationType = OperationType(ast.OperationTypeSubscription)
OperationTypeUnknown = OperationType(ast.OperationTypeUnknown)
OperationTypeQuery = OperationType(ast.OperationTypeQuery)
OperationTypeMutation = OperationType(ast.OperationTypeMutation)
OperationTypeSubscription = OperationType(ast.OperationTypeSubscription)
)

var (
Expand Down Expand Up @@ -196,11 +194,11 @@ func (r *Request) OperationType() (OperationType, error) {
return OperationTypeUnknown, nil
}

func (r *Request) ComputeEstimatedCost(calc *plan.CostCalculator, variables *astjson.Value) {
func (r *Request) ComputeEstimatedCost(calc *plan.CostCalculator, vars resolve.VariablesView) {
if calc != nil {
r.estimatedCost = calc.EstimateCost(variables)
r.estimatedCost = calc.EstimateCost(vars)
// Debugging of cost trees. Uncomment to debug.
// fmt.Println(calc.DebugPrint(variables, nil))
// fmt.Println(calc.DebugPrint(vars, nil))
} else {
r.estimatedCost = 0
}
Expand All @@ -210,11 +208,11 @@ func (r *Request) EstimatedCost() int {
return r.estimatedCost
}

func (r *Request) ComputeActualCost(calc *plan.CostCalculator, variables *astjson.Value, actualListSizes map[string]int) {
func (r *Request) ComputeActualCost(calc *plan.CostCalculator, vars resolve.VariablesView, actualListSizes map[string]int) {
if calc != nil {
r.actualCost = calc.ActualCost(variables, actualListSizes)
r.actualCost = calc.ActualCost(vars, actualListSizes)
// Debugging of cost trees. Uncomment to debug.
// fmt.Println(calc.DebugPrint(variables, actualListSizes))
// fmt.Println(calc.DebugPrint(vars, actualListSizes))
} else {
r.actualCost = 0
}
Expand Down
Loading
Loading