Skip to content

Commit

Permalink
[skip ci] Upgrade Apollo Server
Browse files Browse the repository at this point in the history
  • Loading branch information
mehallhm committed Oct 2, 2024
1 parent 38d9790 commit 5b45b6c
Show file tree
Hide file tree
Showing 17 changed files with 149 additions and 52 deletions.
11 changes: 7 additions & 4 deletions graphql/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { ApolloServer, gql } from "apollo-server";
import { ApolloServer } from "@apollo/server";
import { startStandaloneServer } from "@apollo/server/standalone";
import gql from "graphql-tag";
import GraphQLJSON, { GraphQLJSONObject } from "graphql-type-json";
import macros from "../utils/macros";

Expand Down Expand Up @@ -61,12 +63,13 @@ const server = new ApolloServer({
searchResolvers,
termInfoResolvers,
],
debug: true,
// debug: true,
});

if (require.main === module) {
server
.listen()
startStandaloneServer(server, {
listen: { port: 3000 },
})
.then(({ url }) => {
macros.log(`ready at ${url}`);
return;
Expand Down
10 changes: 7 additions & 3 deletions graphql/resolvers/major.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import { UserInputError } from "apollo-server";
import prisma from "../../services/prisma";
import { Major as PrismaMajor } from "@prisma/client";
import { GraphQLError } from "graphql";

const noResultsError = (recordType): never => {
throw new UserInputError(`${recordType} not found!`);
throw new GraphQLError(`${recordType} not found!`, {
extensions: {
code: "BAD_USER_INPUT",
},
});
};

const getLatestMajorOccurrence = async (
majorId: string
majorId: string,
): Promise<PrismaMajor> => {
const majors: PrismaMajor[] = await prisma.major.findMany({
where: { majorId: majorId },
Expand Down
69 changes: 45 additions & 24 deletions graphql/tests/class.test.seq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
* This file is part of Search NEU and licensed under AGPL3.
* See the license file in the root folder for details.
*/
import { gql } from "apollo-server";
import { GraphQLResponse } from "apollo-server-core";
import gql from "graphql-tag";
import { GraphQLResponse } from "@apollo/server";
import { DocumentNode, GraphQLError } from "graphql";
import prisma from "../../services/prisma";
import server from "../index";
Expand Down Expand Up @@ -164,11 +164,15 @@ describe("returns errors for non-existant classes", () => {
`,
});

expect(res?.errors?.length).toBe(1);
expect(res?.errors?.[0]).toEqual(
if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

expect(res.body.singleResult.errors.length).toBe(1);
expect(res.body.singleResult.errors?.[0]).toEqual(
new GraphQLError(
`We couldn't find any occurrences of a class with subject 'CS' and class ID '2510'`
)
`We couldn't find any occurrences of a class with subject 'CS' and class ID '2510'`,
),
);
});

Expand All @@ -187,10 +191,13 @@ describe("returns errors for non-existant classes", () => {
}
`,
});
if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

expect(res?.errors).toBeUndefined();
expect(res.body.singleResult.errors).toBeUndefined();
// This doesn't throw an error - it just omits any classes for which we have no data
expect(res?.data).toEqual({
expect(res.body.singleResult.data).toEqual({
bulkClasses: [],
});
});
Expand All @@ -207,12 +214,15 @@ describe("returns errors for non-existant classes", () => {
}
`,
});
if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

expect(res?.errors?.length).toBe(1);
expect(res?.errors?.[0]).toEqual(
expect(res.body.singleResult.errors.length).toBe(1);
expect(res.body.singleResult.errors?.[0]).toEqual(
new GraphQLError(
`We couldn't find any occurrences of a class with subject 'CS' and class ID '2510'`
)
`We couldn't find any occurrences of a class with subject 'CS' and class ID '2510'`,
),
);
});

