-
-
Notifications
You must be signed in to change notification settings - Fork 572
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
best practice for creating multiple records with foreign key relations in a single mutation #793
Comments
What you're looking for is nested mutations (ref: #361) which we don't officially have yet; however there is a plugin from the community which implements it that I was told is working just this morning - I suggest you hop on to gitter and chat to @mlipscombe - I'm sure an offer to test the software would be welcome. Another option is to add a custom mutation via a plugin, the easiest way to achieve this is with the new From the SQL point of view you could write code in one of the more dynamic languages (plpython, plv8, etc) that constructs the column list dynamically. |
I've published a very alpha version of |
Thanks for the prompt reply @benjie and great work @mlipscombe 👍. I will evaluate the options, although I will probably have to go the route of the stored procedure outlined above since at least in some cases, I need to support more than a single level of nesting. |
@leoschweizer unlimited depth of nesting is high on my todo list -- I'd say in the next few days. Before writing this plugin, I tried using functions to achieve the same thing, but it's not practical, because a function with a table type argument will not "do the right thing" when columns have defaults, because there's no way of specifying "default value" for a column ... if it is omitted, it will be |
@mlipscombe that's good to know. I guess for the time being I have to enumerate all the columns explicitly then, including the default values... |
@leoschweizer the latest release I just made (1.0.0-alpha.5) of |
I'm submitting a ...
PostGraphile version: Library: 4.0.0
Suppose I have a relation of
Customer(id, name, email, ...)
andAddress(id, customerId, street, zip, city, ...)
. From the business logic perspective, I don't want a customer to exist without at least one address, so when creating a customer, I always want to create an address as well in an atomic way (if the address creation fails for some reason, the customer creation should fail as well).I suppose this use case is fairly common, however I couldn't find any recommendations on how to handle it.
The crucial point is probably that the
id
of the customer has to be known in order to create theAddress
. I can think of two options about handling that:Use client-side generated ids, which would allow to create a "compound mutation" with payloads for a
Customer
and anAddress
which can then simply be inserted on the server side. However I really dislike this option, since it feels like this opens a whole attack surface (or I need to introduce custom validation logic to ensure that ids match etc).Use a custom mutation like the following:
This is picked up nicely by the automatic schema generation of Postgraphile. However, what I dislike about this approach is that I have to re-enumerate all the fields of the
Address
table in the function. That means that I would have to update the stored procedure when the schema changes. Or is there a clever way to use everything from theaddress
input and add theid
of the newly generatedCustomer
to that input?Any advice on how to handle this use case cleanly would be appreciated!
(btw: this project is awesome 🥇)
The text was updated successfully, but these errors were encountered: