-
-
Notifications
You must be signed in to change notification settings - Fork 438
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
updateOrCreate
functionality
#2214
Comments
Sorry to reopen but I think you may have overlooked how this works. It works differently to Using the directive, you specify to columns to run the createProposal(input: ProposalCreateInput! @spread): Proposal @updateOrCreate(columns: ["candidate_id", "client_id"] canRestore: true) mutation CreateProposal {
createProposal(input: {
description: "test"
candidate: {
connect: 1
}
client: {
connect: 1
}
}) {
id
}
} |
I see how your proposal is mechanically distinct from the existing directives, thank you for elaborating and providing an example. Given the semantic similarity to createProposal(input: ProposalCreateInput! @spread): Proposal!
@upsert(identifyingColumns: ["candidate_id", "client_id"]) The |
Any chance you could provide some pointers on how to go about adding directive arguments? I've done a hacky version of upserting by custom fields on a fork to achieve the this functionality, but it would be great to get this into the main repo. Didn't manage to figure out directive args last time I tried. Happy to give it another stab though. |
@spawnia Thanks for the pointers. I've made a brief start on this: #2426 I've managed to get a very basic version of the identifyingColumns functionality working for single-level inputs only. I haven't tackled nested relationships yet or more complex upsert logic, as I think this will need more thought. @eNzyOfficial looks to have relationships working in his implementation, but I was hesitant to copy that into upsert as I suspected the SaveModel and ArgPartitioner classes may already encapsulate some of that logic. Revisiting my own use case for this, I found that I needed more control for upsert queries, e.g. multiple conditional where, whereHas and orWhereHas clauses. I'll have a think about how best this could be modelled. If anyone else has a good idea feel free to chip in :) It may be the case that custom directives are the best option for such complex mutations. My real world use caseWe have an API where users can create Quotes, Quotes have Locations. A Location must belong to a Postcode, can belong to an Address and a CLI (phone number). Users can create Locations with these relationships via a mutation: input QuoteRequestLineInput {
...
quoteRequestLocationBEnd: QuoteRequestLocationBEndBelongsTo!
}
input QuoteRequestLocationBEndBelongsTo {
upsert: QuoteRequestLocationInput
}
input QuoteRequestLocationInput {
postcode: PostcodeBelongsTo!
cli: CliBelongsTo
address: AddressBelongsTo
} When a user submits a Location upsert mutation, we would like to update any existing Locations that have the same Postcode and either the same Address or the same CLI if provided. Users don't know the IDs of Addresses or CLIs, so we try to match details using a base query that we crudely resolve from the UpsertModel: QuoteRequestLocation::whereHas(
'postcode',
static fn ($q) => $q->where('postcode', $postcode)
)
->when(
$cli || $address,
static fn ($q) => $q->where(
static fn ($q) => $q->whereHas(
'cli',
static fn ($q) => $q->where('cli', $cli)
)
->orWhereHas(
'address',
static fn ($q) => $q->where([
['thoroughfare', '=', Arr::get($address, 'thoroughfare')],
['building_number', '=', Arr::get($address, 'building_number')],
['sub_building_number', '=', Arr::get($address, 'sub_building_number', 0)],
])
)
)
); |
Not sure if this is useful for core lighthouse but I can create a PR if it's something of interest
What problem does this feature proposal attempt to solve?
Similar to the eloquent method
updateOrCreate
Which possible solutions should be considered?
This is what I currently have in our project, it works in the same way as updateOrCreate with the added feature of being able to restore softDeleted models as well
Usecase for the softdeleted stuff:
We have a proposal which is basically an intermediary table for client/candidate that also uses softdeletes (so it can be restored and we can keep all previous activity around that proposal should we need to look at it) this is restrained to
unique([client_id,candidate_id])
so trying to create another row with the same will throw an error, rather than checking if a row exists, and if its deleted, then changing it across multiple api calls, it's easier just to do it through a single directive.Open to alternative solutions, I kinda threw this together via the other directives/arguments for
@create
and@update
UpdateOrCreateDirective.php (the actual directive)
UpdateOrCreateModel.php (ArgResolver)
The text was updated successfully, but these errors were encountered: