Skip to content

Commit

Permalink
feat: Type identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
mathhulk committed Nov 7, 2024
1 parent e915063 commit 722d3b0
Show file tree
Hide file tree
Showing 15 changed files with 159 additions and 62 deletions.
47 changes: 47 additions & 0 deletions apps/backend/src/modules/class/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GraphQLError, GraphQLScalarType, Kind } from "graphql";

import { getCourse } from "../course/controller";
import { CourseModule } from "../course/generated-types/module-types";
import { getGradeDistributionByClass } from "../grade-distribution/controller";
Expand All @@ -13,6 +15,51 @@ import { IntermediateClass, IntermediateSection } from "./formatter";
import { ClassModule } from "./generated-types/module-types";

const resolvers: ClassModule.Resolvers = {
ClassNumber: new GraphQLScalarType({
name: "ClassNumber",
parseValue: (value) => value,
serialize: (value) => value,
description:
"Unique class number (primary section number), such as 001 or 003",
parseLiteral(ast) {
if (ast.kind === Kind.STRING) return ast.value;

throw new GraphQLError("Provided value is not a class number", {
extensions: { code: "BAD_USER_INPUT" },
});
},
}),

SectionNumber: new GraphQLScalarType({
name: "SectionNumber",
parseValue: (value) => value,
serialize: (value) => value,
description: "Unique section number, such as 101, 105L, or 999",
parseLiteral(ast) {
if (ast.kind === Kind.STRING) return ast.value;

throw new GraphQLError("Provided value is not a section number", {
extensions: { code: "BAD_USER_INPUT" },
});
},
}),

SectionIdentifier: new GraphQLScalarType({
name: "SectionIdentifier",
parseValue: (value) => value,
serialize: (value) => value,
description:
"Unique 5-digit section identifier (CCN), such as 28082 or 67231",
parseLiteral(ast) {
if (ast.kind === Kind.INT && ast.value.length === 5)
return parseInt(ast.value);

throw new GraphQLError("Provided value is not a section identifier", {
extensions: { code: "BAD_USER_INPUT" },
});
},
}),

Query: {
class: async (_, { subject, courseNumber, number, year, semester }) => {
const _class = await getClass(
Expand Down
27 changes: 16 additions & 11 deletions apps/backend/src/modules/class/typedefs/class.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { gql } from "graphql-tag";

export default gql`
scalar ClassNumber
type Query {
class(
year: Int!
semester: Semester!
subject: String!
courseNumber: String!
number: String!
courseNumber: CourseNumber!
number: ClassNumber!
): Class
section(
year: Int!
semester: Semester!
subject: String!
courseNumber: String!
classNumber: String!
number: String!
courseNumber: CourseNumber!
classNumber: ClassNumber!
number: SectionNumber!
): Section
}
type Class {
"Identifiers"
subject: String!
courseNumber: String!
number: String!
courseNumber: CourseNumber!
number: ClassNumber!
year: Int!
semester: Semester!
session: String!
Expand Down Expand Up @@ -84,15 +86,18 @@ export default gql`
IOP
}
scalar SectionNumber
scalar SectionIdentifier
type Section {
"Identifiers"
subject: String!
courseNumber: String!
classNumber: String!
number: String!
courseNumber: CourseNumber!
classNumber: ClassNumber!
number: SectionNumber!
year: Int!
semester: Semester!
ccn: Int!
ccn: SectionIdentifier!
"Relationships"
class: Class!
Expand Down
16 changes: 16 additions & 0 deletions apps/backend/src/modules/course/resolver.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { GraphQLError, GraphQLScalarType, Kind } from "graphql";

import { getGradeDistributionByCourse } from "../grade-distribution/controller";
import {
getAssociatedCourses,
Expand All @@ -9,6 +11,20 @@ import { IntermediateCourse } from "./formatter";
import { CourseModule } from "./generated-types/module-types";

const resolvers: CourseModule.Resolvers = {
CourseNumber: new GraphQLScalarType({
name: "CourseNumber",
parseValue: (value) => value,
serialize: (value) => value,
description: "Unique course number, such as 61A or C54",
parseLiteral(ast) {
if (ast.kind === Kind.STRING) return ast.value;

throw new GraphQLError("Provided value is not a course number", {
extensions: { code: "BAD_USER_INPUT" },
});
},
}),

Query: {
course: async (_, { subject, number }, _context, _info) => {
const course = await getCourse(subject, number);
Expand Down
6 changes: 4 additions & 2 deletions apps/backend/src/modules/course/typedefs/course.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
import { gql } from "graphql-tag";

export default gql`
scalar CourseNumber
type Query {
course(subject: String!, number: String!): Course
course(subject: String!, number: CourseNumber!): Course
courses: [Course!]!
}
type Course {
"Identifiers"
subject: String!
number: String!
number: CourseNumber!
"Relationships"
classes: [Class!]!
Expand Down
4 changes: 2 additions & 2 deletions apps/backend/src/modules/enrollment/typedefs/enrollment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ export default gql`
year: Int!
semester: Semester!
subject: String!
courseNumber: String!
number: String!
courseNumber: CourseNumber!
number: SectionNumber!
): Section!
}
`;
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ export default gql`
type Query {
grade(
subject: String!
courseNumber: String!
classNumber: String
courseNumber: CourseNumber!
classNumber: ClassNumber
year: Int
semester: Semester
givenName: String
Expand Down
12 changes: 6 additions & 6 deletions apps/backend/src/modules/schedule/typedefs/schedule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { gql } from "graphql-tag";
const typedef = gql`
type SelectedClass {
class: Class!
selectedSections: [Int!]
selectedSections: [SectionIdentifier!]
}
type Event {
Expand All @@ -20,7 +20,7 @@ const typedef = gql`
name: String!
createdBy: String!
year: Int!
semester: String!
semester: Semester!
term: Term!
public: Boolean!
classes: [SelectedClass!]!
Expand All @@ -43,9 +43,9 @@ const typedef = gql`
input SelectedClassInput {
subject: String!
courseNumber: String!
number: String!
sections: [Int!]!
courseNumber: CourseNumber!
number: ClassNumber!
sections: [SectionIdentifier!]!
}
input UpdateScheduleInput {
Expand All @@ -58,7 +58,7 @@ const typedef = gql`
input CreateScheduleInput {
name: String!
year: Int!
semester: String!
semester: Semester!
events: [EventInput!]
classes: [SelectedClassInput!]
public: Boolean
Expand Down
6 changes: 3 additions & 3 deletions apps/backend/src/modules/user/typedefs/user.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@ const typedef = gql`
input BookmarkedCourseInput {
subject: String!
number: String!
number: CourseNumber!
}
input BookmarkedClassInput {
year: Int!
semester: Semester!
subject: String!
courseNumber: String!
number: String!
courseNumber: CourseNumber!
number: ClassNumber!
}
input UpdateUserInput {
Expand Down
Loading

0 comments on commit 722d3b0

Please sign in to comment.