implement graphQL and init postgres

This commit is contained in:
Fran Jurmanović
2025-07-06 19:19:36 +02:00
parent 016728532c
commit 26a0d33592
25 changed files with 1713 additions and 314 deletions

View File

@@ -0,0 +1,421 @@
package handler
import (
"context"
"omega-server/local/model"
"omega-server/local/service"
"strings"
"github.com/gofiber/fiber/v2"
)
// GraphQLHandler handles GraphQL requests
type GraphQLHandler struct {
membershipService *service.MembershipService
}
// NewGraphQLHandler creates a new GraphQL handler
func NewGraphQLHandler(membershipService *service.MembershipService) *GraphQLHandler {
return &GraphQLHandler{
membershipService: membershipService,
}
}
// GraphQLRequest represents a GraphQL request
type GraphQLRequest struct {
Query string `json:"query"`
Variables map[string]interface{} `json:"variables"`
}
// GraphQLResponse represents a GraphQL response
type GraphQLResponse struct {
Data interface{} `json:"data,omitempty"`
Errors []GraphQLError `json:"errors,omitempty"`
}
// GraphQLError represents a GraphQL error
type GraphQLError struct {
Message string `json:"message"`
Path []string `json:"path,omitempty"`
}
// Handle processes GraphQL requests
func (h *GraphQLHandler) Handle(c *fiber.Ctx) error {
var req GraphQLRequest
if err := c.BodyParser(&req); err != nil {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Invalid request body"}},
})
}
// Basic query parsing and handling
ctx := c.UserContext()
// Simple query routing based on query string
query := strings.TrimSpace(req.Query)
switch {
case strings.Contains(query, "mutation") && strings.Contains(query, "login"):
return h.handleLogin(c, ctx, req)
case strings.Contains(query, "mutation") && strings.Contains(query, "createUser"):
return h.handleCreateUser(c, ctx, req)
case strings.Contains(query, "mutation") && strings.Contains(query, "createProject"):
return h.handleCreateProject(c, ctx, req)
case strings.Contains(query, "mutation") && strings.Contains(query, "createTask"):
return h.handleCreateTask(c, ctx, req)
case strings.Contains(query, "query") && strings.Contains(query, "me"):
return h.handleMe(c, ctx, req)
case strings.Contains(query, "query") && strings.Contains(query, "users"):
return h.handleUsers(c, ctx, req)
case strings.Contains(query, "query") && strings.Contains(query, "projects"):
return h.handleProjects(c, ctx, req)
case strings.Contains(query, "query") && strings.Contains(query, "tasks"):
return h.handleTasks(c, ctx, req)
default:
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Query not supported"}},
})
}
}
// handleLogin handles login mutations
func (h *GraphQLHandler) handleLogin(c *fiber.Ctx, ctx context.Context, req GraphQLRequest) error {
// Extract variables
email, ok := req.Variables["email"].(string)
if !ok {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Email is required"}},
})
}
password, ok := req.Variables["password"].(string)
if !ok {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Password is required"}},
})
}
// Call service
token, err := h.membershipService.Login(ctx, email, password)
if err != nil {
return c.Status(401).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Invalid credentials"}},
})
}
// Mock user data for now
userData := map[string]interface{}{
"id": "1",
"email": email,
"fullName": "User",
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
}
response := map[string]interface{}{
"login": map[string]interface{}{
"token": token,
"user": userData,
},
}
return c.JSON(GraphQLResponse{Data: response})
}
// handleCreateUser handles user creation mutations
func (h *GraphQLHandler) handleCreateUser(c *fiber.Ctx, ctx context.Context, req GraphQLRequest) error {
// Extract variables
email, ok := req.Variables["email"].(string)
if !ok {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Email is required"}},
})
}
password, ok := req.Variables["password"].(string)
if !ok {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Password is required"}},
})
}
fullName, ok := req.Variables["fullName"].(string)
if !ok {
fullName = ""
}
// Create domain model
user := &model.User{
Email: email,
FullName: fullName,
}
if err := user.SetPassword(password); err != nil {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: err.Error()}},
})
}
// Call service
createdUser, err := h.membershipService.CreateUser(ctx, user, []string{"user"})
if err != nil {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: err.Error()}},
})
}
// Convert user to response format
userData := map[string]interface{}{
"id": createdUser.ID,
"email": createdUser.Email,
"fullName": createdUser.FullName,
"createdAt": createdUser.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
"updatedAt": createdUser.UpdatedAt.Format("2006-01-02T15:04:05Z07:00"),
}
response := map[string]interface{}{
"createUser": userData,
}
return c.JSON(GraphQLResponse{Data: response})
}
// handleMe handles me queries
func (h *GraphQLHandler) handleMe(c *fiber.Ctx, ctx context.Context, req GraphQLRequest) error {
// This would typically extract user ID from JWT token
// For now, return mock data
userData := map[string]interface{}{
"id": "1",
"email": "admin@example.com",
"fullName": "System Administrator",
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
}
response := map[string]interface{}{
"me": userData,
}
return c.JSON(GraphQLResponse{Data: response})
}
// handleUsers handles users queries
func (h *GraphQLHandler) handleUsers(c *fiber.Ctx, ctx context.Context, req GraphQLRequest) error {
// Call service
users, err := h.membershipService.ListUsers(ctx)
if err != nil {
return c.Status(500).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Failed to fetch users"}},
})
}
// Convert users to response format
usersData := make([]map[string]interface{}, len(users))
for i, user := range users {
usersData[i] = map[string]interface{}{
"id": user.ID,
"email": user.Email,
"fullName": user.FullName,
"createdAt": user.CreatedAt.Format("2006-01-02T15:04:05Z07:00"),
"updatedAt": user.UpdatedAt.Format("2006-01-02T15:04:05Z07:00"),
}
}
response := map[string]interface{}{
"users": usersData,
}
return c.JSON(GraphQLResponse{Data: response})
}
// handleCreateProject handles project creation mutations
func (h *GraphQLHandler) handleCreateProject(c *fiber.Ctx, ctx context.Context, req GraphQLRequest) error {
// Extract variables
name, ok := req.Variables["name"].(string)
if !ok {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Name is required"}},
})
}
ownerId, ok := req.Variables["ownerId"].(string)
if !ok {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Owner ID is required"}},
})
}
description, _ := req.Variables["description"].(string)
// Mock project data for now
projectData := map[string]interface{}{
"id": "mock-project-id",
"name": name,
"description": description,
"ownerId": ownerId,
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
}
response := map[string]interface{}{
"createProject": projectData,
}
return c.JSON(GraphQLResponse{Data: response})
}
// handleCreateTask handles task creation mutations
func (h *GraphQLHandler) handleCreateTask(c *fiber.Ctx, ctx context.Context, req GraphQLRequest) error {
// Extract variables
title, ok := req.Variables["title"].(string)
if !ok {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Title is required"}},
})
}
projectId, ok := req.Variables["projectId"].(string)
if !ok {
return c.Status(400).JSON(GraphQLResponse{
Errors: []GraphQLError{{Message: "Project ID is required"}},
})
}
description, _ := req.Variables["description"].(string)
status, _ := req.Variables["status"].(string)
if status == "" {
status = "todo"
}
priority, _ := req.Variables["priority"].(string)
if priority == "" {
priority = "medium"
}
// Mock task data for now
taskData := map[string]interface{}{
"id": "mock-task-id",
"title": title,
"description": description,
"status": status,
"priority": priority,
"projectId": projectId,
"createdAt": "2024-01-01T00:00:00Z",
"updatedAt": "2024-01-01T00:00:00Z",
}
response := map[string]interface{}{
"createTask": taskData,
}
return c.JSON(GraphQLResponse{Data: response})
}
// handleProjects handles projects queries
func (h *GraphQLHandler) handleProjects(c *fiber.Ctx, ctx context.Context, req GraphQLRequest) error {
// Mock empty projects list for now
response := map[string]interface{}{
"projects": []interface{}{},
}
return c.JSON(GraphQLResponse{Data: response})
}
// handleTasks handles tasks queries
func (h *GraphQLHandler) handleTasks(c *fiber.Ctx, ctx context.Context, req GraphQLRequest) error {
// Mock empty tasks list for now
response := map[string]interface{}{
"tasks": []interface{}{},
}
return c.JSON(GraphQLResponse{Data: response})
}
// GetSchema returns the GraphQL schema
func (h *GraphQLHandler) GetSchema() string {
return `
# Core Types
type User {
id: String!
email: String!
fullName: String
createdAt: String!
updatedAt: String!
}
type Project {
id: String!
name: String!
description: String
ownerId: String!
createdAt: String!
updatedAt: String!
}
type Task {
id: String!
title: String!
description: String
status: String!
priority: String!
projectId: String!
createdAt: String!
updatedAt: String!
}
# Input Types
input LoginInput {
email: String!
password: String!
}
input UserCreateInput {
email: String!
fullName: String!
password: String!
}
input ProjectCreateInput {
name: String!
description: String
ownerId: String!
}
input TaskCreateInput {
title: String!
description: String
status: String
priority: String
projectId: String!
}
# Response Types
type AuthResponse {
token: String!
user: User!
}
type MessageResponse {
message: String!
success: Boolean!
}
# Queries
type Query {
me: User!
users: [User!]!
user(id: String!): User
projects: [Project!]!
project(id: String!): Project
tasks(projectId: String): [Task!]!
task(id: String!): Task
}
# Mutations
type Mutation {
login(input: LoginInput!): AuthResponse!
createUser(input: UserCreateInput!): User!
createProject(input: ProjectCreateInput!): Project!
createTask(input: TaskCreateInput!): Task!
}
`
}

