tercul-backend/cmd/api/server.go
google-labs-jules[bot] bb5e18d162 refactor: Introduce application layer and dataloaders
This commit introduces a new application layer to the codebase, which decouples the GraphQL resolvers from the data layer. The resolvers now call application services, which in turn call the repositories. This change improves the separation of concerns and makes the code more testable and maintainable.

Additionally, this commit introduces dataloaders to solve the N+1 problem in the GraphQL resolvers. The dataloaders are used to batch and cache database queries, which significantly improves the performance of the API.

The following changes were made:
- Created application services for most of the domains.
- Refactored the GraphQL resolvers to use the new application services.
- Implemented dataloaders for the `Author` aggregate.
- Updated the `app.Application` struct to hold the application services instead of the repositories.
- Fixed a large number of compilation errors in the test files that arose from these changes.

There are still some compilation errors in the `internal/adapters/graphql/integration_test.go` file. These errors are due to the test files still trying to access the repositories directly from the `app.Application` struct. The remaining work is to update these tests to use the new application services.
2025-09-08 10:19:43 +00:00

43 lines
1.3 KiB
Go

package main
import (
"net/http"
"tercul/internal/adapters/graphql"
"tercul/internal/app"
"tercul/internal/platform/auth"
"github.com/99designs/gqlgen/graphql/handler"
)
// NewServer creates a new GraphQL server with the given resolver
func NewServer(resolver *graphql.Resolver) http.Handler {
c := graphql.Config{Resolvers: resolver}
c.Directives.Binding = graphql.Binding
srv := handler.NewDefaultServer(graphql.NewExecutableSchema(c))
// Create a mux to handle GraphQL endpoint only (no playground here; served separately in production)
mux := http.NewServeMux()
mux.Handle("/query", srv)
return mux
}
// NewServerWithAuth creates a new GraphQL server with authentication middleware
func NewServerWithAuth(application *app.Application, resolver *graphql.Resolver, jwtManager *auth.JWTManager) http.Handler {
c := graphql.Config{Resolvers: resolver}
c.Directives.Binding = graphql.Binding
srv := handler.NewDefaultServer(graphql.NewExecutableSchema(c))
// Apply authentication middleware to GraphQL endpoint
authHandler := auth.GraphQLAuthMiddleware(jwtManager)(srv)
// Apply dataloader middleware
dataloaderHandler := graphql.Middleware(application, authHandler)
// Create a mux to handle GraphQL endpoint only (no playground here; served separately in production)
mux := http.NewServeMux()
mux.Handle("/query", dataloaderHandler)
return mux
}