Jsonapi-resources: Include relationships by default

Created on 10 Jun 2015  路  4Comments  路  Source: cerebris/jsonapi-resources

Is there any way to include relationships by default by specifying them from the controller?

Thanks!

Most helpful comment

@lgebhardt @barelyknown
Referencing #389, I'd like to add a feature that provides a straightforward way to do this that does involve overriding JR methods. I think it's a popular use case and it would be nice for users to have a clean interface that doesn't involve reading and understanding the implications of overriding the source code.

I'm wondering if you have thoughts about whether this would be better to do at the controller or the resource level. As per #389, it would be straightforward to provide a method that could be called from any controller action. This would give easy granular control over which actions use default includes. It could alternatively be a macro class method in the controller that takes a block that lets the user configure the override based on the action.

It seems so far that you've preference adding as much custom logic to the resources vs the controllers and I think that could be an option as well. It seems easy enough for the request to check for include overrides in the resource during its initialization. Either way, I think that the include params coming from the client should be ignored for any action with an override, providing a configuration option for returning a 400 vs just logging and swallowing them. Thoughts?

All 4 comments

We don't currently have an official way to include some related resources by default. You could override the setup_request method on your controller and insert a new IncludeDirective. You could also create a before_filter that runs before the setup_request and initialize the include parameter if it isn't provided.

@lgebhardt @barelyknown
Referencing #389, I'd like to add a feature that provides a straightforward way to do this that does involve overriding JR methods. I think it's a popular use case and it would be nice for users to have a clean interface that doesn't involve reading and understanding the implications of overriding the source code.

I'm wondering if you have thoughts about whether this would be better to do at the controller or the resource level. As per #389, it would be straightforward to provide a method that could be called from any controller action. This would give easy granular control over which actions use default includes. It could alternatively be a macro class method in the controller that takes a block that lets the user configure the override based on the action.

It seems so far that you've preference adding as much custom logic to the resources vs the controllers and I think that could be an option as well. It seems easy enough for the request to check for include overrides in the resource during its initialization. Either way, I think that the include params coming from the client should be ignored for any action with an override, providing a configuration option for returning a 400 vs just logging and swallowing them. Thoughts?

:+1: for the feature

to me the controller level provides the most flexibility, but the resource level would suffice as well.

:+1:

After writing the below, I saw this: https://github.com/cerebris/jsonapi-resources/issues/389#issuecomment-176303658 which seems an easy and sufficient approach.

@mmartinson re last para - I think it should be the other way around. The 'override' should include related default resources UNLESS the client has specified 'include', in which case the 'override' defaults should be ignored. This gives maximum ease without losing flexibility, and is in line with:

http://jsonapi.org/format/#fetching-includes "If an endpoint supports the include parameter and a client supplies it, the server MUST NOT include unrequested resource objects in the included section of the compound document."

Then just have an :include_by_default resource option

Was this page helpful?
0 / 5 - 0 ratings