forked from graph-gophers/graphql-go
/
caching.go
43 lines (33 loc) · 791 Bytes
/
caching.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
package caching
import (
"context"
"time"
"github.com/graph-gophers/graphql-go/example/caching/cache"
)
const Schema = `
schema {
query: Query
}
type Query {
hello(name: String!): String!
me: UserProfile!
}
type UserProfile {
name: String!
}
`
type Resolver struct{}
func (r Resolver) Hello(ctx context.Context, args struct{ Name string }) string {
cache.AddHint(ctx, cache.Hint{MaxAge: cache.TTL(1 * time.Hour), Scope: cache.ScopePublic})
return "Hello " + args.Name + "!"
}
func (r Resolver) Me(ctx context.Context) *UserProfile {
cache.AddHint(ctx, cache.Hint{MaxAge: cache.TTL(1 * time.Minute), Scope: cache.ScopePrivate})
return &UserProfile{name: "World"}
}
type UserProfile struct {
name string
}
func (p *UserProfile) Name() string {
return p.name
}