Expand All @@ -229,11 +239,15 @@ describe("returns errors for non-existant classes", () => {
`,
});

expect(res?.errors?.length).toBe(1);
expect(res?.errors?.[0]).toEqual(
if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

expect(res.body.singleResult.errors.length).toBe(1);
expect(res.body.singleResult.errors?.[0]).toEqual(
new GraphQLError(
"We couldn't find a course matching the term '202310', subject 'CS', and class ID '2500'"
)
"We couldn't find a course matching the term '202310', subject 'CS', and class ID '2500'",
),
);
});

Expand All @@ -248,11 +262,15 @@ describe("returns errors for non-existant classes", () => {
`,
});

expect(res?.errors?.length).toBe(1);
expect(res?.errors?.[0]).toEqual(
if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

expect(res.body.singleResult.errors.length).toBe(1);
expect(res.body.singleResult.errors?.[0]).toEqual(
new GraphQLError(
"We couldn't find a course matching the hash 'neu.edu/202310/CS/2500'"
)
"We couldn't find a course matching the hash 'neu.edu/202310/CS/2500'",
),
);
});

Expand All @@ -266,12 +284,15 @@ describe("returns errors for non-existant classes", () => {
}
`,
});
if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

expect(res?.errors?.length).toBe(1);
expect(res?.errors?.[0]).toEqual(
expect(res.body.singleResult.errors.length).toBe(1);
expect(res.body.singleResult.errors?.[0]).toEqual(
new GraphQLError(
"We couldn't find a section matching the hash 'neu.edu/201830/CS/2500/123456'"
)
"We couldn't find a section matching the hash 'neu.edu/201830/CS/2500/123456'",
),
);
});
});
Expand Down
4 changes: 2 additions & 2 deletions graphql/tests/major.test.seq.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";
import prisma from "../../services/prisma";
import server from "../index";
import { DocumentNode } from "graphql";
import { GraphQLResponse } from "apollo-server-core";
import { GraphQLResponse } from "@apollo/server";

const query = async (queryBody: {
query: string | DocumentNode;
Expand Down
11 changes: 8 additions & 3 deletions graphql/tests/search.test.seq.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";
import { mocked } from "jest-mock";
import searcher from "../../services/searcher";
import server from "../index";
import { Course, Requisite, Section } from "../../types/types";
import { DocumentNode } from "graphql";
import { GraphQLResponse } from "apollo-server-core";
import { GraphQLResponse } from "@apollo/server";

jest.mock("../../services/searcher");

Expand Down Expand Up @@ -208,7 +208,12 @@ describe("search resolver", () => {
10,
{},
]);
expect(res.data.search).toEqual({

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

expect(res.body.singleResult.data.search).toEqual({
totalCount: 1,
pageInfo: { hasNextPage: false },
});
Expand Down
2 changes: 1 addition & 1 deletion graphql/typeDefs/class.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";

const typeDef = gql`
extend type Query {
Expand Down
2 changes: 1 addition & 1 deletion graphql/typeDefs/classOccurrence.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";

const typeDef = gql`
type ClassOccurrence {
Expand Down
2 changes: 1 addition & 1 deletion graphql/typeDefs/employee.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";

const typeDef = gql`
type Employee {
Expand Down
2 changes: 1 addition & 1 deletion graphql/typeDefs/major.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";

const typeDef = gql`
extend type Query {
Expand Down
2 changes: 1 addition & 1 deletion graphql/typeDefs/majorOccurrence.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";

const typeDef = gql`
type MajorOccurrence {
Expand Down
2 changes: 1 addition & 1 deletion graphql/typeDefs/search.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";

const typeDef = gql`
extend type Query {
Expand Down
2 changes: 1 addition & 1 deletion graphql/typeDefs/termInfo.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";

const typeDef = gql`
extend type Query {
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@
"fs-extra": "11.2.0",
"got": "14.4.2",
"graphql": "^16.3.0",
"graphql-tag": "^2.12.6",
"graphql-type-json": "^0.3.0",
"he": "^1.2.0",
"jsonwebtoken": "^9.0.0",
Expand Down
34 changes: 32 additions & 2 deletions tests/end_to_end/search.test.git.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";
import server from "../../graphql/index";
import { DocumentNode } from "graphql";
import { GraphQLResponse } from "apollo-server-core";
import { GraphQLResponse } from "@apollo/server";

async function query(q: DocumentNode): Promise<GraphQLResponse> {
return await server.executeOperation({ query: q });
Expand All @@ -24,6 +24,11 @@ describe("Searching for courses", () => {
}
`);

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

// @ts-ignore - added since singleResult is implicitly an unknown
const result = res.data?.search.nodes ?? [];
expect(result.length).toBe(1);
expect(result[0].name).toBe("Fundamentals of Computer Science 1");
Expand All @@ -45,6 +50,11 @@ describe("Searching for courses", () => {
}
`);

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

// @ts-ignore - added since singleResult is implicitly an unknown
const result = res.data?.search?.nodes;
expect(result.length).toBeGreaterThan(0);
}
Expand All @@ -65,6 +75,11 @@ describe("Searching for courses", () => {
}
`);

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

// @ts-ignore - added since singleResult is implicitly an unknown
const result = res.data?.search?.nodes;
expect(result.length).toBe(0);
}
Expand All @@ -85,6 +100,11 @@ describe("Searching for courses", () => {
}
`);

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

// @ts-ignore - added since singleResult is implicitly an unknown
const name = res.data?.search.nodes[0].name;
expect(name).toMatch(/Fundamentals of Computer Science.*/);
});
Expand All @@ -107,6 +127,11 @@ describe("Searching for professors", () => {
}
`);

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

// @ts-ignore - added since singleResult is implicitly an unknown
const obj = res.data?.search.nodes[0];
expect(obj.firstName).toBe("Matthias");
expect(obj.lastName).toBe("Felleisen");
Expand All @@ -127,6 +152,11 @@ describe("Searching for professors", () => {
}
`);

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

// @ts-ignore - added since singleResult is implicitly an unknown
const obj2 = res2.data?.search.nodes[0];
expect(obj2.name).toBe("Jeff Burds");
});
Expand Down
24 changes: 21 additions & 3 deletions tests/end_to_end/setup.test.git.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { gql } from "apollo-server";
import gql from "graphql-tag";
import prisma from "../../services/prisma";
import server from "../../graphql/index";
import { DocumentNode } from "graphql";
import { GraphQLResponse } from "apollo-server-core";
import { GraphQLResponse } from "@apollo/server";

const NUM_TERMIDS = 3;
const NUMS_COURSES = {
Expand Down Expand Up @@ -37,7 +37,13 @@ describe("TermID setup", () => {
}
}
`);
expect(res.data?.termInfos.length).toBe(NUM_TERMIDS);

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

// @ts-ignore - added since singleResult is implicitly an unknown
expect(res.body.singleResult.termInfos.length).toBe(NUM_TERMIDS);
});

test("The termIDs in the database match those in GraphQL", async () => {
Expand All @@ -50,6 +56,12 @@ describe("TermID setup", () => {
}
}
`);

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

// @ts-ignore - added since singleResult is implicitly an unknown
const gqlTermInfos = res.data?.termInfos.map((t) => t.termId).sort();

const dbTermInfos = (
Expand Down Expand Up @@ -105,7 +117,13 @@ describe("Course and section setup", () => {
}
}
`);

if (res.body.kind !== "single") {
fail("incorrect graphql response kind");
}

// It won't exactly match the number we have, but at least make sure we have SOMETHING
// @ts-ignore - added since singleResult is implicitly an unknown
expect(res.data?.search.totalCount).toBeGreaterThan(0);
}
});
Expand Down
Loading

0 comments on commit 5b45b6c

Please sign in to comment.