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

Any recommended workaround for embedding resources in Create/Update requests #775

Open
ianpetzer opened this issue Jul 22, 2016 · 10 comments

Comments

@ianpetzer
Copy link

Hi,

I've read up a lot of information about how 'embedded' / 'nested' resources are scheduled for v1.1 of the json-api but I was just wondering if anyone using jsonapi-resources has an interim solution, for de-serializing create/update requests where I can create / update a set of related entities. In the case of the create they wouldn't have client generated ids. I'm just hoping to create them as Rails nested resources.

Is there possibly a workaround for json-api resources similar to what was created for Active Model Serializers here: json-api/json-api#795 (comment)

I'm just trying to create a a few related entries in an atomic request.

For record fetching, I use sideloading.

I'm using Ember-data with the EmbeddedRecordsMixin to include the embedded relationships

Thanks for any suggestions

@aldhsu
Copy link
Contributor

aldhsu commented Jul 22, 2016

Have you read this article?

@lgebhardt
Copy link
Member

This will be resolved when JSON API is extended with Operations. Over the next few weeks I'm hoping to put some time into testing this in JR as a step in writing the update to the spec.

@ianpetzer
Copy link
Author

ianpetzer commented Jul 25, 2016

Thanks @aldhsu . That is a great article, and lovely solution for the client side. Does this work for you with json-api-resources.

I keep getting an error message:

title: "Invalid Links Object"
code: "115"
detail:" Data is not a valid Links Object.

@aldhsu
Copy link
Contributor

aldhsu commented Jul 26, 2016

@ianpetzer I haven't had to implement saving embedded records yet. Just something on my horizon. You may need to handle the request manually using the processor hooks.

@enzonotario
Copy link

Hi @ianpetzer , can you implement that? I'm new in ruby rails world and I don't understand how to use the "processor hook"..

@LarryMckuydee
Copy link

@ianpetzer, have you manage to overcome this issue? I keep getting the same error message like you:

title: "Invalid Links Object"
code: "115"

I follow http://jsonapi.org/format/#crud-creating

@ianpetzer
Copy link
Author

ianpetzer commented Mar 31, 2017 via email

@freysie
Copy link

freysie commented May 26, 2017

Thanks for bringing JSONAPI Suite to my attention, @ianpetzer.

I, too, would absolutely love for JR to support embedded resources one way or another, even though it’s not (yet‽) part of the jsonapi.org spec.

The way JSONAPI Suite deals with temporary client IDs seems pretty cool.

I wish someone smarter and faster than me would whip together a fork of JR which does this.

@martinverdejo
Copy link

I managed to create a record with nested association records following the docs section on flattening relationships.

So instead of writing my request this way:

{
  "data": {
    "type": "redemptions",
    "relationships": {
      "items": 
        "data": [
          { "type": "items", "attributes": { "offer_id": "1", "quantity": "7" } },
          { "type": "items", "attributes": { "offer_id": "3", "quantity": "3" } }
        ]
    }
  }
}

I sent the relationship data as part of the main object's attributes:

{
  "data": {
    "type": "redemptions",
    "attributes": {
      "items": [
        { "offer_id": "1", "quantity": "7" }
        { "offer_id": "3", "quantity": "3" }
      ]
    }
  }
}

And I defined my resource object getter and setter:

class Platform::Api::RedemptionResource < JSONAPI::Resource

  attributes :items

  model_name 'Platform::Redemption'

  has_many :items, class_name: 'RedemptionItem'

  def items
    @model.items.pluck(:id)
  end

  def items=(params)
    @model.items.destroy_all
    params.each do |param|
      @model.items.build(offer_id: param[:offer_id], quantity: param[:quantity])
    end
  end

end

I have a feeling there will be a better solution in the future depending on how the conversation goes, but this is my working solution for now.

@derekcannon
Copy link

Thanks @martinverdejo, this example was very helpful. I hope to see this feature included soon. There is an example of this in the official JSON API docs here http://jsonapi.org/format/#crud-creating

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

No branches or pull requests

8 participants