View File

@@ -0,0 +1,100 @@
# Minimal GraphQL Schema for Phase 1
# Core Types
type User {
id: String!
email: String!
fullName: String
createdAt: String!
updatedAt: String!
}
type Project {
id: String!
name: String!
description: String
ownerId: String!
createdAt: String!
updatedAt: String!
}
type Task {
id: String!
title: String!
description: String
status: String!
priority: String!
projectId: String!
createdAt: String!
updatedAt: String!
}
# Input Types
input LoginInput {
email: String!
password: String!
}
input UserCreateInput {
email: String!
fullName: String!
password: String!
}
input ProjectCreateInput {
name: String!
description: String
ownerId: String!
}
input TaskCreateInput {
title: String!
description: String
status: String
priority: String
projectId: String!
}
# Response Types
type AuthResponse {
token: String!
user: User!
}
type MessageResponse {
message: String!
success: Boolean!
}
# Queries
type Query {
# Authentication
me: User!
# Users
users: [User!]!
user(id: String!): User
# Projects
projects: [Project!]!
project(id: String!): Project
# Tasks
tasks(projectId: String): [Task!]!
task(id: String!): Task
}
# Mutations
type Mutation {
# Authentication
login(input: LoginInput!): AuthResponse!
# Users
createUser(input: UserCreateInput!): User!
# Projects
createProject(input: ProjectCreateInput!): Project!
# Tasks
createTask(input: TaskCreateInput!): Task!
}

