Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bugfix beta] Add inverse relationship on payload when missing #5608

Merged

Commits on Oct 2, 2018

  1. Configuration menu
    Copy the full SHA
    9540b30 View commit details
    Browse the repository at this point in the history
  2. push leftHandSide relationship info for findHasMany/belongsTo

    always pushes the left hand side of a relationship, even when the
    adapter does not return a payload with the left hand side's ids in
    `relationships`.
    
    What a mouthful. Let's unpack that.
    
    Given the following model definitions:
    
    ```javascript
    // app/models/person.js
    
    export default DS.Model.extend({
      name: attr(),
      dogs: hasMany('dog', { async: true })
    });
    ```
    
    ```javascript
    // app/models/dog.js
    
    export default DS.Model.extend({
      name: attr(),
      person: belongsTo('person', { async: true })
    });
    ```
    
    Given `PersonAdapter#findRecord` returns the following payload and is
    pushed into the store:
    
    ```javascript
    const payload = {
      data: {
        type: 'person',
        id: '1',
        attributes: {
          name: 'John Churchill'
        },
        relationships: {
          dogs: {
            links: {
              related: 'http://exmaple.com/person/1/dogs'
            }
          }
        }
      }
    };
    ```
    
    and the result of `DogAdapter#findHasMany` resolves with the following
    payload:
    
    ```javascript
    {
      data: [
        { id: 1, type: 'dog', attributes: { name: 'Scooby' } },
        { id: 2, type: 'dog', attributes: { name: 'Scrappy' } }
      ]
    }
    ```
    
    Notice how the payload for `DogAdapter#findHasMany` does not have a
    `relationships` key for any of the returned dogs. This is technically
    valid JSONAPI, but in today's Ember Data, *for explicit inverses only*
    (notice how we specified 'person' on the Dog model; an implicit
    relationship would be generated for us if we left it out and used
    internally for tracking various things), the inverse relationship does
    not get set up. For example, `dog.get('person.id')` would still be null
    when it should be `1` (the `id` of the Person it belongsTo).
    
    This commit fixes that by always pushing the inverse relationship using
    the store's RelationshipPayloadsManager. In this example, the following
    relationship info is pushed:
    
    ```javascript
    {
      data: {
        id: '1',
        type: 'person'
      }
    }
    ```
    
    This makes the result of requesting `DogAdapter#findHasMany` effectively
    the following payload:
    
    ```javascript
    {
      data: [
        {
          id: 1,
          type: 'dog',
          attributes: { name: 'Scooby' },
          relationships: { id: '1', type: 'person' }
        },
        {
          id: 2,
          type: 'dog',
          attributes: { name: 'Scrappy' },
          relationships: { id: '1', type: 'person' }
        }
      ]
    }
    ```
    
    Adding this information makes the relationships line up correctly.
    fivetanley committed Oct 2, 2018
    Configuration menu
    Copy the full SHA
    e7b45e2 View commit details
    Browse the repository at this point in the history
  3. Configuration menu
    Copy the full SHA
    5b6c5d4 View commit details
    Browse the repository at this point in the history
  4. Configuration menu
    Copy the full SHA
    2ff7965 View commit details
    Browse the repository at this point in the history
  5. Configuration menu
    Copy the full SHA
    be3dc16 View commit details
    Browse the repository at this point in the history
  6. Configuration menu
    Copy the full SHA
    8344c66 View commit details
    Browse the repository at this point in the history

Commits on Oct 3, 2018

  1. Configuration menu
    Copy the full SHA
    451183f View commit details
    Browse the repository at this point in the history
  2. prettier

    runspired committed Oct 3, 2018
    Configuration menu
    Copy the full SHA
    7aa4499 View commit details
    Browse the repository at this point in the history