Comments (6)
@mecampbellsoup it's not possible because when you are using the jsonapi_(render|format|serialize)
method you don't actually need to pass any additional argument in order to get included the related resources or make things like sort, filter and fields work. JU + JR automatically validate and interpret your request params to generate the response you're expecting. It's all about passing the query string params – such as include
– and then you get the JSON with the included resources.
from jsonapi-utils.
@tiagopog This comment pulls a relevant quote from the JSON API spec:
Inclusion of Related Resources
An endpoint MAY return resources related to the primary data by default.
An endpoint MAY also support an include request parameter to allow the client to customize which related resources should be returned.
If an endpoint does not support the include parameter, it must respond with 400 Bad Request to any requests that include it.
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.
I really like the gist of what this other comment is suggesting.
When I try something like this, it doesn't include the related record though:
params[:include] ||= 'draft'
jsonapi_render json: page
Is this something that could be worked into JSONAPI::Utils?
from jsonapi-utils.
@chrisdpeters up to now JU does not support a default value for the include query string param, but I'm willing to do such implementation for the v0.5.0 milestone. We could open a new thread and start a further discussion about the feature's details. What do you think?
from jsonapi-utils.
@tiagopog 👍 Opened a new issue: #36.
from jsonapi-utils.
@tiagopog Are you saying that includes should "just work" then? i.e. if I have jsonapi_render json: @resource
and there are include directives in the params
, it'll return them?
from jsonapi-utils.
@adambedford yes, this should work properly.
So if you want a relationship to be included by default for a given controller action you could call a before_action
callback on that action merging then the required default related resource to params[:included]
.
Try something like that:
before_action :include_default_relationships, on: :index
def index
jsonapi_render json: User.all
end
private
# You can take one of the approaches:
# 1. Inclusive:
def include_default_relationships
included = params[:included].to_s.split(',')
params[:included] = (included | %w(author posts)).join(',')
end
# 2. Exclusive:
def include_default_relationships
params[:included] ||= 'author,posts'
end
from jsonapi-utils.
Related Issues (20)
- Trying to override pagination...
- Decimals formatted as strings in json response
- Update jsonapi-resources 0.9.12 HOT 10
- Bump the rails version to 6.1 for tagged version 7.13.0 HOT 1
- Release a new version? HOT 1
- Support for operation processors? HOT 3
- Rendering scopes with `includes` is slow for no reason HOT 2
- Passing resource relationship in POST request HOT 1
- resource_params => NoMethodError: undefined method `keys' for nil:NilClass HOT 2
- Render multiple invalid AR objects
- Bring support to fast_jsonapi HOT 9
- Use the cache with jsonapi_render. HOT 1
- Getting Invalid resource for custom resource response on patch request
- Non-REST actions?
- JSONAPI::Utils::Exceptions::BadRequest undefined
- Relax jsonapi-resources dependency HOT 3
- Support for sorting and pagination from JR
- jsonapi/utils.rb:9:in `include': wrong number of arguments (given 0, expected 1+) (ArgumentError)
- Formatting of error's "detail" attribute
- Accept and Content-Type headers are not verified
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from jsonapi-utils.