forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scalar_context.go
39 lines (31 loc) · 1.04 KB
/
scalar_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
package singlefile
import (
"context"
"io"
"strconv"
"github.com/operandinc/gqlgen/graphql"
)
type StringFromContextInterface struct {
OperationName string
}
var (
_ graphql.ContextMarshaler = StringFromContextInterface{}
_ graphql.ContextUnmarshaler = (*StringFromContextInterface)(nil)
)
func (StringFromContextInterface) MarshalGQLContext(ctx context.Context, w io.Writer) error {
io.WriteString(w, strconv.Quote(graphql.GetFieldContext(ctx).Field.Name))
return nil
}
func (i *StringFromContextInterface) UnmarshalGQLContext(ctx context.Context, v interface{}) error {
i.OperationName = graphql.GetFieldContext(ctx).Field.Name
return nil
}
func MarshalStringFromContextFunction(v string) graphql.ContextMarshaler {
return graphql.ContextWriterFunc(func(ctx context.Context, w io.Writer) error {
io.WriteString(w, strconv.Quote(graphql.GetFieldContext(ctx).Field.Name))
return nil
})
}
func UnmarshalStringFromContextFunction(ctx context.Context, v interface{}) (string, error) {
return graphql.GetFieldContext(ctx).Field.Name, nil
}