Skip to content

Commit 1d7c555

Browse files
committed
Remove unecessary queries
1 parent 052faf4 commit 1d7c555

3 files changed

Lines changed: 28 additions & 90 deletions

File tree

src/rmp_client/client.py

Lines changed: 13 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@
2727
SchoolSearchResult,
2828
)
2929
from .queries import (
30-
GET_SCHOOL_QUERY,
31-
GET_TEACHER_QUERY,
3230
RATINGS_LIST_QUERY,
3331
SCHOOL_RATINGS_LIST_QUERY,
3432
SCHOOL_SEARCH_RESULTS_QUERY,
@@ -285,20 +283,13 @@ def iter_professors_for_school(
285283
# ---- Professor details + ratings ---------------------------------------------
286284

287285
def get_professor(self, professor_id: str) -> Professor:
288-
"""Fetch a single professor by legacy numeric ID (GetTeacherQuery)."""
289-
node_id = _teacher_node_id(professor_id)
290-
data = self.raw_query({
291-
"operationName": "GetTeacherQuery",
292-
"query": GET_TEACHER_QUERY,
293-
"variables": {"id": node_id},
294-
})
286+
"""Fetch a single professor by legacy numeric ID.
295287
296-
node = (data.get("data") or {}).get("node")
297-
if not node:
298-
raise ParsingError(
299-
f"Teacher not found in GraphQL response for id={professor_id}"
300-
)
301-
return self._parse_professor_node(node)
288+
Uses the ratings list query with a minimal page size to retrieve
289+
full teacher details in a single request.
290+
"""
291+
page = self._fetch_professor_ratings_page(professor_id, first=1)
292+
return page.professor
302293

303294
def get_professor_ratings_page(
304295
self,
@@ -399,20 +390,13 @@ def iter_professor_ratings(
399390
# ---- School details + ratings ------------------------------------------------
400391

401392
def get_school(self, school_id: str) -> School:
402-
"""Fetch a single school by legacy numeric ID (GetSchoolQuery)."""
403-
node_id = _school_node_id(school_id)
404-
data = self.raw_query({
405-
"operationName": "GetSchoolQuery",
406-
"query": GET_SCHOOL_QUERY,
407-
"variables": {"id": node_id},
408-
})
393+
"""Fetch a single school by legacy numeric ID.
409394
410-
node = (data.get("data") or {}).get("node")
411-
if not node:
412-
raise ParsingError(
413-
f"School not found in GraphQL response for id={school_id}"
414-
)
415-
return self._parse_school_node(node)
395+
Uses the school ratings list query with a minimal page size to retrieve
396+
full school details (including category summaries) in a single request.
397+
"""
398+
page = self._fetch_school_ratings_page(school_id, first=1)
399+
return page.school
416400

417401
def get_compare_schools(
418402
self, school_id_1: str, school_id_2: str
@@ -596,13 +580,7 @@ def _fetch_school_ratings_page(
596580
"GraphQL response missing data.node (school not found or invalid id)"
597581
)
598582

599-
school = self._parse_school_node({
600-
"id": node.get("legacyId") or node.get("id") or school_id,
601-
"name": node.get("name"),
602-
"city": node.get("city"),
603-
"state": node.get("state"),
604-
"country": node.get("country"),
605-
})
583+
school = self._parse_school_node(node)
606584

607585
ratings_conn = node.get("ratings") or {}
608586
edges = ratings_conn.get("edges") or []

src/rmp_client/queries.py

Lines changed: 14 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,20 @@
7272
city
7373
state
7474
country
75+
numRatings
76+
avgRatingRounded
77+
summary {
78+
campusCondition
79+
campusLocation
80+
careerOpportunities
81+
clubAndEventActivities
82+
foodQuality
83+
internetSpeed
84+
schoolReputation
85+
schoolSafety
86+
schoolSatisfaction
87+
socialActivities
88+
}
7589
ratings(first: $count, after: $cursor) {
7690
edges {
7791
cursor
@@ -165,56 +179,3 @@
165179
}
166180
"""
167181

168-
GET_TEACHER_QUERY = """
169-
query GetTeacherQuery($id: ID!) {
170-
node(id: $id) {
171-
... on Teacher {
172-
id
173-
legacyId
174-
firstName
175-
lastName
176-
department
177-
avgRating
178-
avgDifficulty
179-
numRatings
180-
wouldTakeAgainPercent
181-
school {
182-
id
183-
legacyId
184-
name
185-
city
186-
state
187-
}
188-
}
189-
}
190-
}
191-
"""
192-
193-
GET_SCHOOL_QUERY = """
194-
query GetSchoolQuery($id: ID!) {
195-
node(id: $id) {
196-
... on School {
197-
id
198-
legacyId
199-
name
200-
city
201-
state
202-
country
203-
numRatings
204-
avgRatingRounded
205-
summary {
206-
campusCondition
207-
campusLocation
208-
careerOpportunities
209-
clubAndEventActivities
210-
foodQuality
211-
internetSpeed
212-
schoolReputation
213-
schoolSafety
214-
schoolSatisfaction
215-
socialActivities
216-
}
217-
}
218-
}
219-
}
220-
"""

tests/test_client.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,12 @@ def test_multi_page(self, client: RMPClient) -> None:
388388

389389
class TestRawQuery:
390390
def test_sends_query_and_gets_response(self, client: RMPClient) -> None:
391-
from rmp_client.queries import GET_SCHOOL_QUERY
392391
import base64
393392

394393
node_id = base64.b64encode(f"School-{SCHOOL_QUEENS}".encode()).decode()
395394
result = client.raw_query({
396395
"operationName": "GetSchoolQuery",
397-
"query": GET_SCHOOL_QUERY,
396+
"query": "query GetSchoolQuery($id: ID!) { node(id: $id) { ... on School { id legacyId name } } }",
398397
"variables": {"id": node_id},
399398
})
400399
assert "data" in result

0 commit comments

Comments
 (0)