Creating record with sub-table

Say I have a schema with Parts, Orders, and OrderParts (let me know if there’s an easier way to do this, but I want every order to have multiple parts & a quantity for each).

Can I create an order with parts in a single mutation, or do I need to first create the order and then create rows in OrderParts referencing the created order?

Hi @samwoolertonLW!

Have you tried relations? This feature allows you to avoid creating tables like OrderParts.


Yeah @samwoolertonLW, @vorobeez is pointing you to the right docs! Good example would be:

 * The author record's bio is gets updated while 
 * a new Post is being created and associated.
mutation {
  authorUpdate(filter: {
    name: "Huxley"
  data: {
    bio: "Just a guy who loves possum.",
    posts: {
      create: [{
        title: "Can't stop the Possum",
        body: "Cause Possum is Awesome",
        publishingDate: "2019-09-22T03:45:33.432Z"
  }) {
    posts(last: 1) {
      items {

Here’s a video to walk you through it too:

1 Like

Yeah I’m using relations throughout, just I think this is a special case that needs a temp table too.
I’d be stoked if it didn’t though! Would simplify things a fair bit.

Given that an order can have many parts with a quantity for each, can you still do this with a relationship directly between Orders and Parts? I assumed that having the extra field meant that it would need that bridging table.
Note that OrderParts has order_id, part_id, quantity

That link & sample code are fantastic thanks! Being able to create/update/remove child records at the same time as the parent was exactly what I needed.

Still interested if the schema could be done without a temp table but otherwise good to go

I think there’s only one alternative. Create custom switch in OrderParts table with parts (Some kind of enum). But it depends on your context.

No need for temp table! That’s what we handle in the background :slight_smile:

Just make the Has Many relationship between orders and parts.