Skip to content

REST Resources Many to many Relations

Sebastien Dubois edited this page Dec 9, 2016 · 1 revision

Usually, when you have many-to-many relations, you can expose the other side of the relationship on each side.

We'll take the following example: if you model orders and products, products can be part of multiple orders and orders can contain multiple products.

In that case, you could make it so that the products are available from the orders and conversely, that orders are available from the products.

Now, for the case where you want to manipulate the many-to-many relationship, you can expose the relationship itself as a resource, allowing you to perform CRUD operations on it.

For example, you could create a resource such as orders-products:

GET .../orders-products/<uuid>
 
HTTP/1.1 200 OK
{
    "order": {
        "uuid": "..."
    },
    "product": {
        "uuid": "..."
    }
}

In the example above, retrieving a specific item in the many-to-many resource collection returns metadata about the association. The client can use that to locate the linked items.

You MAY also let your API client create the association themselves.

If your API clients do not need to retrieve only the association metadata or create the associations themselves, then you SHOULD simply add links or inline the relations on either or both sides (see section about entity and link expansion).

Here's an example:

GET .../orders/<uuid>
 
HTTP/1.1 200 OK
{
    ...
    "products": [
        {
            "uuid": "..."
        },
        ...
    ]
}

Depending on the quantity of data, you MAY either put the IDs or inline some or all of the data of the other side of the relationship. You could also create specific resources under either or both sides to retrieve only the linked items.

Here's an example: GET .../orders/<uuid>/products

With the example above, you can also easily add support for pagination, filtering, sorting, ...

History

REST

REST

REST API Design Goals

REST Constraints

REST Resources

REST Resources Design Workflow

REST Resources Naming

REST Resources URIs

REST Resources Parameters

REST Resources Single items and collections

REST Resources Relations

REST Resources Many to many Relations

REST Resources Relations expansion

REST Resources Actions

REST API Versioning

REST API Documentation

HTTP

HTTP Methods

HTTP Status Codes

HTTP Status Codes Success (2xx)

HTTP Status Codes Redirection (3xx)

HTTP Status Codes Client Error (4xx)

HTTP Status Codes Server Error (5xx)

Media types

CRUD Operations

CRUD About

CRUD Create Single item

CRUD Retrieve Single item

CRUD Retrieve Collection

CRUD Update Single item

CRUD Delete Single item

Pagination

Pagination About

Pagination Rules and metadata

Pagination Example

Pagination Out of range/bounds

Filtering

Filtering About

Filtering Using styles

Filtering Using includes

Filtering Using excludes

Sorting

Sorting About

Sorting Metadata

Sorting Example

Searching

Searching About

Searching Local search

Searching Scoped search

Searching Global search

Searching Advanced search

Searching Wildcards

Searching Formatting

Long-running operations

Long-running Operations About

Long-running Operations Flow

Long-running Operations Rules

Long-running Operations Example

Concurrency control

Concurrency About

Concurrency Headers to use

Concurrency vs Delete operation

Concurrency vs Pagination

Caching and conditional requests

Caching and conditional requests About

Caching and conditional requests Rules

Caching and conditional requests HTTP headers

Conditional requests

Cache control

Error handling

Error handling About

Error handling Expectations

Error handling Status codes

Error handling Error details

Error handling Example with a single error

Error handling Example with multiple errors

Error handling Example with parameters

Error handling Example with additional metadata

Error handling Warnings

Compression

Compression About

Bulk operations

Bulk operations About

Bulk operations Types

Bulk operations Atomic

Bulk operations Non-atomic

Bulk operations Asynchronous

Bulk operations HTTP status codes

Bulk operations Resources naming convention

Bulk operations Errors

Bulk operations Creation example

Bulk operations Update example

Bulk operations Create and update example

File upload

File upload About

File upload File sizes

File upload Simple file upload

File upload Simple file upload example

File upload Complex file upload

File upload Complex file upload example

Security recommendations

REST Security General recommendations

REST Security Transport layer

REST Security Error handling

REST Security Insecure direct object references

REST Security CORS

REST Security Updates and consistency

REST Security API keys

Miscellaneous

Data formats

Internationalization

Rate limiting

Null values

Dates and times

Redirections

References

Clone this wiki locally