-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontext.go
57 lines (47 loc) · 1.02 KB
/
context.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
package mgql
import (
"context"
"io"
"github.com/99designs/gqlgen/graphql"
"github.com/nrfta/go-log"
"github.com/vektah/gqlparser/v2/gqlerror"
)
type Context struct {
Variables map[string]interface{}
w io.Writer
ctx context.Context
operationName string
wrote bool
}
func (c *Context) check() {
if c.wrote {
log.Fatal("MGQL: You tried to write a response more then once in " + c.operationName)
}
}
func (c *Context) Context() context.Context {
return c.ctx
}
func (c *Context) Data(data interface{}) {
c.check()
c.wrote = true
writeJson(
c.w,
&graphql.Response{
Data: JSON(data),
},
)
}
func (c *Context) GraphqlError(err ...*gqlerror.Error) {
c.check()
c.wrote = true
writeJson(c.w, &graphql.Response{Errors: err})
}
func (c *Context) Error(err ...error) {
c.check()
c.wrote = true
gqlErrors := gqlerror.List{}
for _, e := range err {
gqlErrors = append(gqlErrors, &gqlerror.Error{Message: e.Error()})
}
writeJson(c.w, &graphql.Response{Errors: gqlErrors})
}