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

Using PATCH for update / edit #370

Closed
Antarian opened this issue Feb 26, 2021 · 6 comments
Closed

Using PATCH for update / edit #370

Antarian opened this issue Feb 26, 2021 · 6 comments

Comments

@Antarian
Copy link

Description
In this line in code

method: 'PUT',

is forced PUT for update operation. s it posible to somehow detect if schema is using put or patch? JsonLD is displaying this information.

@alanpoulain
Copy link
Member

React admin always sends all the data, this behavior would need to be changed beforehand for this feature to be useful.

@Antarian
Copy link
Author

yes, but specific PATCH format (merge-patch+json) may send all the data. And I think I found something about this partial PATCH in React Admin issues. Will send link here in a moment.
Maybe some kind of manual config here would be useful, but that is sounding kind of like a hack. As ApiPlatform is creating new child entity when PUT is used and overriding same entity when PATCH is used. Which is my required and expected behaviour for EDIT.
So far I retreat to hack/decorate serializer/normalizer to alter this behaviour of PUT in ApiPlatform when id is sent. If I will be successful.

@Antarian
Copy link
Author

Antarian commented Feb 26, 2021

Probably more relevant than anything below is this piece of code from React Admin repo:
https://github.com/marmelab/react-admin/blob/master/packages/ra-core/src/actions/dataActions/crudUpdate.ts
which is using "previousData".

marmelab/react-admin#256

and also this one, but not sure how much is it related:
josx/ra-data-feathers#20
with this PR
https://github.com/josx/ra-data-feathers/pull/49/files

But this may be more relevant:
marmelab/react-admin#338
with related PR
https://github.com/marmelab/react-admin/pull/351/files

I am hopefully not spamming this, will go through internal code and try to understand how this works.

@vaheeds
Copy link

vaheeds commented Apr 28, 2021

I am using react-admin as the front-end. In my case, the backend API uses some sort of validation and does not allow _id to be modified. So it cannot accept PUT requests and throws an error when the _id field is presented in the PATCH request. So I have to use PATCH and I want to send modified fields only.
How can I overcome this issue?

@vaheeds
Copy link

vaheeds commented May 4, 2021

I figured it out in a custom data provider:

  update: (resource, params) =>
    httpClient(`${apiUrl}/${resource}/${params.id}`, {
      method: 'PATCH',
      body: JSON.stringify(diff(params.data, params.previousData)),
    }).then(({ json }) => ({ data: json })),

where the diff function is:

const diff = (object, base) => {
  return transform(object, (result, value, key) => {
    if (!isEqual(value, base[key])) {
      result[key] =
        isObject(value) && isObject(base[key]) ? diff(value, base[key]) : value;
    }
  });
};

now, only the altered fielt sent in request body with a PATCH method.

@vincentchalamon
Copy link
Contributor

Fixed by #589, available in v4.0.3

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

No branches or pull requests

4 participants