mrichar1 / jsonapi-vuex

Use a JSONAPI api with a Vuex store, with data restructuring/normalization.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Misunderstanding or ?

santerref opened this issue · comments

Hi,

Thank you for this wonderful package !

I have been using it for a while on some projects and there is something I was not sure if it was possible. Maybe it is specified in the documentation but I can manage to get this working.

I am wondering if it's possible to make a patch request without having to remember the previous "include" that was made?

Let's say I have a blog model with comments and tags and I have a button to unpublish de blog post.

My first request will be:

this.$store.dispatch('jv/get', ['blogs/1',{
    params: {
        include: 'comments,tags'
    }
}])

After, when I unpublish de blog, I do something like:

this.$store.dispatch('jv/patch', [{
    _jv: {
        type: 'blogs',
        id: '1'
    },
    published: false
}]);

But then, the comments and the tags disappear because I do not include the "include" param in my request.

Do I have to always keep in mind to not forget my "include" param on every patch request?

Thanks,
Francis.

I'm not sure what is causing this behaviour for you. The behaviour of the JSON spec is that it uses relationships if they are in the object, but if there is no relationship section at all then it won't remove them on the server.

In your above example, there is no _jv: { relationships: { ...}} key so this patch should have no effect on the server.

Equally if the object you are using in the patch is one from the store, then it should include the relationship section it had when it was fetched, so there should be no change.

It might help to set the cleanPatch: true config option when setting up the module - this will drop any keys in the patch that are also in the store. It is designed to stop out-of-date records in the store accidentally being 'added back' when a patch happens - but should also guarantee to remove all references to relationships etc.

Let us know if that helps?

Hi @mrichar1 !

Thanks for your quick reply.

I think my question was wrong.

When I send my PATCH request, I do not use the object (record) in the state, but only a raw JSON object: {_jv: {type: 'blogs', id: '1'}}.

I think this is my issue.

But it might be a great option (or not) to add !? Something that could find an existing record before sending the PATCH request to make sure relationships are kept !

Example: mergeExistingOnPatch: true.

The cleanPatch will help me because I only want to send what have been changed.

Francis.

Hi,

Creating a new object 'from scratch' shouldn't matter. If the object doesn't contain a relationships section (in _jv) then the patch won't modify any relationships. I've just tested this using the testapp provided with the library and it behaves as expected.

The store is empty, and the object I'm passing to the patch action is:

{
   _jv: {
     type: 'widget',
     id: '1',
   },
   color: 'red',
 }

(Different type/attribute, but the same effect).

Can you let me know what JSONAPI server you are running/connecting to? There is a possibility that the implementation doesn't follow the JSONAPI spec correctly.

Hi,

I am using the Laravel JSON API package: https://laravel-json-api.readthedocs.io/en/latest/

I will make more tests and see what response I get and if the relationships section is in _jv.

However, I do not say that the relationships are modified or removed from the server (database), I said that the relationships are removed from the Vuex store when the response of the PATCH request is parsed from your package.

All comments and tags are removed from the Vuex store.

Ahh - apologies - I thought you meant that the patch was removing relationships on the server-side. I now see what you mean!

With patch the server can return no data (204) or a representation of the resulting patched object (200).https://jsonapi.org/format/#crud-updating-responses-200

If it returns 204, then the code already does a 'merge' of the patch object onto the store, so relationships should be unaffected.

If it returns 200, then the data should contain the relationships section with the appropriate relationship identifier objects. Something like:

data: {
  type: 'blogs',
  id: '1'
  attributes: {
    published: false
  },
  relationships: {
    tags: {
      data; { type: 'tags', id: '10' }
    },
    comments: {
      data: { type: 'comments', id: '20' }
    }
}

Since the related items are already in the store (from the original get with include) these will just create a reference 'pointing' to these objects.

Can you set the config option preserveJson: true and then paste the contents of _jv/json for the get and patch operations?

Hello, @mrichar1

I am new guy for this package and I'd like to use this one for my project.
So I have one issue and want your help.

I've got the json api like this.
baseUrl : https://dev-54ta5gq-2mtjdsd3z4gog.de-2.platformsh.site/jsonapi
method: Get,
its response looks like following
`
{
"jsonapi": {
"version": "1.0",
"meta": {
"links": {
"self": {
"href": "http://jsonapi.org/format/1.0/"
}
}
}
},
"data": [],
"links": {
"action--action": {
"href": "https://dev-54ta5gq-2mtjdsd3z4gog.de-2.platformsh.site/jsonapi/action/action"
},
"api_key--api_key": {
"href": "https://dev-54ta5gq-2mtjdsd3z4gog.de-2.platformsh.site/jsonapi/api_key/api_key"
},
"base_field_override--base_field_override": {
"href": "https://dev-54ta5gq-2mtjdsd3z4gog.de-2.platformsh.site/jsonapi/base_field_override/base_field_override"
}
}
}

`

I want to get the data from the href url related to the specific links key.
But I don't know how to do this.
Can you give me advice?
Thanks.

commented

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.