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

@graphile-contrib/pg-non-null

v1.0.3

Published

A PostGraphile plugin helping you handle non nullable fields.

Downloads

68

Readme

@graphile-contrib/pg-non-null

Plugins helping you handle fields which are non nullable. There are two plugins within this repository:

  • PgNonNullSmartCommentPlugin Allows the use of @nonNull smart comment to flag fields as non nullable in the resulting GraphQL schema.
  • PgNonNullRelationsPlugin This plugin makes sure that fields behind foreign keys which are flagged as NOT NULL are also non nullable in the resulting GraphQL schema.

⚠️ Please be wary and use with caution, as these plugins influence only the output types! By no means do any of the plugins guarantee that Postgres will follow your manual non null overrides! ⚠️

Installation:

npm install --save @graphile-contrib/pg-non-null

or

yarn add @graphile-contrib/pg-non-null

Usage:

CLI:

Default:

postgraphile --append-plugins @graphile-contrib/pg-non-null

Only PgNonNullSmartCommentPlugin:

postgraphile --append-plugins @graphile-contrib/pg-non-null/smart-comment

Only PgNonNullRelationsPlugin:

postgraphile --append-plugins @graphile-contrib/pg-non-null/relations

Library:

const PgNonNullPlugin = require('@graphile-contrib/pg-non-null');

const PgNonNullSmartCommentPlugin = require('@graphile-contrib/pg-non-null/smart-comment');
const PgNonNullRelationsPlugin = require('@graphile-contrib/pg-non-null/relations');

app.use(
  postgraphile(process.env.POSTGRES_ENDPOINT, process.env.POSTGRES_SCHEMA, {
    appendPlugins: [PgNonNullPlugin],
    // or
    appendPlugins: [PgNonNullSmartCommentPlugin, PgNonNullRelationsPlugin],
  }),
);
  • PgNonNullRelationsPlugin requires no further actions as it will automatically detect not null foreign keys and mark the links as non nullable.

  • PgNonNullSmartCommentPlugin uses the smart comment feature with the following syntax to manually infer non null states:

    • Commenting @nonNull [field] on a TABLE, VIEW or TYPE indicates that matching fields are not null.

      CREATE TABLE private.user (
        id         serial PRIMARY KEY,
        first_name text NOT NULL,
        last_name  text
      );
      COMMENT ON TABLE private.user IS '@nonNull last_name';
      CREATE OR VIEW public.user AS
        SELECT
          id,
          first_name,
          last_name
        FROM private.foo;
      COMMENT ON VIEW public.user IS E'@nonNull id\n@nonNull first_name\n@nonNull last_name';
      CREATE TYPE public.user AS (
        id         int,
        first_name text,
        last_name  text
      );
      COMMENT ON TYPE public.user IS $$
      @nonNull id
      @nonNull first_name
      @nonNull last_name
      $$;
    • Commenting @nonNull on a COLUMN or FUNCTION indicates that the field itself is not null.

      CREATE TABLE public.user (
        id         serial PRIMARY KEY,
        first_name text NOT NULL,
        last_name  text
      );
      COMMENT ON COLUMN public.user.last_name IS '@nonNull';
      CREATE FUNCTION public.user_full_name("user" public.user) RETURNS text AS
      $$
        SELECT "user".first_name || ' ' || "user".last_name
      $$
      LANGUAGE SQL STABLE;
      COMMENT ON FUNCTION public.user_full_name IS '@nonNull';

Example:

This SQL schema:

CREATE TABLE public.user (
  id         serial PRIMARY KEY,
  first_name text NOT NULL,
  last_name  text NOT NULL
);

CREATE TYPE public.article_state AS (
  is_public boolean,
  likes     int
);

COMMENT ON TYPE public.article_state IS $$
@nonNull is_public
@nonNull likes
$$;

CREATE TABLE public.article (
  id serial PRIMARY KEY,

  -- `userByAuthorId` link should not be null because the foreign key is not null
  author_id serial NOT NULL REFERENCES public.user(id),

  "state" public.article_state NOT NULL,

  title   text NOT NULL,
  content text
);

CREATE FUNCTION public.user_full_name("user" public.user) RETURNS text AS
$$
  SELECT "user".first_name || ' ' || "user".last_name
$$
LANGUAGE SQL STABLE;

COMMENT ON FUNCTION public.user_full_name IS '@nonNull';

will result in the following GraphQL schema:

type User {
  id: Int!
  firstName: String!
  lastName: String!

  fullName: String! # NonNull derived by the `PgNonNullSmartCommentPlugin`
}

type ArticleState {
  isPublic Boolean! # NonNull derived by the `PgNonNullSmartCommentPlugin`
  likes Int! # NonNull derived by the `PgNonNullSmartCommentPlugin`
}

type Article {
  id: Int!
  authorId: Int!
  state: ArticleState!
  title: String!
  content: String

  userByAuthorId: User! # NonNull derived by the `PgNonNullRelationsPlugin`
}