npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@priotas/postgraphile-plugin-nested-mutations

v1.0.2

Published

Nested mutations plugin for PostGraphile

Downloads

7

Readme

@priotas/postgraphile-plugin-nested-mutations

This plugin implements nested mutations based on both forward and reverse foreign key relationships in PostGraphile v4. Nested mutations can be of infinite depth.

Installation

yarn add @priotas/postgraphile-plugin-nested-mutations

Getting Started

CLI

postgraphile --append-plugins @priotas/postgraphile-plugin-nested-mutations

See here for more information about loading plugins with PostGraphile.

Library

const express = require('express');
const { postgraphile } = require('postgraphile');
const PostGraphileNestedMutations = require('@priotas/postgraphile-plugin-nested-mutations');

const app = express();

app.use(
  postgraphile(pgConfig, schema, {
    appendPlugins: [
      PostGraphileNestedMutations,
    ],
  })
);

app.listen(5000);

Plugin Options

When using PostGraphile as a library, the following plugin options can be passed via graphileBuildOptions:

Use simple field names for nested mutations. Instead of names suffixed with tableBy<Key> and tableUsing<Key>, tables with a single foreign key relationship between them will have their nested relation fields named table. Defaults to false.

postgraphile(pgConfig, schema, {
  graphileBuildOptions: {
    nestedMutationsSimpleFieldNames: true,
  }
});

Controls whether the deleteOthers field is available on nested mutations. Defaults to true.

postgraphile(pgConfig, schema, {
  graphileBuildOptions: {
    nestedMutationsDeleteOthers: false,
  }
});

If enabled, plural names for one-to-one relations will be used. For backwards compatibility. Defaults to false.

postgraphile(pgConfig, schema, {
  graphileBuildOptions: {
    nestedMutationsOldUniqueFields: false,
  }
});

Usage

This plugin creates an additional field on each GraphQL Input type for every forward and reverse foreign key relationship on a table, with the same name as the foreign table.

Each nested mutation field will have the following fields. They will accept an array if the relationship is a one-to-many relationship, or a single input if they are one-to-one.

Connect to Existing Record

connectByNodeId

Connect using a nodeId from the nested table.

connectBy<K>

Connect using any readable primary key or unique constraint on the nested table.

Creating New Records

create

Create a new record in the nested table.

Delete existing Record

deleteByNodeId

Delete using a nodeId from the nested table.

deleteBy<K>

Delete using any readable primary key or unique constraint on the nested table.

Updating Records

updateByNodeId

Update a record using a nodeId from the nested table.

updatedBy<K>

Update a record using any readable primary key or unique constraint on the nested table.

Example

create table parent (
  id serial primary key,
  name text not null
);

create table child (
  id serial primary key,
  parent_id integer,
  name text not null,
  constraint child_parent_fkey foreign key (parent_id)
    references p.parent (id)
);

A nested mutation against this schema, using Parent as the base mutation would look like this:

mutation {
  createParent(input: {
    parent: {
      name: "Parent 1"
      childrenUsingId: {
        connectById: [{
          id: 1
        }]
        create: [{
          name: "Child 1"
        }, {
          name: "Child 2"
        }]
      }
    }
  }) {
    parent {
      id
      name
      childrenByParentId {
        nodes {
          id
          name
        }
      }
    }
  }
}

Or using Child as the base mutation:

mutation {
  createChild(input: {
    child: {
      name: "Child 1"
      parentToParentId: {
        create: {
          name: "Parent of Child 1"
        }
      }
    },
  }) {
    child {
      id
      name
      parentByParentId {
        id
        name
      }
    }
  }
}

Smart Comments

Smart comments are supported for renaming the nested mutation fields.

comment on constraint child_parent_fkey on child is
  E'@fieldName parent\n@foreignFieldName children';