Perex19
Perex19

Reputation: 354

REST endpoint design for retrieving results in many to many relationship

I have read many of the other posts on designing many to many relationships restfully; however, i couldn't quite found the answers i was looking for.

Basically, I have 2 tables in the database and another many-to-many table that bridges these 2 tables. For simplicity lets call them:


I have 2 questions:

1) How should I get all the entries inside the CourseInstructor table.

Is GET /courses/instructors correct? (or GET /instructors/courses or both)

Or should I have a seperate endpoint called /coursesinstructors


2) Also I want to be able to get all rows in the CourseInstructor table by passing in CourseName.

Normally i think i would do, /courses?name=coursename if i was searching for a course. And, I might use the endpoint /courses/{courseId}/instructors/{instructorId} to search for a specific entry in the many-to-many table.

Thus, I am curious would something like this work: /courses?name=coursename/instructors (Is this even possible?)

Another alternative is to have another endpoint called /coursesinstructors and i can make /courseinstructor?name=coursename to get the results.

Or should i make 2 calls to get the results by doing: - get the id through /courses?name=coursename - and follow it with courses/{id}/instructors

I am open to any other suggestions and solutions other than the ones I came up with above (not sure mine are correct solutions anyways).

Upvotes: 0

Views: 782

Answers (2)

Eric Stein
Eric Stein

Reputation: 13672

If you really do need (1), a full dump of all course-instructor pairings, then I think your best bet is to support

GET /course-instructors
GET /course-instructors?courseName=whatever

You can then either link to or embed the course and the instructor.

{
    "course": "/courses/12",
    "instructor": "/instructors/54"
}

{
    "course": {
        "name": "Political Science",
        ...
    },
    "instructor": {
        "name": "Hober Mallow",
        ...
    }
}

What you return depends on the needs of your clients. Perhaps you return abbreviated information plus a link to the full representation, or perhaps it's ok to return just the links because courses and instructors are (should be!) highly cacheable.

As an aside, I think you'd be well-served to stop thinking about how the database stores data, and rather think about what clients need from your API.

Upvotes: 1

Alex Marculescu
Alex Marculescu

Reputation: 5770

For the first scenario, it really depends on what your use cases are. GET /courses/instructors would imply (only) retrieving the instructors, so I'd personally go with just GET /courses, and embed the instructors.

As for the second scenario, /courses?name=coursename should be good enough (plus /courses/{courseId}/instructors/{instructorId} for drilling down to a specific entry). The 2 calls is another valid option. However, an URL like /courses?name=coursename/instructors doesn't seem to be valid, according to RFC 3986.

Upvotes: 1

Related Questions