Jsonapi-resources: creating singular resources

Created on 22 Apr 2016  路  10Comments  路  Source: cerebris/jsonapi-resources

aka a singleton resource
Help! There does not seem to be an example or documentation of this.

in the README.md, in the routes section ( jsonapi_resource ) it states:

Like jsonapi_resources, but for resources you lookup without an id.
So (to use an example of a ProfileResource ), i've added
jsonapi_resource :profiles
but doing a 'GET /profiles' I get a response body

{
"errors": [
{
"title": "Record not found",
"detail": "The record identified by could not be found.",
"id": null,
"href": null,
"code": 404,
"source": null,
"links": null,
"status": "404",
"meta": null
}
]
}

I want the Profile record to be be the profile of context current user. (which i have) I'm not sure how in the resource to set it up so the current_user id is the id without actually setting it.

perhaps by customizing base records for finder? but I not seeing it :(

Most helpful comment

I noticed that when I am using singular resources according to this guide, that the self relationship pointed to a non-existent plural version of the resource.

GET /configuration

{
  "data": {
    "id": "singleton-id",
    "type": "configurations",
    "links": {
      "self": "http://localhost:8081/configurations/singleton-id"
    }
  }
}

I'd expect the self link to be http://localhost:8081/configuration

All 10 comments

the existing demo apps do not demonstrate the using of the singular resource, and there doesn't seemed to be any documentation that (explicitly) address this.

You will need to override the find_by_key method on your resource. For example:

ProfileResource
  def self.find_by_key(key, options = {})
    context = options[:context]
    # create a new ProfileResource from the current user's profile
    self.new(context[:current_user].profile, context)
  end
end

I got it working. It took 3 things to align:

  1. config/routes.rb:

`jsonapi_resource :profile`

  1. in the base_controller.rb :

def context { user: current_user } end

as per the [docs on context (https://github.com/cerebris/jsonapi-resources#context)

  1. and then as @Igebhardt states overriding the find_by_key method on the resource:

def self.find_by_key(key, options = {}) context = options[:context] self.new(context[:current_user].profile, context) end

then the singleton calls ( GET /profile, POST /profile, PUT /profile, DELETE /profile ) will work ( the json api type will still be plural type: 'profiles', )

Hope this helps the next person.... :)

@iirving Great!

I've document this on wiki

@iirving Thanks. One thing that might be good to note is that singletons are not officially supported by the spec, so there may be clients that are not compatible with this approach.

It seemed they were supported at the beginning of the year(?) when i started building my api, but are now (more) explicitly not supported :(

I believe they were allowed in an early draft of the JSONAPI spec, but did not make it to the final. I just recently removed my interfaces that were still relying on Singletons. I hadn't focused on the fact that they were implicitly not allowed until @senid231 's comment in #705. Until then I was also under the assumption it was allowed, and had planned to add support into JR to better handle singletons.

yea it is only with the @senid231 comments on the json-api repo https://github.com/json-api/json-api/issues/1046 that this came to light for me (and the lack of support in the Ember Data jsonapi adapter for singletons - I was able to get around that wilth an simple hack but after much gnashing of teeth )

I noticed that when I am using singular resources according to this guide, that the self relationship pointed to a non-existent plural version of the resource.

GET /configuration

{
  "data": {
    "id": "singleton-id",
    "type": "configurations",
    "links": {
      "self": "http://localhost:8081/configurations/singleton-id"
    }
  }
}

I'd expect the self link to be http://localhost:8081/configuration

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lgebhardt picture lgebhardt  路  5Comments

evolve2k picture evolve2k  路  3Comments

balinterdi picture balinterdi  路  5Comments

d3crypt3d picture d3crypt3d  路  3Comments

blmundie picture blmundie  路  4Comments