View File

@@ -0,0 +1,173 @@
package service
import (
"context"
"omega-server/local/model"
"omega-server/local/service"
)
// GraphQLService provides GraphQL-specific business logic
type GraphQLService struct {
membershipService *service.MembershipService
}
// NewGraphQLService creates a new GraphQL service
func NewGraphQLService(membershipService *service.MembershipService) *GraphQLService {
return &GraphQLService{
membershipService: membershipService,
}
}
// AuthResponse represents authentication response
type AuthResponse struct {
Token string `json:"token"`
User *model.User `json:"user"`
}
// MessageResponse represents a generic message response
type MessageResponse struct {
Message string `json:"message"`
Success bool `json:"success"`
}
// Login handles user authentication
func (s *GraphQLService) Login(ctx context.Context, email, password string) (*AuthResponse, error) {
token, err := s.membershipService.Login(ctx, email, password)
if err != nil {
return nil, err
}
// For now, return a mock user. In a full implementation, we'd get the user from the token
user := &model.User{
BaseModel: model.BaseModel{
ID: "mock-user-id",
},
Email: email,
FullName: "Mock User",
}
return &AuthResponse{
Token: token,
User: user,
}, nil
}
// CreateUser handles user creation
func (s *GraphQLService) CreateUser(ctx context.Context, email, password, fullName string) (*model.User, error) {
// Create domain model
user := &model.User{
Email: email,
FullName: fullName,
}
if err := user.SetPassword(password); err != nil {
return nil, err
}
return s.membershipService.CreateUser(ctx, user, []string{"user"})
}
// GetUsers retrieves all users
func (s *GraphQLService) GetUsers(ctx context.Context) ([]*model.User, error) {
return s.membershipService.ListUsers(ctx)
}
// GetUser retrieves a specific user by ID
func (s *GraphQLService) GetUser(ctx context.Context, id string) (*model.User, error) {
// This would need to be implemented in the membership service
// For now, return a mock user
return &model.User{
BaseModel: model.BaseModel{
ID: id,
},
Email: "mock@example.com",
FullName: "Mock User",
}, nil
}
// GetMe retrieves the current authenticated user
func (s *GraphQLService) GetMe(ctx context.Context, userID string) (*model.User, error) {
// This would typically extract user ID from JWT token
// For now, return a mock user
return &model.User{
BaseModel: model.BaseModel{
ID: userID,
},
Email: "current@example.com",
FullName: "Current User",
}, nil
}
// CreateProject handles project creation
func (s *GraphQLService) CreateProject(ctx context.Context, name, description, ownerID string) (*model.Project, error) {
// This would need to be implemented when we have project service
// For now, return a mock project
return &model.Project{
BaseModel: model.BaseModel{
ID: "mock-project-id",
},
Name: name,
Description: description,
OwnerID: ownerID,
}, nil
}
// GetProjects retrieves all projects
func (s *GraphQLService) GetProjects(ctx context.Context) ([]*model.Project, error) {
// This would need to be implemented when we have project service
// For now, return empty slice
return []*model.Project{}, nil
}
// GetProject retrieves a specific project by ID
func (s *GraphQLService) GetProject(ctx context.Context, id string) (*model.Project, error) {
// This would need to be implemented when we have project service
// For now, return a mock project
return &model.Project{
BaseModel: model.BaseModel{
ID: id,
},
Name: "Mock Project",
Description: "Mock project description",
OwnerID: "mock-owner-id",
}, nil
}
// CreateTask handles task creation
func (s *GraphQLService) CreateTask(ctx context.Context, title, description, status, priority, projectID string) (*model.Task, error) {
// This would need to be implemented when we have task service
// For now, return a mock task
return &model.Task{
BaseModel: model.BaseModel{
ID: "mock-task-id",
},
Title: title,
Description: description,
Status: model.TaskStatus(status),
Priority: model.TaskPriority(priority),
ProjectID: projectID,
}, nil
}
// GetTasks retrieves tasks, optionally filtered by project ID
func (s *GraphQLService) GetTasks(ctx context.Context, projectID *string) ([]*model.Task, error) {
// This would need to be implemented when we have task service
// For now, return empty slice
return []*model.Task{}, nil
}
// GetTask retrieves a specific task by ID
func (s *GraphQLService) GetTask(ctx context.Context, id string) (*model.Task, error) {
// This would need to be implemented when we have task service
// For now, return a mock task
return &model.Task{
BaseModel: model.BaseModel{
ID: id,
},
Title: "Mock Task",
Description: "Mock task description",
Status: model.TaskStatusTodo,
Priority: model.TaskPriorityMedium,
ProjectID: "mock-project-id",
}, nil
}