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

Missing Relationship Function: DELETE #254

Open
thedanmwangi opened this issue Dec 5, 2023 · 2 comments
Open

Missing Relationship Function: DELETE #254

thedanmwangi opened this issue Dec 5, 2023 · 2 comments

Comments

@thedanmwangi
Copy link

Hello, great work on this package!

However, I have encountered an error when deleting a relationship using the code:
jsonApi.one('author', 1).relationships('articles').delete([{ id: 1 }])

The error I am getting is:
Uncaught TypeError: schema_schema__WEBPACK_IMPORTED_MODULE_12. jsonApi.one(...).relationships(...).delete is not a function_

Could this be an internal issue with the package?

I am on the latest version of Devour-Client.

@tijn
Copy link
Collaborator

tijn commented Feb 6, 2024

You're right. In our own apps we're using a patch for this that probably should become part of the official devour-client, but only after improving.

Our code looks like this:

import { get, last, map } from "lodash"

export function destroyRelationships(relationshipName, ids, params = {}) {
  const lastRequest = last(this.builderStack)
  const modelName = get(lastRequest, "model")
  if (!modelName) {
    throw new Error("destroyRelationships must be called with a preceding model.")
  }
  const relationship = this.relationshipFor(modelName, relationshipName)

  this.builderStack.push({ path: "relationships" })
  this.builderStack.push({ path: relationship.path, model: relationship.type })

  const data = map(ids, (id) => {
    return { type: relationship.type, id: id }
  })

  const req = {
    method: "DELETE",
    url: this.urlFor(),
    model: modelName,
    params: params,
    data: data,
  }

  if (this.resetBuilderOnCall) {
    this.resetBuilder()
  }

  return this.runMiddleware(req)
}

The thing with this code is that you cannot use it like you described (which would be totally logical to expect):

jsonApi.one('author', 1).relationships('articles').delete([{ id: 1 }])

You need to do something like this:

jsonApi.one('author', 1).destroyRelationships('articles', [1])

So, you see, it works but it should be brought in line with the rest of the methods (in my opinion).

@thedanmwangi
Copy link
Author

That's a great approach. I ended up using an arbitrary request to the relationship endpoint:

jsonApi.request('https://my-api-endpoint/resource/relationships/relation-type', 'DELETE', {}, my-id-payload)

Works like a charm!

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

2 participants