Holiday Context - Relationships

Let's say I have two collection resources:

/persons
/organizations

A GETto /persons/id/returns a specific person. Similarly, a specific organization returns from GETto /organizations/id.

A person may be a member of one or more organizations. In this context of a relationship, we have data such as the person’s role in the organization, the date the person joined the organization, ...

Which design makes sense?

  • A membership resource /memberships/idto which a GETreturns relationship context data (together with a link to the person and organization).

  • A /persons/id/organizations/idand a /organizations/id/persons/id. A GETreturns the context of the relationship to one of the two, and GETredirects it to the other (status code http 303) to the other.

  • Something else?

+4
source share
2 answers

Another option is to embed relationships in the resources themselves. This makes it easier for the customer to track the relationship between resources as they are used. For example, here is a hypothetical one personwith relations to two resources organizationthrough two resources membershipand one of these resources membership:

"person890": {
    "firstName": "Jane",
    "lastName": "Smith",
    "links": [{
        "rel": "membership",
        "href": "memberships/123"
    }, {
        "link": "membership",
        "href": "memberships/456"
    }]
}

"membership123": {
    "role": "chairwoman",
    "date: "12/23/2013",
    "term": "3 years",
    "links": [{
        "rel": "person",
        "href": "persons/890",
    }, {
        "rel": "organization",
        "href": "organizations/7575"
    }]
}

The basic principle of work here is HATEOAS - "Hypermedia as Engine of Application State" - which allows the client with a minimal understanding of your data to still interact with your API.

+1
source

, , . , , API. , , . , API , 1 .

API , , , , . , , , , :

- URI , /persons/id URI /persons . organizations/id URI , /organizations/id/persons URI .

  • 303, . /organizations/id/persons/id /persons/id, .

  • /memberships/id, 1.

  • , - HATEOAS, .

, , :

  • URI.

  • . , /memberships?person_id=X , .

  • , . - , /persons/id?expand=memberships , , , . , , , . , /persons/id?zoom=1 , /persons/id?zoom=2 zoom = 1 , .

0
source

All Articles