-
Notifications
You must be signed in to change notification settings - Fork 3
/
metadata_resolver.go
59 lines (52 loc) · 1.56 KB
/
metadata_resolver.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
package resolvers
import (
"fmt"
"github.com/chirino/graphql/internal/introspection"
"github.com/chirino/graphql/schema"
"reflect"
)
///////////////////////////////////////////////////////////////////////
//
// MetadataResolverFactory resolves fields using schema metadata
//
///////////////////////////////////////////////////////////////////////
type metadataResolver byte
const MetadataResolver = metadataResolver(0)
func (this metadataResolver) Resolve(request *ResolveRequest, next Resolution) Resolution {
s := request.ExecutionContext.GetSchema()
switch request.Field.Name {
case "__typename":
return func() (reflect.Value, error) {
switch schemaType := request.ParentType.(type) {
case *schema.Union:
for _, pt := range schemaType.PossibleTypes {
if _, ok := TryCastFunction(request.Parent, pt.Name); ok {
return reflect.ValueOf(pt.Name), nil
}
}
case *schema.Interface:
for _, pt := range schemaType.PossibleTypes {
if _, ok := TryCastFunction(request.Parent, pt.Name); ok {
return reflect.ValueOf(pt.Name), nil
}
}
default:
return reflect.ValueOf(schemaType.String()), nil
}
return reflect.ValueOf(""), nil
}
case "__schema":
return func() (reflect.Value, error) {
return reflect.ValueOf(introspection.WrapSchema(s)), nil
}
case "__type":
return func() (reflect.Value, error) {
t, ok := s.Types[request.Args["name"].(string)]
if !ok {
return reflect.Value{}, fmt.Errorf("Could not find the type")
}
return reflect.ValueOf(introspection.WrapType(t)), nil
}
}
return next
}