1package graphql
2
3import (
4 "io"
5 "sync"
6)
7
8// Defer will begin executing the given function and immediately return a result that will block until the function completes
9func Defer(f func() Marshaler) Marshaler {
10 var deferred deferred
11 deferred.mu.Lock()
12
13 go func() {
14 deferred.result = f()
15 deferred.mu.Unlock()
16 }()
17
18 return &deferred
19}
20
21type deferred struct {
22 result Marshaler
23 mu sync.Mutex
24}
25
26func (d *deferred) MarshalGQL(w io.Writer) {
27 d.mu.Lock()
28 d.result.MarshalGQL(w)
29 d.mu.Unlock()
30}