postgraphile-plugin-batch-create-update-delete icon indicating copy to clipboard operation
postgraphile-plugin-batch-create-update-delete copied to clipboard

Get server-generated ID's of inserted rows

Open markhalonen opened this issue 3 years ago • 6 comments

Hey Tim, great looking package here, I especially like the opt-in smart comments design.

I have a query such as

mutation CreateManyPartsTransfers($partsTransfers: [PartsTransferInput!]!) {
    mnCreatePartsTransfer(input: { mnPartsTransfer: $partsTransfers }) {
      clientMutationId
      partsTransfer {
        id
      }
    }
  }

Where I am creating many PartsTransfers rows, and then I would like to get the server-generated ID's back. Currently, it seems to only return 1 id, instead of returning many id's, 1 id for each new row inserted.

Am I missing something or is this just not implemented? And if it's not implemented, how hard do you think it is to implement?

markhalonen avatar May 17 '22 20:05 markhalonen

I personally never implemented this b/c I would of had to build a custom input type to return multiple rows vs. the already written one from an internal postgraphile plugin I borrowed. You can just use a second query to get them that takes place after the mutation. There is a query field you can use on the returned values for this like as show below (just a guess at your table / query name is):

mutation CreateManyPartsTransfers($partsTransfers: [PartsTransferInput!]!) {
    mnCreatePartsTransfer(input: { mnPartsTransfer: $partsTransfers }) {
      clientMutationId
      query {
       allPartsTransfer {
        nodes {
          id
        }
      }
     }
    }
  }

If you want to update the plugin and add the proper type, I would be more than glad to accept a PR. At some point I may update this, just too busy with other stuff. If there was a sponsor I would consider further.

tjmoses avatar May 17 '22 21:05 tjmoses

Thanks for the quick response! Unfortunately

query {
       allPartsTransfer {
        nodes {
          id
        }
      }

returns all PartsTransfers, even ones created before this request...

If you want to update the plugin and add the proper type, I would be more than glad to accept a PR. At some point I may update this, just too busy with other stuff. If there was a sponsor I would consider further.

Fully understand. Can you email me [email protected] with an acceptable hourly rate to work on this task? Thanks!

markhalonen avatar May 17 '22 21:05 markhalonen

Right, but as long as the query is within the mutation you'll get the additional created ids then you could then diff the two sets to get the changed ids. Here's another library I created that somewhat automates that. I do need to update this feature though. If that workaround doesn't work, give me a few days and I'll reach out.

tjmoses avatar May 17 '22 22:05 tjmoses

Right, but as long as the query is within the mutation you'll get the additional created ids then you could then diff the two sets to get the changed ids

This is large table so getting all the ids is not performant, so this is not a solution

markhalonen avatar May 17 '22 23:05 markhalonen