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

Dinamically avoid certain fields in certain JSON-API response #47

Closed
officer-rosmarino opened this issue Feb 23, 2018 · 4 comments
Closed
Labels

Comments

@officer-rosmarino
Copy link

When I return a response that contains no attributes, but only the id values of the data,the attributes field still appears even if it is blank.

Basically the same thing applies to relationships. If I have defined relationship in the registered model it seems there is no way for me to send a response without the data. The only thing I can do is setting that link to null by responding null in the functions responsible to handle links.

GET example.com/v1/bands/1/relationships/musicians

{
    "jsonapi": {
        "version": "1.0"
    },
    "links": {
        "self": "http://localhost:12345/v1/bands/1/relationships/musicians"
        "related": "http://localhost:12345/v1/bands/1/musicians"
    },
    "data": [
        {
            "type": "musician",
            "id": "1",
            "attributes": {} <-- I have selected only the id in the query to the db, but still get this
           "relationships": {
                "bands": null <-- would like to avoid the field relationship at all in this case
            },
        }
    ]
}

Is there a way for me to do this?

Cheers

@danivek
Copy link
Owner

danivek commented Feb 23, 2018

@honestserpent, I think that the best way to do this is to serialize bands resource first. Then respond with the value of musicians's relationship.

GET example.com/v1/bands/1/relationships/musicians

Serializer.serializeAsync(bandData).then((serializedBand) => { 
return serializedBand.data.relationships['musicians'];
})

@officer-rosmarino
Copy link
Author

Thank you! That is extremely smart and almost perfect!!
"Almost" because doing that returns something a little bit different than what I want. In particular, it lacks top level meta and the jsonapi and linka fields.

I guess I can do it by putting that data in another object that also contains that information, like:

let serialized_data = await JSONAPISerializer.serializeAsync(bandData);
res.send(_.merge({ "jsonapi":{"version": "1.0"}}, serialized_data);

What do you think about something like this?

@danivek
Copy link
Owner

danivek commented Feb 23, 2018

for meta and links you can define such these options on the serializer options on relationships (see doc). So you will have them in response. But you're right about jsonapi.

@officer-rosmarino
Copy link
Author

thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants