Skip to content
This repository has been archived by the owner on Oct 18, 2024. It is now read-only.

Commit

Permalink
feat: ✨ fixup schema, add gql support
Browse files Browse the repository at this point in the history
  • Loading branch information
ecxyzzy committed Mar 11, 2024
1 parent 32d3110 commit 444004e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 4 deletions.
6 changes: 6 additions & 0 deletions apps/api/src/routes/v1/graphql/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,12 @@ export const resolvers: ApolloServerOptions<BaseContext>["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"),
Expand Down
14 changes: 14 additions & 0 deletions apps/api/src/routes/v1/graphql/schema/search.graphql
Original file line number Diff line number Diff line change
@@ -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!
}
2 changes: 1 addition & 1 deletion apps/api/src/routes/v1/rest/search/+endpoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Course | Instructor> = keys
.map((x) => courses[x] ?? instructors[x])
.filter((x) =>
Expand Down
6 changes: 3 additions & 3 deletions apps/api/src/routes/v1/rest/search/schema.ts
Original file line number Diff line number Diff line change
@@ -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<typeof QuerySchema>;

0 comments on commit 444004e

Please sign in to comment.