data icon indicating copy to clipboard operation
data copied to clipboard

polymorphic docs don't mention how to find/fetch polymorphic

Open BryanCrotaz opened this issue 7 years ago • 4 comments

Would expect to see how to find/fetch exactly my-model or my-model and all its descendants.

BryanCrotaz avatar Oct 30 '17 17:10 BryanCrotaz

Agreed -- they're also unclear on whether you have to do anything special with JSONAPI:

Note that, for polymorphism to work, Ember Data expects a "type" declaration polymorphic type via the reserved type property on the model. Confused?

I can't make head or tail of this sentence. Do I somehow have to mark types polymorphic with JSONAPI, or it enough that they just have a particular relationship?

smackesey avatar Jun 01 '18 18:06 smackesey

Yeah that sentence is pretty awful :(

Roughly speaking, always give the most specific type possible to any resource your API returns.

For instance, if dog extends pet, return dog not pet. ember-data would consider dog:1 and pet:1 to be separate entities if you were to return both, and any relationships would become weirdly broken.

runspired avatar Jun 03 '18 09:06 runspired

My serializer needs the following code for my polymorphic relationship props to work:

  serialize(snapshot, options) {
    let result = this._super(...arguments);
    let props = snapshot.hasMany('props');
    if(props) {
      result.props = props.map(prop => ({
        id: prop.id,
        type: prop.modelName,
      }));
    }
    return result;
  },

Loading using pushPayload does work automatically. I'm using the JSON serializer.

anlumo avatar Jul 22 '18 19:07 anlumo

We're working on some big improvements to polymorphism that were unlocked by the relationship-refactor work and the identifiers work. The intent is to RFC an official polymorphism feature (polymorphism has mostly been undocumented because it never was fully designed) and we'll be sure to add a polymorphic guide at that time.

Until then, I'll see if we can get some small docs updates done to illuminate how the current setup works.

runspired avatar May 22 '21 02:05 runspired

Explicit relationship polymorphism is now in place, polymorphic find was described by the identifiers RFC (though showing a basic config in the guides would be nice too). Polymorphic find is also covered by the RequestManager, since it is agnostic to type.

Broadly speaking, most things in EmberData are agnostic to type at this point, most polymorphic cases should "just work" excepting those in which you refer to the same resource by multiple identities, in which case you will need to configure the IdentifiersCache to be able to know that these identities refer to the same resource.

Closing in favor of the documentation quest in #8394

runspired avatar Apr 08 '23 07:04 runspired