Skip to content

Commit

Permalink
Export GetGraphqlServer and accept initialized backend (#2243)
Browse files Browse the repository at this point in the history
Signed-off-by: robert-cronin <[email protected]>
  • Loading branch information
robert-cronin authored Nov 15, 2024
1 parent 4939218 commit bc51756
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 22 deletions.
29 changes: 7 additions & 22 deletions cmd/guacgql/cmd/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,26 +19,25 @@ import (
"context"
"errors"
"fmt"
"github.com/guacsec/guac/pkg/version"
"net/http"
"os"
"os/signal"
"slices"
"syscall"
"time"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/guacsec/guac/pkg/version"

"github.com/99designs/gqlgen/graphql/handler/debug"
"github.com/99designs/gqlgen/graphql/playground"
"github.com/guacsec/guac/pkg/assembler/backends"
"github.com/guacsec/guac/pkg/assembler/backends/arangodb"
_ "github.com/guacsec/guac/pkg/assembler/backends/keyvalue"
"github.com/guacsec/guac/pkg/assembler/backends/neo4j"
"github.com/guacsec/guac/pkg/assembler/backends/neptune"
"github.com/guacsec/guac/pkg/assembler/graphql/generated"
"github.com/guacsec/guac/pkg/assembler/graphql/resolvers"
"github.com/guacsec/guac/pkg/assembler/kv"
"github.com/guacsec/guac/pkg/assembler/kv/redis"
"github.com/guacsec/guac/pkg/assembler/server"
"github.com/guacsec/guac/pkg/logging"
"github.com/guacsec/guac/pkg/metrics"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -79,12 +78,14 @@ func startServer(cmd *cobra.Command) {
os.Exit(1)
}

srv, err := getGraphqlServer(ctx)
backend, err := backends.Get(flags.backend, ctx, getOpts[flags.backend](ctx))
if err != nil {
logger.Errorf("unable to initialize graphql server: %v", err)
logger.Errorf("error creating %v backend: %w", flags.backend, err)
os.Exit(1)
}

srv := server.GetGraphqlServer(ctx, backend)

metric, err := setupPrometheus(ctx, "guacgql")
if err != nil {
logger.Fatalf("Error setting up Prometheus: %v", err)
Expand Down Expand Up @@ -172,22 +173,6 @@ func validateFlags() error {
return nil
}

func getGraphqlServer(ctx context.Context) (*handler.Server, error) {
var topResolver resolvers.Resolver

backend, err := backends.Get(flags.backend, ctx, getOpts[flags.backend](ctx))
if err != nil {
return nil, fmt.Errorf("Error creating %v backend: %w", flags.backend, err)
}
topResolver = resolvers.Resolver{Backend: backend}

config := generated.Config{Resolvers: &topResolver}
config.Directives.Filter = resolvers.Filter
srv := handler.NewDefaultServer(generated.NewExecutableSchema(config))

return srv, nil
}

func healthHandler(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
_, _ = fmt.Fprint(w, "Server is healthy")
Expand Down
18 changes: 18 additions & 0 deletions pkg/assembler/server/server.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package server

import (
"context"

"github.com/99designs/gqlgen/graphql/handler"
"github.com/guacsec/guac/pkg/assembler/backends"
"github.com/guacsec/guac/pkg/assembler/graphql/generated"
"github.com/guacsec/guac/pkg/assembler/graphql/resolvers"
)

func GetGraphqlServer(ctx context.Context, backend backends.Backend) *handler.Server {
topResolver := resolvers.Resolver{Backend: backend}
config := generated.Config{Resolvers: &topResolver}
config.Directives.Filter = resolvers.Filter
srv := handler.NewDefaultServer(generated.NewExecutableSchema(config))
return srv
}
26 changes: 26 additions & 0 deletions pkg/assembler/server/server_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package server

import (
"context"
"testing"

_ "github.com/guacsec/guac/pkg/assembler/backends/keyvalue"

"github.com/guacsec/guac/internal/testing/stablememmap"
"github.com/guacsec/guac/pkg/assembler/backends"
)

func TestGetGraphqlServer(t *testing.T) {
ctx := context.Background()

store := stablememmap.GetStore()
backend, err := backends.Get("keyvalue", ctx, store)
if err != nil {
t.Errorf("Error getting backend: %v", err)
}

srv := GetGraphqlServer(ctx, backend)
if srv == nil {
t.Errorf("Expected GetGraphqlServer to return a non-nil server")
}
}

0 comments on commit bc51756

Please sign in to comment.