import { Router } from "express"; import type { Request } from "express"; import { graphqlClient } from "../lib/graphqlClient"; import { respondWithError } from "../lib/error"; import { WorksDocument, AuthorsDocument, type WorksQuery, type AuthorsQuery, } from "../../shared/generated/graphql"; const router = Router(); interface GqlRequest extends Request { gql?: typeof graphqlClient; } // GET /api/search router.get("/", async (req: GqlRequest, res) => { try { const q = req.query.q as string; if (!q) { return res.json({ works: [], authors: [] }); } const client = req.gql || graphqlClient; // Fetch works matching the query const { works } = await client.request( WorksDocument, { search: q, limit: 10 // Limit results for general search } ); // Fetch authors matching the query const { authors } = await client.request( AuthorsDocument, { search: q, limit: 10 } ); res.json({ works, authors }); } catch (error) { respondWithError(res, error, "Failed to perform search"); } }); export default router;