2016-09-14 3 views

答えて

2

私は、実際に返信したものに対して、わかりやすく、判読可能な答えを見つけ出すことが非常に困難でした。 JSONのAPIで、例えば

彼らは、次のJSONを持っている:

{ 
    "links": { 
    "self": "http://example.com/articles", 
    "next": "http://example.com/articles?page[offset]=2", 
    "last": "http://example.com/articles?page[offset]=10" 
    }, 
    "data": [{ 
    "type": "articles", 
    "id": "1", 
    "attributes": { 
     "title": "JSON API paints my bikeshed!" 
    }, 
    "relationships": { 
     "author": { 
     "links": { 
      "self": "http://example.com/articles/1/relationships/author", 
      "related": "http://example.com/articles/1/author" 
     }, 
     "data": { "type": "people", "id": "9" } 
     }, 
     "comments": { 
     "links": { 
      "self": "http://example.com/articles/1/relationships/comments", 
      "related": "http://example.com/articles/1/comments" 
     }, 
     "data": [ 
      { "type": "comments", "id": "5" }, 
      { "type": "comments", "id": "12" } 
     ] 
     } 
    }, 
    "links": { 
     "self": "http://example.com/articles/1" 
    } 
    }], 
    "included": [{ 
    "type": "people", 
    "id": "9", 
    "attributes": { 
     "first-name": "Dan", 
     "last-name": "Gebhardt", 
     "twitter": "dgeb" 
    }, 
    "links": { 
     "self": "http://example.com/people/9" 
    } 
    }, { 
    "type": "comments", 
    "id": "5", 
    "attributes": { 
     "body": "First!" 
    }, 
    "relationships": { 
     "author": { 
     "data": { "type": "people", "id": "2" } 
     } 
    }, 
    "links": { 
     "self": "http://example.com/comments/5" 
    } 
    }, { 
    "type": "comments", 
    "id": "12", 
    "attributes": { 
     "body": "I like XML better" 
    }, 
    "relationships": { 
     "author": { 
     "data": { "type": "people", "id": "9" } 
     } 
    }, 
    "links": { 
     "self": "http://example.com/comments/12" 
    } 
    }] 
} 

ここでこの行:

"self": "http://example.com/articles/1/relationships/author"

は、 "リレーションシップ・リンク" ここ

この行です:

"self": "http://example.com/articles/1/relationships/comments"

も、「リレーションシップ・リンク」

ですええ、私は他の一つはrelatedと呼ばれているので、それは、混乱して知っています。これらのリンクの目的は何ですか?目的はのみの関係を管理しています。あなたがGET /articles/1/relationships/commentsを行うと言うとではなく、はコメントの情報を返します。 は、リソースタイプ/ IDの配列と、メタデータやリンクのような他のものを返します。例:

{ 
    "data": [{ 
    "type": "comments", 
    "id": "13" 
    }, { 
    "type": "comments", 
    "id": "29" 
    }], 
    "links": { 
    "self": "http://example.com/articles/1/relationships/comments", 
    "next": "http://example.com/articles/1/relationships/comments?page[offset]=2", 
    "last": "http://example.com/articles/1/relationships/comments?page[offset]=4" 
    }, 
    "meta": { 
    "copyright": "Copyright 2015 Example Corp.", 
    "authors": [ 
     "Zach Aysan" 
    ]} 
} 

これはなぜ便利ですか?時々我々はちょうどリソース(コメント、作成者)自体ではなく、関係を削除したいので。たとえば、ユーザーテーブルからユーザーを削除しないDELETE /articles/1/relationships/authorを実行した場合、そのユーザーは作成者として削除されます。いくつかのコメントのみを削除するには、PATCH /articles/1/relationships/commentsを実行し、保存したいコメントのみを含めます。ただし、実際のコメントが正しいと思われる場合は、バックエンドが実際のコメントを削除する可能性があることに注意してください。 (関連記事のないコメントは何ですか?)

他のリンクはどうですか?なぜ/people/9の代わりに/articles/1/authorなのですか?記事の著者は、要求の間にを変更する可能性があり、GET /articles/1/authorは常にという著者を返します。これは通常、PATCH /articles/1/authorのようなものをサポートする必要はありません。なぜなら、通常、リソース自体で変更を指示する方が安全だからです。 PATCH /people/9、記事の編集ページで誰かが自分のアバターを変更した場合など。たとえ管理者がPATCHが正しいリソースに移動したという記事の作者を変更したとしても。

私は知っています。それは少し面倒ですが、Ember Dataのようなものはすべて一緒にスナップしなければならないことが分かっています。

関連する問題