From 444004e78f546fc4dde8ff1be41e10eddf7bbbe9 Mon Sep 17 00:00:00 2001 From: Eddy Chen <89349085+ecxyzzy@users.noreply.github.com> Date: Sun, 10 Mar 2024 21:31:53 -0700 Subject: [PATCH] =?UTF-8?q?feat:=20=E2=9C=A8=20fixup=20schema,=20add=20gql?= =?UTF-8?q?=20support?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/api/src/routes/v1/graphql/resolvers.ts | 6 ++++++ .../src/routes/v1/graphql/schema/search.graphql | 14 ++++++++++++++ apps/api/src/routes/v1/rest/search/+endpoint.ts | 2 +- apps/api/src/routes/v1/rest/search/schema.ts | 6 +++--- 4 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 apps/api/src/routes/v1/graphql/schema/search.graphql diff --git a/apps/api/src/routes/v1/graphql/resolvers.ts b/apps/api/src/routes/v1/graphql/resolvers.ts index 52e56058..c261d6f1 100644 --- a/apps/api/src/routes/v1/graphql/resolvers.ts +++ b/apps/api/src/routes/v1/graphql/resolvers.ts @@ -23,6 +23,12 @@ export const resolvers: ApolloServerOptions["resolvers"] = { instructors: proxyRestApi("/v1/rest/instructors"), allInstructors: proxyRestApi("/v1/rest/instructors/all"), larc: proxyRestApi("/v1/rest/larc"), + searchCourses: proxyRestApi("/v1/rest/search", { + argsTransform: (args) => ({ ...args, resultType: "course" }), + }), + searchInstructors: proxyRestApi("/v1/rest/search", { + argsTransform: (args) => ({ ...args, resultType: "instructors" }), + }), websoc: proxyRestApi("/v1/rest/websoc", { argsTransform: geTransform }), depts: proxyRestApi("/v1/rest/websoc/depts"), terms: proxyRestApi("/v1/rest/websoc/terms"), diff --git a/apps/api/src/routes/v1/graphql/schema/search.graphql b/apps/api/src/routes/v1/graphql/schema/search.graphql new file mode 100644 index 00000000..e9d66a20 --- /dev/null +++ b/apps/api/src/routes/v1/graphql/schema/search.graphql @@ -0,0 +1,14 @@ +type CourseSearchResult { + count: Int! + results: [Course!]! +} + +type InstructorSearchResult { + count: Int! + results: [Instructor!]! +} + +extend type Query { + searchCourses(query: String!, limit: Int, offset: Int): CourseSearchResult! + searchInstructors(query: String!, limit: Int, offset: Int): InstructorSearchResult! +} diff --git a/apps/api/src/routes/v1/rest/search/+endpoint.ts b/apps/api/src/routes/v1/rest/search/+endpoint.ts index 9438122e..1ba3b374 100644 --- a/apps/api/src/routes/v1/rest/search/+endpoint.ts +++ b/apps/api/src/routes/v1/rest/search/+endpoint.ts @@ -17,7 +17,7 @@ export const GET = createHandler(async (event, context, res) => { const maybeParsed = QuerySchema.safeParse(query); if (maybeParsed.success) { const { data } = maybeParsed; - const keys = Array.from(new Set(u.search(haystack, data.q)[0]?.map((x) => mapping[x]))); + const keys = Array.from(new Set(u.search(haystack, data.query)[0]?.map((x) => mapping[x]))); const results: Array = keys .map((x) => courses[x] ?? instructors[x]) .filter((x) => diff --git a/apps/api/src/routes/v1/rest/search/schema.ts b/apps/api/src/routes/v1/rest/search/schema.ts index fd56b2bd..6b52bf31 100644 --- a/apps/api/src/routes/v1/rest/search/schema.ts +++ b/apps/api/src/routes/v1/rest/search/schema.ts @@ -1,10 +1,10 @@ import { z } from "zod"; export const QuerySchema = z.object({ - q: z.string(), + query: z.string(), resultType: z.enum(["course", "instructor"]).optional(), - limit: z.coerce.number().default(10), - offset: z.coerce.number().default(0), + limit: z.coerce.number().int().default(10), + offset: z.coerce.number().int().default(0), }); export type Query = z.infer;