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

postgraphile-node

v1.0.0

Published

Query Postgraphile in Node without SQL or network requests

Downloads

10

Readme

postgraphile-node

Query Postgraphile from the same Node app.

Usage

import { PostgraphileClient } from 'postgraphile-node';
import postgraphile from 'postgraphile';
import { gql } from 'graphql-request'

const middleware = postgraphile('postgres://user:pass@your-host/db-name');
const client = new PostgraphileClient(middleware);

const query = gql`
  {
    Movie(title: "Inception") {
      releaseDate
      actors {
        name
      }
    }
  }
`;

client.request(query).then((data) => console.log(data));

What it does

This library provides PostgraphileClient. It's a drop-in replacement for the GraphQLClient class in graphql-request that allows you to send GQL queries directly to Postgraphile without any network requests.

Why would anyone want to do this

Postgraphile is great for auto-generating a public GraphQL API, but there isn't a good way to access the database internally from the same Node server. You need to do this often when customizing the API, for example with custom plugins.

Postgraphile provides its internal Postgres client to custom plugins, but accessing the database this way requires writing and maintaining blocks of SQL, which is best avoided.

You could make HTTP requests to Postgraphile's /graphql endpoint, but that would have the app make HTTP requests to itself. It can work, but it's a bit hacky.

Another option is to add another database connector like TypeORM, Sequelize, or Prisma to access the database, but then you are having to adopt yet another technology in addition to Postgraphile, which isn't DRY.

Detecting whether a query came from PostgraphileClient

PostgraphileClient adds a boolean property isInternalQuery to the mocked req object that can be used to detect whether the query came from this library.

The following example shows how to use isInternalQuery with graphql-shield to break the infinite loop caused by querying Postgraphile from its own schema:

import postgraphile from 'postgraphile';
import { rule } from 'graphql-shield';

const middleware = postgraphile('postgres://user:pass@your-host/db-name', {
  async additionalGraphQLContextFromRequest(req: any) {
    return {
      isInternalQuery: req.isInternalQuery || false
    };
  }
});

const yourRule = rule({ cache: 'no_cache' })(
  async (parent, args, ctx, info) => {
    // ctx.isInternalQuery is true here if the query was from PostgraphileClient.
  }
);

This approach can be used anywhere the GraphQL context object is available, such as in Postgraphile plugins.