@@ -74,28 +74,14 @@ func NewPlanner(planConfiguration *plan.Configuration, definition *ast.Document,
7474 }, nil
7575}
7676
77+ // PlanOperation creates a query plan from an operation file in a pretty-printed text or JSON format
7778func (pl * Planner ) PlanOperation (operationFilePath string , outputFormat PlanOutputFormat ) (string , error ) {
78- operation , err := pl .parseOperation (operationFilePath )
79- if err != nil {
80- return "" , & PlannerOperationValidationError {err : err }
81- }
82-
83- operationName := findOperationName (operation )
84- if operationName == nil {
85- return "" , & PlannerOperationValidationError {err : errors .New ("operation name not found" )}
86- }
87-
88- err = pl .normalizeOperation (operation , operationName )
89- if err != nil {
90- return "" , & PlannerOperationValidationError {err : err }
91- }
92-
93- err = pl .validateOperation (operation )
79+ operation , err := pl .ParseAndPrepareOperation (operationFilePath )
9480 if err != nil {
95- return "" , & PlannerOperationValidationError { err : err }
81+ return "" , err
9682 }
9783
98- rawPlan , err := pl .planOperation (operation )
84+ rawPlan , err := pl .PlanPreparedOperation (operation )
9985 if err != nil {
10086 return "" , fmt .Errorf ("failed to plan operation: %w" , err )
10187 }
@@ -111,31 +97,35 @@ func (pl *Planner) PlanOperation(operationFilePath string, outputFormat PlanOutp
11197 return string (marshal ), nil
11298 }
11399
114- return "" , fmt .Errorf ("invalid type specified: %q" , outputFormat )
100+ return "" , fmt .Errorf ("invalid outputFormat specified: %q" , outputFormat )
115101}
116102
117- func (pl * Planner ) PlanParsedOperation (operation * ast.Document ) (* resolve.FetchTreeQueryPlanNode , error ) {
118- operationName := findOperationName (operation )
119- if operationName == nil {
120- return nil , errors .New ("operation name not found" )
103+ // ParseAndPrepareOperation parses, normalizes and validates the operation
104+ func (pl * Planner ) ParseAndPrepareOperation (operationFilePath string ) (* ast.Document , error ) {
105+ operation , err := pl .parseOperation (operationFilePath )
106+ if err != nil {
107+ return nil , & PlannerOperationValidationError {err : err }
121108 }
122109
123- err := pl .normalizeOperation (operation , operationName )
124- if err != nil {
125- return nil , fmt .Errorf ("failed to normalize operation: %w" , err )
110+ return pl .PrepareOperation (operation )
111+ }
112+
113+ // PrepareOperation normalizes and validates the operation
114+ func (pl * Planner ) PrepareOperation (operation * ast.Document ) (* ast.Document , error ) {
115+ operationName := findOperationName (operation )
116+ if operationName == nil {
117+ return nil , & PlannerOperationValidationError {err : errors .New ("operation name not found" )}
126118 }
127119
128- err = pl .validateOperation (operation )
129- if err != nil {
120+ if err := pl .normalizeOperation (operation , operationName ); err != nil {
130121 return nil , & PlannerOperationValidationError {err : err }
131122 }
132123
133- rawPlan , err := pl .planOperation (operation )
134- if err != nil {
135- return nil , fmt .Errorf ("failed to plan operation: %w" , err )
124+ if err := pl .validateOperation (operation ); err != nil {
125+ return nil , & PlannerOperationValidationError {err : err }
136126 }
137127
138- return rawPlan , nil
128+ return operation , nil
139129}
140130
141131func (pl * Planner ) normalizeOperation (operation * ast.Document , operationName []byte ) (err error ) {
@@ -169,7 +159,8 @@ func (pl *Planner) normalizeOperation(operation *ast.Document, operationName []b
169159 return nil
170160}
171161
172- func (pl * Planner ) planOperation (operation * ast.Document ) (planNode * resolve.FetchTreeQueryPlanNode , err error ) {
162+ // PlanPreparedOperation creates a query plan from a normalized and validated operation
163+ func (pl * Planner ) PlanPreparedOperation (operation * ast.Document ) (planNode * resolve.FetchTreeQueryPlanNode , err error ) {
173164 defer func () {
174165 if r := recover (); r != nil {
175166 err = fmt .Errorf ("panic during plan generation: %v" , r )
@@ -358,8 +349,8 @@ func (pg *PlanGenerator) loadConfiguration(routerConfig *nodev1.RouterConfig, lo
358349 // we need to merge the base schema, it contains the __schema and __type queries
359350 // these are not usually part of a regular GraphQL schema
360351 // the engine needs to have them defined, otherwise it cannot resolve such fields
361- err = asttransform . MergeDefinitionWithBaseSchema ( & definition )
362- if err != nil {
352+
353+ if err := asttransform . MergeDefinitionWithBaseSchema ( & definition ); err != nil {
363354 return fmt .Errorf ("failed to merge graphql schema with base schema: %w" , err )
364355 }
365356
@@ -410,5 +401,6 @@ func findOperationName(operation *ast.Document) (operationName []byte) {
410401 return operation .OperationDefinitionNameBytes (operation .RootNodes [i ].Ref )
411402 }
412403 }
404+ // TODO: assign static operation name if we have single anonymous operation
413405 return nil
414406}
0 commit comments