Search code examples
postgraphile

Configure postgraphile to support succinct nested mutations


I have come to postgraphile yesterday from a couple of years with Hasura. I was stunned at what PG does out of the box. My main sense of loss is around the GQL api that PG offers, relative to Hasura. If I have Customer with a foreign key to an Address, and I add the nested mutations plugin, I can say this:

mutation MyMutation {
  createCustomer(
    input: {customer: {addressToAddressId: {create: {line1: "5", line2: "The Street"}}, firstName: "Breda", lastName: "Smith"}}
  ) {
    address {
      id
    }
    customer {
      id
    }
  }
}

But with Hasura I would be able to say something like :

mutation MyMutation {
  createCustomer(input:{firstName: "Breda", lastName: "Smith", address:{line1: "5", line2: "The Street"}}) {
    address {
      id
    }
    customer {
      id
    }
  }
}

How can I configure PG so it can do this kind of mutation?


Solution

  • I asked on the postgraphile discord channel, and Benjie replied:

    It’d be possible to write a plugin to do this, but I’m not a big fan of CRUD mutations in general. I’d do it this way:

    The first way to consider adding a mutation to your GraphQL schema is with a Custom Mutations. These create individual highly specialised mutations from the VOLATILE functions in your PostgreSQL database. There's a checklist of requirements they must meet defined in their documentation .

    If this doesn't suit your needs, you may also using makeExtendSchemaPlugin or have a look through the community pluginsto see if one suits your needs. Relevant reading on GraphQL mutation design