|
| 1 | +// cache-demo runs only the cache-related subgraphs without requiring NATS. |
| 2 | +package main |
| 3 | + |
| 4 | +import ( |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/99designs/gqlgen/graphql" |
| 10 | + "github.com/99designs/gqlgen/graphql/handler" |
| 11 | + "github.com/99designs/gqlgen/graphql/handler/transport" |
| 12 | + "github.com/99designs/gqlgen/graphql/playground" |
| 13 | + "golang.org/x/sync/errgroup" |
| 14 | + |
| 15 | + "github.com/wundergraph/cosmo/demo/pkg/injector" |
| 16 | + "github.com/wundergraph/cosmo/demo/pkg/subgraphs/cachegraph" |
| 17 | + "github.com/wundergraph/cosmo/demo/pkg/subgraphs/cachegraph_ext" |
| 18 | + "github.com/wundergraph/cosmo/demo/pkg/subgraphs/viewer" |
| 19 | +) |
| 20 | + |
| 21 | +func main() { |
| 22 | + servers := []*http.Server{ |
| 23 | + gqlServer("cachegraph", 4012, cachegraph.NewSchema()), |
| 24 | + gqlServer("cachegraph-ext", 4013, cachegraph_ext.NewSchema()), |
| 25 | + viewerServer(4014), |
| 26 | + } |
| 27 | + |
| 28 | + log.Println("Cache demo subgraphs starting (no NATS required):") |
| 29 | + log.Println(" cachegraph: http://localhost:4012/") |
| 30 | + log.Println(" cachegraph-ext: http://localhost:4013/") |
| 31 | + log.Println(" viewer: http://localhost:4014/") |
| 32 | + |
| 33 | + g := new(errgroup.Group) |
| 34 | + for _, srv := range servers { |
| 35 | + g.Go(srv.ListenAndServe) |
| 36 | + } |
| 37 | + log.Fatal(g.Wait()) |
| 38 | +} |
| 39 | + |
| 40 | +func gqlServer(name string, port int, schema graphql.ExecutableSchema) *http.Server { |
| 41 | + srv := handler.New(schema) |
| 42 | + srv.AddTransport(transport.POST{}) |
| 43 | + srv.AddTransport(transport.GET{}) |
| 44 | + mux := http.NewServeMux() |
| 45 | + mux.Handle("/", playground.Handler(name, "/graphql")) |
| 46 | + mux.Handle("/graphql", srv) |
| 47 | + return &http.Server{Addr: ":" + strconv.Itoa(port), Handler: injector.Latency(injector.HTTP(mux))} |
| 48 | +} |
| 49 | + |
| 50 | +func viewerServer(port int) *http.Server { |
| 51 | + mux := http.NewServeMux() |
| 52 | + mux.Handle("/", playground.Handler("viewer", "/graphql")) |
| 53 | + mux.Handle("/graphql", viewer.NewHandler()) |
| 54 | + return &http.Server{Addr: ":" + strconv.Itoa(port), Handler: injector.Latency(injector.HTTP(mux))} |
| 55 | +} |
0 commit comments