Search code examples
ember.jsember-datajson-api

How to serialize relationships into JSON-API without relationship id's?


The API that I am connecting to isn't using JSON-API for its response format, so I am having to serialize the responses for my Ember app.

The problem I am having is that my response from the API looks like this:

{
    @type: "application/x.app-name.nurse-collection+json",
    @type.item: "application/x.app-name.nurse+json",
    @type.collection: "application/x.app-name.nurse-collection+json",
    @href: "http://192.168.33.10:3000/api/nurses{?includePatients}",
    @item: "http://192.168.33.10:3000/api/nurses/{id}",
    items: [
    {
        @type: "application/x.app-name.nurse+json",
        @type.item: "application/x.app-name.nurse+json",
        @type.collection: "application/x.app-name.nurse-collection+json",
        @href: "http://192.168.33.10:3000/api/nurses/1{?includePatients}",
        id: 1,
        Nurse: "Maria Holmes",
        NurseTelephoneNo: "123 4567",
        NurseMobileNo: "0123345566",
        Email: "someone@example.com",
        Patients: {
            @type: "application/x.app-name.patient-collection+json",
            @href: "http://192.168.33.10:3000/api/nurses/1/patients{?name,gender,includeNurse,includeEvents,includeReferrals,includeReviews,includePanels,includeRetrospectives,includePhbs,includeDisputeStage2s,includeDisputeResolutionPanels,includeEthnicity,includeFirstLanguage,includeMaritalStatus,includeReligion,includeSecurityLevel,includeSexualOrientation,includeWhichReview}",
            @rel: "http://192.168.33.10:3000/api/nurses/1/patients{?name,gender,includeNurse,includeEvents,includeReferrals,includeReviews,includePanels,includeRetrospectives,includePhbs,includeDisputeStage2s,includeDisputeResolutionPanels,includeEthnicity,includeFirstLanguage,includeMaritalStatus,includeReligion,includeSecurityLevel,includeSexualOrientation,includeWhichReview}"
            }
        },
        {
        @type: "application/x.app-name.nurse+json",
        @type.item: "application/x.app-name.nurse+json",
        @type.collection: "application/x.app-name.nurse-collection+json",
        @href: "http://192.168.33.10:3000/api/nurses/2{?includePatients}",
        id: 2,
        Nurse: "Julie Smart",
        NurseTelephoneNo: "543 1234",
        NurseMobileNo: null,
        Email: "someone@example.com",
        Patients: {
            @type: "application/x.app-name.patient-collection+json",
            @href: "http://192.168.33.10:3000/api/nurses/2/patients{?name,gender,includeNurse,includeEvents,includeReferrals,includeReviews,includePanels,includeRetrospectives,includePhbs,includeDisputeStage2s,includeDisputeResolutionPanels,includeEthnicity,includeFirstLanguage,includeMaritalStatus,includeReligion,includeSecurityLevel,includeSexualOrientation,includeWhichReview}",
            @rel: "http://192.168.33.10:3000/api/nurses/2/patients{?name,gender,includeNurse,includeEvents,includeReferrals,includeReviews,includePanels,includeRetrospectives,includePhbs,includeDisputeStage2s,includeDisputeResolutionPanels,includeEthnicity,includeFirstLanguage,includeMaritalStatus,includeReligion,includeSecurityLevel,includeSexualOrientation,includeWhichReview}"
        }
    }, 

    ...

    ]
}

So in this example, patients could be a collection, but I don't have the id's for the hasMany relationship until I have gone to that endpoint.

I'm wondering if there is a way to serialize this in a way that is JSON-API compliant, but will let me load relationship data asynchronously, once my app has decided to go to the patients endpoint.


Solution

  • I would write a custom serializer that fits this response type instead of converting it to the JSON-API spec.

    It doesn't look like there's any information here about the patient records (ie a list of ids), so ignore it here unless you want to set the link attribute for the hasMany.

    Loading relationship data asynchronously will work fine with ember-data. When you get an array of patient records and add them to the store they will be available with nurse.get('patients') as long as the loaded records each have a nurse_id.