endpoints / ember-data-endpoints

Adapter for Ember Data to work with endpoints implementation of jsonapi
MIT License
6 stars 1 forks source link

support nested relations? #9

Open tkellen opened 9 years ago

tkellen commented 9 years ago

Some context: In this example, an organization has projects and a project has phases. Sometimes, a user may wish to see the organization related to the phase, but not want the intermediate project record in the response. This can be accomplished using endpoints with the following:

GET /project-phases/12?include=project.organizations

{
  "linked": {
    "organizations": [
      {
        "id": 12,
        "name": ".....",
      }
    ]
  },
  "project-phases": {
    "id": 1,
    "name": "Phase One",
    "project_id": 25,
    "first_day": "2014-04-21T04:00:00.000Z",
    "last_day": "2014-12-22T05:00:00.000Z",
    "links": {
      "project.organization": {
        "type": "organizations",
        "id": 12
      }
    }
  }
}

Is supporting this even possible with ember-data @bmac?

leobalter commented 9 years ago

It looks it is possible and that's exactly the point where I am trying to solve on my project. The include parameter already works but when I use the async parameter in the model entries, it's not looking at the host and namespace.

leobalter commented 9 years ago

link.related is a relative url and it's necessary to apply the host and namespace.

This can be seen at:

https://github.com/endpoints/ember-data-endpoints/blob/master/addon/json-api-adapter.js#L66

Where I changed both findBelongsTo and findHasMany to:

url = `${Ember.get(this,'host')}/${Ember.get(this,'namespace')}${link.related}`;
bmac commented 9 years ago

Interesting. I feel like endpoints should be returning the full url here. @tkellen thoughts?

tkellen commented 9 years ago

No prob, I can update this (will do first thing tomorrow) It seems like relative URLs should work tho?

On Apr 30, 2015, at 6:04 PM, Brendan McLoughlin notifications@github.com wrote:

Interesting. I feel like endpoints should be returning the full url here. @tkellen https://github.com/tkellen thoughts?

— Reply to this email directly or view it on GitHub https://github.com/endpoints/ember-data-endpoints/issues/9#issuecomment-97983874 .

bmac commented 9 years ago

@leobalter what does the JSON response from the endpoint look like? For some reason I can't get the v2 version of my api to work.

leobalter commented 9 years ago

https://www.dropbox.com/s/lfh807olfu5p7d8/Captura%20de%20tela%202015-04-30%2019.08.34.png

bmac commented 9 years ago

Thanks @leobalter.

@tkellen, I'd prefer not to add the complexity to the adapter to make it try and decide if it needs to add the namespace to a links url. However, if we and/or Ember Data did go down that route then I think these urls should not begin with a / as it makes them look like absolute urls instead of relative urls.

tkellen commented 9 years ago

@bmac Try re-running get-secrets and provisioning? Working on the URLs thing now.

tkellen commented 9 years ago

PS: I need to update the API to only run one version--the one you have checked out on your local machine.

The current deployment setup for the API clones and checks out each version--if you set currentVersion in group_vars/all.yml to v2, it will use the code you have checked out locally. Otherwise it's just a folder on the VM that you don't have access to from your local machine. Whoops.

/cc @bobholt if you have any more trouble.

leobalter commented 9 years ago

IMHO, this looks very like an adapter responsibility. As the host and namespace is part of the adapter settings, relative links should also be resolved by the adapter.

My implementation on #18 is only a workaround and it needs to be improved (needs to check the existence of values, etc) but it seems weird to get the resolved url on the other cases.

bmac commented 9 years ago

@leobalter I disagree. The logic for building the urls should live in the server. It makes it easier to change that logic or update the url in the future without also requiring a change in the client.