-
Notifications
You must be signed in to change notification settings - Fork 54
/
resolver.go
64 lines (54 loc) · 1.29 KB
/
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
60
61
62
63
64
package main
import (
"context"
_ "embed"
"fmt"
"net/http"
"github.com/99designs/gqlgen/graphql"
"github.com/99designs/gqlgen/graphql/handler"
)
var name = "gqlgen-service"
var version = "0.1.0"
//go:embed schema.graphql
var schema string
func newResolver() http.Handler {
c := Config{
Resolvers: &Resolver{
gizmos: generateGizmos(),
service: Service{
Name: name,
Version: version,
Schema: schema,
},
},
Directives: DirectiveRoot{
// Support the @boundary directive as a no-op
Boundary: func(ctx context.Context, obj interface{}, next graphql.Resolver) (res interface{}, err error) {
return next(ctx)
},
},
}
return handler.NewDefaultServer(NewExecutableSchema(c))
}
type Resolver struct {
gizmos map[string]*Gizmo
service Service
}
func (r *Resolver) Query() QueryResolver {
return r
}
func (r *Resolver) Service(ctx context.Context) (*Service, error) {
return &r.service, nil
}
func (r *Resolver) Gizmo(ctx context.Context, id string) (*Gizmo, error) {
if gizmo, ok := r.gizmos[id]; ok {
return gizmo, nil
}
return nil, fmt.Errorf("no gizmo found with id: %s", id)
}
func (r *Resolver) RandomGizmo(ctx context.Context) (*Gizmo, error) {
for _, gizmo := range r.gizmos {
return gizmo, nil
}
return nil, fmt.Errorf("failed to find a gizmo")
}