forked from wundergraph/graphql-go-tools
-
Notifications
You must be signed in to change notification settings - Fork 4
/
execution_engine_v2_custom.go
157 lines (129 loc) · 5.19 KB
/
execution_engine_v2_custom.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package graphql
import (
"context"
"errors"
"sync"
"github.com/TykTechnologies/graphql-go-tools/v2/pkg/engine/plan"
"github.com/TykTechnologies/graphql-go-tools/v2/pkg/engine/postprocess"
"github.com/TykTechnologies/graphql-go-tools/v2/pkg/engine/resolve"
"github.com/TykTechnologies/graphql-go-tools/v2/pkg/operationreport"
)
var (
ErrRequiredStagesMissing = errors.New("required stages for custom execution engine v2 are missing")
)
type CustomExecutionEngineV2NormalizerStage interface {
Normalize(operation *Request) error
}
type CustomExecutionEngineV2ValidatorStage interface {
ValidateForSchema(operation *Request) error
}
type CustomExecutionEngineV2InputValidationStage interface {
InputValidation(operation *Request) error
}
type CustomExecutionEngineV2ResolverStage interface {
Setup(ctx context.Context, postProcessor *postprocess.Processor, resolveContext *resolve.Context, operation *Request, options ...ExecutionOptionsV2)
Plan(postProcessor *postprocess.Processor, operation *Request, report *operationreport.Report) (plan.Plan, error)
Resolve(resolveContext *resolve.Context, planResult plan.Plan, writer resolve.SubscriptionResponseWriter) error
Teardown()
}
type CustomExecutionEngineV2 interface {
CustomExecutionEngineV2NormalizerStage
CustomExecutionEngineV2ValidatorStage
CustomExecutionEngineV2ResolverStage
CustomExecutionEngineV2InputValidationStage
}
type ExecutionEngineV2Executor interface {
Execute(ctx context.Context, operation *Request, writer resolve.SubscriptionResponseWriter, options ...ExecutionOptionsV2) error
}
type CustomExecutionEngineV2Stages struct {
RequiredStages CustomExecutionEngineV2RequiredStages
OptionalStages *CustomExecutionEngineV2OptionalStages
}
func (c *CustomExecutionEngineV2Stages) AllRequiredStagesProvided() bool {
return c.RequiredStages.ResolverStage != nil
}
type CustomExecutionEngineV2RequiredStages struct {
ResolverStage CustomExecutionEngineV2ResolverStage
}
type CustomExecutionEngineV2OptionalStages struct {
NormalizerStage CustomExecutionEngineV2NormalizerStage
ValidatorStage CustomExecutionEngineV2ValidatorStage
InputValidationStage CustomExecutionEngineV2InputValidationStage
}
type CustomExecutionEngineV2Executor struct {
ExecutionStages CustomExecutionEngineV2Stages
internalExecutionContextPool sync.Pool
}
func NewCustomExecutionEngineV2Executor(executionEngineV2 CustomExecutionEngineV2) (*CustomExecutionEngineV2Executor, error) {
executionStages := CustomExecutionEngineV2Stages{
RequiredStages: CustomExecutionEngineV2RequiredStages{
ResolverStage: executionEngineV2,
},
OptionalStages: &CustomExecutionEngineV2OptionalStages{
NormalizerStage: executionEngineV2,
ValidatorStage: executionEngineV2,
},
}
return NewCustomExecutionEngineV2ExecutorByStages(executionStages)
}
func NewCustomExecutionEngineV2ExecutorByStages(executionStages CustomExecutionEngineV2Stages) (*CustomExecutionEngineV2Executor, error) {
return &CustomExecutionEngineV2Executor{
ExecutionStages: executionStages,
internalExecutionContextPool: sync.Pool{
New: func() interface{} {
return newInternalExecutionContext()
},
},
}, nil
}
func (c *CustomExecutionEngineV2Executor) getExecutionCtx() *internalExecutionContext {
return c.internalExecutionContextPool.Get().(*internalExecutionContext)
}
func (c *CustomExecutionEngineV2Executor) putExecutionCtx(ctx *internalExecutionContext) {
ctx.reset()
c.internalExecutionContextPool.Put(ctx)
}
func (c *CustomExecutionEngineV2Executor) Execute(ctx context.Context, operation *Request, writer resolve.SubscriptionResponseWriter, options ...ExecutionOptionsV2) error {
if !c.ExecutionStages.AllRequiredStagesProvided() {
return ErrRequiredStagesMissing
}
var err error
if c.ExecutionStages.OptionalStages != nil && c.ExecutionStages.OptionalStages.NormalizerStage != nil {
err = c.ExecutionStages.OptionalStages.NormalizerStage.Normalize(operation)
if err != nil {
return err
}
}
if c.ExecutionStages.OptionalStages != nil && c.ExecutionStages.OptionalStages.ValidatorStage != nil {
err = c.ExecutionStages.OptionalStages.ValidatorStage.ValidateForSchema(operation)
if err != nil {
return err
}
}
if c.ExecutionStages.OptionalStages != nil && c.ExecutionStages.OptionalStages.InputValidationStage != nil {
if err := c.ExecutionStages.OptionalStages.InputValidationStage.InputValidation(operation); err != nil {
return err
}
}
execContext := c.getExecutionCtx()
defer c.putExecutionCtx(execContext)
execContext.prepare(ctx, operation.Variables, operation.request)
c.ExecutionStages.RequiredStages.ResolverStage.Setup(ctx, execContext.postProcessor, execContext.resolveContext, operation, options...)
var report operationreport.Report
planResult, err := c.ExecutionStages.RequiredStages.ResolverStage.Plan(execContext.postProcessor, operation, &report)
if err != nil {
return err
} else if report.HasErrors() {
return report
}
err = c.ExecutionStages.RequiredStages.ResolverStage.Resolve(execContext.resolveContext, planResult, writer)
if err != nil {
return err
}
c.ExecutionStages.RequiredStages.ResolverStage.Teardown()
return nil
}
// Interface Guards
var (
_ ExecutionEngineV2Executor = (*CustomExecutionEngineV2Executor)(nil)
)