forked from 99designs/gqlgen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
selection.go
68 lines (56 loc) · 1.61 KB
/
selection.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
65
66
67
68
//go:generate gorunpkg github.com/99designs/gqlgen
package selection
import (
context "context"
"fmt"
"time"
"github.com/99designs/gqlgen/graphql"
"github.com/vektah/gqlparser/ast"
)
type Resolver struct{}
func (r *Resolver) Query() QueryResolver {
return &queryResolver{r}
}
type queryResolver struct{ *Resolver }
func (r *queryResolver) Events(ctx context.Context) ([]Event, error) {
var sels []string
reqCtx := graphql.GetRequestContext(ctx)
fieldSelections := graphql.GetResolverContext(ctx).Field.Selections
for _, sel := range fieldSelections {
switch sel := sel.(type) {
case *ast.Field:
sels = append(sels, fmt.Sprintf("%s as %s", sel.Name, sel.Alias))
case *ast.InlineFragment:
sels = append(sels, fmt.Sprintf("inline fragment on %s", sel.TypeCondition))
case *ast.FragmentSpread:
fragment := reqCtx.Doc.Fragments.ForName(sel.Name)
sels = append(sels, fmt.Sprintf("named fragment %s on %s", sel.Name, fragment.TypeCondition))
}
}
var events []Event
for i := 0; i < 10; i++ {
if i%2 == 0 {
events = append(events, &Like{
Selection: sels,
Collected: formatCollected(graphql.CollectFieldsCtx(ctx, []string{"Like"})),
Reaction: ":=)",
Sent: time.Now(),
})
} else {
events = append(events, &Post{
Selection: sels,
Collected: formatCollected(graphql.CollectFieldsCtx(ctx, []string{"Post"})),
Message: "Hey",
Sent: time.Now(),
})
}
}
return events, nil
}
func formatCollected(cf []graphql.CollectedField) []string {
var res []string
for _, f := range cf {
res = append(res, fmt.Sprintf("%s as %s", f.Name, f.Alias))
}
return res
}