import { Router } from "express"; import type { Request } from "express"; import { graphqlClient } from "../lib/graphqlClient"; import { respondWithError } from "../lib/error"; import { GetUserDocument, UsersDocument, UpdateUserDocument, DeleteUserDocument, type GetUserQuery, type UsersQuery, type UpdateUserMutation, type DeleteUserMutation, } from "../../shared/generated/graphql"; const router = Router(); // GET /api/users/:id interface GqlRequest extends Request { gql?: typeof graphqlClient; } router.get("/:id", async (req: GqlRequest, res) => { try { const client = req.gql || graphqlClient; const { user } = await client.request(GetUserDocument, { id: req.params.id, }); res.json(user); } catch (error) { respondWithError(res, error, "Failed to fetch user"); } }); // GET /api/users router.get("/", async (req: GqlRequest, res) => { try { const client = req.gql || graphqlClient; const { users } = await client.request(UsersDocument, { ...req.query, }); res.json(users); } catch (error) { respondWithError(res, error, "Failed to fetch users"); } }); // PUT /api/users/:id router.put("/:id", async (req: GqlRequest, res) => { try { const client = req.gql || graphqlClient; const { updateUser } = await client.request( UpdateUserDocument, { id: req.params.id, input: req.body, } ); res.json(updateUser); } catch (error) { respondWithError(res, error, "Failed to update user"); } }); // DELETE /api/users/:id router.delete("/:id", async (req: GqlRequest, res) => { try { const client = req.gql || graphqlClient; const { deleteUser } = await client.request( DeleteUserDocument, { id: req.params.id, } ); res.json({ success: deleteUser }); } catch (error) { respondWithError(res, error, "Failed to delete user"); } }); export default router;