Skip to content

Commit 228dbb5

Browse files
committed
Add extension interface and handlers
1 parent 21fe71d commit 228dbb5

File tree

1 file changed

+98
-0
lines changed

1 file changed

+98
-0
lines changed

extensions.go

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
package graphql
2+
3+
import (
4+
"context"
5+
6+
"github.com/graphql-go/graphql/gqlerrors"
7+
)
8+
9+
// Extension is an interface for extensions in graphql
10+
type Extension interface {
11+
// Init is used to help you initialize the extension
12+
Init(context.Context, *Params)
13+
// Name returns the name of the extension (make sure it's custom)
14+
Name() string
15+
// ParseDidStart ...
16+
ParseDidStart(context.Context)
17+
// ParseDidStart ...
18+
ParseEnded(context.Context, error)
19+
// ValidationDidStart ...
20+
ValidationDidStart(context.Context)
21+
// ValidationEnded ...
22+
ValidationEnded(context.Context, []gqlerrors.FormattedError)
23+
// ExecutionDidStart notifies about the start of the execution
24+
ExecutionDidStart(context.Context)
25+
// ExecutionEnded notifies about the end of the execution
26+
ExecutionEnded(context.Context)
27+
// ResolveFieldDidStart notifies about the start of the resolving of a field
28+
ResolveFieldDidStart(context.Context, *ResolveInfo)
29+
// ResolveFieldEnded notifies about the end of the resolving of a field
30+
ResolveFieldEnded(context.Context, *ResolveInfo)
31+
// HasResult returns if the extension wants to add data to the result
32+
HasResult() bool
33+
// GetResult returns the data that the extension wants to add to the result
34+
GetResult(context.Context) interface{}
35+
}
36+
37+
// handleExtensionsInits handles all the init functions for all the extensions in the schema
38+
func handleExtensionsInits(p *Params) {
39+
for _, ext := range p.Schema.extensions {
40+
ext.Init(p.Context, p)
41+
}
42+
}
43+
44+
// handleExtensionsParseDidStart ...
45+
func handleExtensionsParseDidStart(p *Params) {
46+
for _, ext := range p.Schema.extensions {
47+
ext.ParseDidStart(p.Context)
48+
}
49+
}
50+
51+
// handleExtensionsParseEnded ...
52+
func handleExtensionsParseEnded(p *Params, err error) {
53+
for _, ext := range p.Schema.extensions {
54+
ext.ParseEnded(p.Context, err)
55+
}
56+
}
57+
58+
// handleExtensionsValidationDidStart ...
59+
func handleExtensionsValidationDidStart(p *Params) {
60+
for _, ext := range p.Schema.extensions {
61+
ext.ValidationDidStart(p.Context)
62+
}
63+
}
64+
65+
// handleExtensionsValidationEnded ...
66+
func handleExtensionsValidationEnded(p *Params, errs []gqlerrors.FormattedError) {
67+
for _, ext := range p.Schema.extensions {
68+
ext.ValidationEnded(p.Context, errs)
69+
}
70+
}
71+
72+
// handleExecutionDidStart handles the ExecutionDidStart functions
73+
func handleExtensionsExecutionDidStart(p *ExecuteParams) {
74+
for _, ext := range p.Schema.extensions {
75+
ext.ExecutionDidStart(p.Context)
76+
}
77+
}
78+
79+
// handleExecutionEnded handles the notification of the extensions about the end of the execution
80+
func handleExtensionsExecutionEnded(p *ExecuteParams) {
81+
for _, ext := range p.Schema.extensions {
82+
ext.ExecutionEnded(p.Context)
83+
}
84+
}
85+
86+
// handleResolveFieldDidStart handles the notification of the extensions about the start of a resolve function
87+
func handleExtensionsResolveFieldDidStart(exts []Extension, p *executionContext, i *ResolveInfo) {
88+
for _, ext := range exts {
89+
ext.ResolveFieldDidStart(p.Context, i)
90+
}
91+
}
92+
93+
// handleResolveFieldEnded handles the notification of the extensions about the end of a resolve function
94+
func handleExtensionsResolveFieldEnded(exts []Extension, p *executionContext, i *ResolveInfo) {
95+
for _, ext := range exts {
96+
ext.ResolveFieldEnded(p.Context, i)
97+
}
98+
}

0 commit comments

Comments
 (0)