0

In Laravel Lighthouse GraphQL, I'd love to be able to delete records that match certain conditions rather than passing just an individual ID.

I get this error:

The @delete directive requires the field deletePostTag to only contain a single argument.

This functionality seems currently unsupported, but if I'm wrong and this is actually supported, please let me know, because this would be the most straightforward approach.

So then my second approach was to try to first run an @find query to retrieve the ID of the record that I want to delete (based on certain fields equaling certain values).

But https://lighthouse-php.com/4.16/api-reference/directives.html#find shows:

type Query {
  userById(id: ID! @eq): User @find
}

and does not show how I could provide (instead of the primary key ID) 2 arguments: a foreign key ID, and a string.

How can I most simply accomplish my goal of deleting records that match certain conditions (rather than deleting via primary key)?

Ryan
  • 22,332
  • 31
  • 176
  • 357

1 Answers1

1

I'm not sure about the @delete functionality regarding multiple arguments, but from what you've posted that appears to be unsupported at the moment. Regarding your query, you should instead use something like @all in conjunction with @where which would allow you to filter the collection by as many vars/args as you'd like. If your argument list grows beyond 3 or so, I would take a look at Complex Where Conditions. They have worked very well for my team so far, and allow a lot of filtering flexibility.

Also take a look at the directive's docs stating:

You can also delete multiple models at once. Define a field that takes a list of IDs and returns a collection of the deleted models.

So if you return multiple models you'd like to delete from your query, you may use this approach to delete them all at once.

Dharman
  • 30,962
  • 25
  • 85
  • 135
  • Ahh, okay so `@find` is for looking up via primary key, and `@all` with `@where` let's me query via whatever fields I choose. I had a brain skip. Thanks. I bet you're on the right track for my secondary approach since my preferred approach seems unsupported. I'll probably accept this answer once I get back to that project in the coming days and ensure this works. Thanks. – Ryan Oct 03 '20 at 20:18
  • Although the `@all` with `@where` fetched the ID I needed, I could not figure out a good way to then use that in the next query. I'd need to call one Apollo GraphQL query after another, and I'd need it to return a promise, as people asked for in this abandoned issue: https://github.com/apollographql/react-apollo/issues/3499 Instead, I opted to find a way to populate my autocomplete dropdown with the IDs I needed in the first place. I wish there were an easier and cleaner way to do this. +1 anyway because I really appreciate your hints! – Ryan Oct 04 '20 at 00:46