|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "errors" |
| 5 | + "fmt" |
| 6 | + "math/rand" |
| 7 | + "net/http" |
| 8 | + "sync/atomic" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/graphql-go/graphql" |
| 12 | + "github.com/graphql-go/handler" |
| 13 | +) |
| 14 | + |
| 15 | +var actionSerial uint64 |
| 16 | + |
| 17 | +type Node struct { |
| 18 | + Name string |
| 19 | + Level int |
| 20 | + Start uint64 |
| 21 | + Ready uint64 |
| 22 | + Fin uint64 |
| 23 | +} |
| 24 | + |
| 25 | +func (n Node) Resolve(p graphql.ResolveParams) (interface{}, error) { |
| 26 | + switch p.Info.FieldName { |
| 27 | + case "nm": |
| 28 | + return n.Name, nil |
| 29 | + case "lvl": |
| 30 | + return n.Level, nil |
| 31 | + case "seq": // h - history |
| 32 | + return fmt.Sprintf(" %d %d %d ", n.Start, n.Ready, n.Fin), nil |
| 33 | + case "sub": |
| 34 | + nextLevel := n.Level + 1 |
| 35 | + nodes := make([]interface{}, 2) |
| 36 | + for i := 0; i < 2; i++ { |
| 37 | + atomic.AddUint64(&actionSerial, 1) |
| 38 | + nn := Node{"Subnode", nextLevel, actionSerial, 0, 0} |
| 39 | + ch := make(chan Node) |
| 40 | + go func() { |
| 41 | + r := rand.Intn(90) + 10 |
| 42 | + time.Sleep(time.Duration(r) * time.Millisecond) |
| 43 | + atomic.AddUint64(&actionSerial, 1) |
| 44 | + nn.Ready = actionSerial |
| 45 | + ch <- nn |
| 46 | + }() |
| 47 | + nodes[i] = func() (interface{}, error) { |
| 48 | + n := <-ch |
| 49 | + atomic.AddUint64(&actionSerial, 1) |
| 50 | + n.Fin = actionSerial |
| 51 | + return n, nil |
| 52 | + } |
| 53 | + } |
| 54 | + return nodes, nil |
| 55 | + } |
| 56 | + return nil, errors.New("Node resolver: Unknown field " + p.Info.FieldName) |
| 57 | +} |
| 58 | + |
| 59 | +func main() { |
| 60 | + |
| 61 | + rand.Seed(time.Now().UTC().UnixNano()) |
| 62 | + |
| 63 | + var nodeType = graphql.NewObject(graphql.ObjectConfig{ |
| 64 | + Name: "Node", |
| 65 | + Fields: graphql.Fields{ |
| 66 | + "nm": &graphql.Field{Type: graphql.NewNonNull(graphql.String)}, |
| 67 | + "lvl": &graphql.Field{Type: graphql.NewNonNull(graphql.Int)}, |
| 68 | + "seq": &graphql.Field{Type: graphql.NewNonNull(graphql.String)}, |
| 69 | + }, |
| 70 | + }) |
| 71 | + |
| 72 | + nodeType.AddFieldConfig("sub", &graphql.Field{ |
| 73 | + Type: graphql.NewNonNull(graphql.NewList(graphql.NewNonNull(nodeType))), |
| 74 | + }) |
| 75 | + |
| 76 | + queryType := graphql.NewObject(graphql.ObjectConfig{ |
| 77 | + Name: "Query", |
| 78 | + Fields: graphql.Fields{ |
| 79 | + "a": &graphql.Field{ |
| 80 | + Name: "a", |
| 81 | + Type: nodeType, |
| 82 | + Resolve: func(p graphql.ResolveParams) (interface{}, error) { |
| 83 | + actionSerial = 0 |
| 84 | + return Node{"Root node", 0, 0, 0, 0}, nil |
| 85 | + }, |
| 86 | + }, |
| 87 | + }, |
| 88 | + }) |
| 89 | + |
| 90 | + var schema, err = graphql.NewSchema(graphql.SchemaConfig{ |
| 91 | + Query: queryType, |
| 92 | + }) |
| 93 | + if err != nil { |
| 94 | + panic(err) |
| 95 | + } |
| 96 | + |
| 97 | + handler := handler.New(&handler.Config{ |
| 98 | + Schema: &schema, |
| 99 | + Pretty: true, |
| 100 | + GraphiQL: true, |
| 101 | + Playground: true, |
| 102 | + }) |
| 103 | + http.Handle("/gql", handler) |
| 104 | + fmt.Println("Run...") |
| 105 | + http.ListenAndServe(":8080", nil) |
| 106 | +} |
0 commit comments