Giter VIP home page Giter VIP logo

Comments (6)

tiagopog avatar tiagopog commented on May 30, 2024

@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.

chrisdpeters avatar chrisdpeters commented on May 30, 2024

@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.

tiagopog avatar tiagopog commented on May 30, 2024

@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.

chrisdpeters avatar chrisdpeters commented on May 30, 2024

@tiagopog 👍 Opened a new issue: #36.

from jsonapi-utils.

adambedford avatar adambedford commented on May 30, 2024

@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.

tiagopog avatar tiagopog commented on May 30, 2024

@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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.