4.13.0 • Published 17 days ago

postgraphile v4.13.0

Weekly downloads
17,654
License
MIT
Repository
github
Last release
17 days ago

PostGraphile

Package on npm MIT license Gitter chat room Donate Follow

This is the ALPHA VERSION of PostGraphile v4 (previously known as PostGraphQL); use with caution and monitor the releases.

A GraphQL schema created by reflection over a PostgreSQL schema.

Stable: v3 (v3 branch) - maintenance only, called “PostGraphQL”
Next: v4 (master branch) - renamed to PostGraphile; see graphile.org for docs


The strongly typed GraphQL data querying language is a revolutionary new way to interact with your server. Similar to how JSON very quickly overtook XML, GraphQL will likely overtake REST. Why? Because GraphQL allows us to express our data in the exact same way we think about it.

The PostgreSQL database is the self acclaimed “world’s most advanced open source database” and even after 20 years that still rings true. PostgreSQL is the most feature rich SQL database available and provides an excellent public reflection API giving its users a lot of power over their data. And despite being over 20 years old, the database still has frequent releases.

With PostGraphile, you can access the power of PostgreSQL through a well designed GraphQL server. PostGraphile uses PostgreSQL reflection APIs to automatically detect primary keys, relationships, types, comments, and more providing a GraphQL server that is highly intelligent about your data.

PostGraphile holds a fundamental belief that a well designed database schema should be all you need to serve well thought out APIs. PostgreSQL already has amazing user management and relationship infrastructure, why duplicate that logic in a custom API? PostGraphile is likely to provide a more performant and standards compliant GraphQL API then any created in house. Focus on your product and let PostGraphile manage how the data gets to the product.

For a critical evaluation of PostGraphile to determine if it fits in your tech stack, read evaluating PostGraphile for your project.

Introduction

Watch a talk by the original author Caleb at GraphQL Summit for a fast 7 minute introduction to using the PostGraphile project. This was using v2; we're now up to v4 which has many more bells and whistles!

PostGraphile at GraphQL Summit

Usage

First install using npm:

npm install -g postgraphile

…and then just run it! By default, PostGraphile will connect to your local database at postgres://localhost:5432 and introspect the public schema.

postgraphile

For information about how to change these defaults, just run:

postgraphile --help

You can also use PostGraphile as native HTTP, Connect, Express, or Koa (experimental) middleware. Just import postgraphile:

import { createServer } from "http";
import postgraphile from "postgraphile";

createServer(postgraphile());

For more information around using PostGraphile as a library, and the options the API expects read the library usage article.

There is also a docker image for running PostGraphile maintained by @angelosarto, simply pass the same options to the docker container:

# TODO: these commands are NOT correct right now - we need to publish postgraphile!
# In the interrim you can use older version by replacing this with `postgraphql/postgraphql`
docker pull graphile/postgraphile
docker run graphile/postgraphile --help

To connect to a database and expose the PostGraphile port try this:

# TODO: make this work!
docker run -p 5000:5000 graphile/postgraphile --connection postgres://POSTGRES_USER:POSTGRES_PASSWORD@POSTGRES_HOST:POSTGRES_PORT/POSTGRES_DATABASE

Also make sure to check out the forum example and especially step by step tutorial for a demo of a PostGraphile compliant schema and authentication.

Benefits

PostGraphile uses the joint benefits of PostgreSQL and GraphQL to provide a number of key benefits.

Automatic Relation Detection

Does your table’s authorId column reference another table? PostGraphile knows and will give you a field for easily querying that reference.

A schema like:

create table post (
  id serial primary key,
  author_id int non null references user(id),
  headline text,
  body text,
  ...
);

Can query relations like so:

{
  allPosts {
    edges {
      node {
        headline
        body
        author: userByAuthorId {
          name
        }
      }
    }
  }
}

Custom Mutations and Computed Columns

Procedures in PostgreSQL are powerful for writing business logic in your database schema, and PostGraphile allows you to access those procedures through a GraphQL interface. Create a custom mutation, write an advanced SQL query, or even extend your tables with computed columns! Procedures allow you to write logic for your app in SQL instead of in the client all while being accessible through the GraphQL interface.

So a search query could be written like this:

create function search_posts(search text) returns setof post as $$
  select *
  from post
  where
    headline ilike ('%' || search || '%') or
    body ilike ('%' || search || '%')
$$ language sql stable;

And queried through GraphQL like this:

{
  searchPosts(search: "Hello world", first: 5) {
    pageInfo {
      hasNextPage
    }
    totalCount
    edges {
      cursor
      node {
        headline
        body
      }
    }
  }
}

For more information, check out our procedure documentation and our advanced queries documentation.

Advanced Watch Mode

Running PostGraphile in watch mode will get you the best experience for iterating on a GraphQL API in the whole GraphQL ecosystem.

postgraphile --watch

PostGraphile will watch your Postgres database for changes. New tables, updated columns, new procedures, and more! When these changes are detected PostGraphile will re-create your schema and will automatically update any opened GraphiQL windows with the new schema while preserving your navigation state in the documentation viewer.

Fully Documented APIs

Introspection of a GraphQL schema is powerful for developer tooling and one element of introspection is that every type in GraphQL has an associated description field. As PostgreSQL allows you to document your database objects, naturally PostGraphile exposes these documentation comments through GraphQL.

Documenting PostgreSQL objects with the COMMENT command like so:

create table user (
  username text non null unique,
  ...
);

comment on table user is 'A human user of the forum.';
comment on column user.username is 'A unique name selected by the user to represent them on our site.';

Will let you reflect on the schema and get the JSON below:

{
  __type(name: "User") { ... }
}
{
  "__type": {
    "name": "User",
    "description": "A human user of the forum.",
    "fields": {
      "username": {
        "name": "username",
        "description":
          "A unique name selected by the user to represent them on our site."
      }
    }
  }
}

UI For Development Comes Standard

GraphiQL is a great tool by Facebook to let you interactively explore your data. When development mode is enabled in PostGraphile, the GraphiQL interface will be automatically displayed at your GraphQL endpoint.

Just navigate with your browser to the URL printed to your console after starting PostGraphile and use GraphiQL with your data! Even if you don’t want to use GraphQL in your app, this is a great interface for working with any PostgreSQL database.

Token Based Authorization

PostGraphile lets you use token based authentication with JSON Web Tokens (JWT) to secure your API. It doesn’t make sense to redefine your authentication in the API layer, instead just put your authorization logic in the database schema! With an advanced grants system and row level security, authorization in PostgreSQL is more than enough for your needs.

PostGraphile follows the PostgreSQL JSON Web Token Serialization Specification for serializing JWTs to the database for your use in authorization. The role claim of your JWT will become your PostgreSQL role and all other claims can be found under the jwt.claims namespace (see retrieving claims in PostgreSQL).

To enable token based authorization use the --secret <string> command line argument with a secure string PostGraphile will use to sign and verify tokens. And if you don’t want authorization, just don’t set the --secret argument and PostGraphile will ignore all authorization information!

Cursor Based Pagination For Free

There are some problems with traditional limit/offset pagination and realtime data. For more information on such problems, read this article.

PostGraphile not only provides limit/offset pagination, but it also provides cursor based pagination ordering on the column of your choice. Never again skip an item with free cursor based pagination!

Relay Specification Compliant

You don’t have to use GraphQL with React and Relay, but if you are, PostGraphile implements the brilliant Relay specifications for GraphQL. Even if you are not using Relay your project will benefit from having these strong, well thought out specifications implemented by PostGraphile.

The specific specs PostGraphile implements are:


Documentation


Requirements

We officially only support Node v8.6+; if you require support for an earlier version of Node please raise an issue explaining why you can't update to the latest Node LTS and we'll wee what we can do.

We officially support PostgreSQL 9.6+, but the tests run against PostgreSQL 9.4 so all our core functionality should work on that DB too.

We don't have much in the way of support for PG10-only features yet - if you'd like to sponsor development of these, please get in touch!

The Graphile suite of tools are developed under Mac OS X and tested under Linux, since we're a small project we don't have the resources to ensure strong Windows support, however we would like to support that platform so if you have any issues related to operating system, please raise an issue or — even better — a pull request!

Contributing

Want to help testing and developing PostGraphile? Check out the contributing document to get started quickly!

Maintenance

The maintainer of this project is @Benjie - follow him on Twitter!

Thanks

Thanks so much to the people working on PostgREST which was definitely a huge inspiration for this project!

A humongous, heart-felt, thank you to the original author of PostGraphile - Caleb Meredith - for everything he put into PostGraphile! He's now graduated from the project and we all wish him the best for his future ventures!

Even more thanks to each and every sponsor of the project - without their help progress would be a lot slower! Please join them in supporting this project 🙏

Thanks and enjoy 👍

5.0.0-beta.21

17 days ago

5.0.0-beta.20

27 days ago

5.0.0-beta.19

1 month ago

5.0.0-beta.18

2 months ago

5.0.0-beta.17

3 months ago

4.14.0

5 months ago

5.0.0-beta.12

5 months ago

5.0.0-beta.15

5 months ago

5.0.0-beta.16

4 months ago

5.0.0-beta.13

5 months ago

5.0.0-beta.14

5 months ago

5.0.0-beta.8

6 months ago

5.0.0-beta.9

6 months ago

5.0.0-beta.11

5 months ago

5.0.0-beta.10

6 months ago

5.0.0-beta.6

7 months ago

5.0.0-beta.7

6 months ago

5.0.0-beta.4

7 months ago

5.0.0-beta.5

7 months ago

5.0.0-beta.2

7 months ago

5.0.0-beta.3

7 months ago

5.0.0-beta.1

7 months ago

5.0.0-alpha.7

9 months ago

5.0.0-alpha.6

9 months ago

5.0.0-alpha.5

9 months ago

5.0.0-alpha.4

9 months ago

5.0.0-alpha.9

9 months ago

5.0.0-alpha.8

9 months ago

5.0.0-alpha.19

7 months ago

5.0.0-alpha.17

7 months ago

5.0.0-alpha.18

7 months ago

5.0.0-alpha.15

8 months ago

5.0.0-alpha.16

8 months ago

5.0.0-alpha.13

8 months ago

5.0.0-alpha.14

8 months ago

5.0.0-alpha.11

9 months ago

5.0.0-alpha.12

8 months ago

5.0.0-alpha.10

9 months ago

5.0.0-alpha.20

7 months ago

5.0.0-1.3

10 months ago

5.0.0-1.2

11 months ago

5.0.0-1.1

11 months ago

5.0.0-alpha.3

10 months ago

5.0.0-alpha.2

10 months ago

5.0.0-alpha.1

10 months ago

5.0.0-0.5

1 year ago

5.0.0-0.4

1 year ago

5.0.0-0.3

1 year ago

5.0.0-0.9

1 year ago

5.0.0-0.8

1 year ago

5.0.0-0.7

1 year ago

5.0.0-0.6

1 year ago

4.13.0

1 year ago

5.0.0-0.30

12 months ago

5.0.0-0.37

11 months ago

5.0.0-0.36

12 months ago

5.0.0-0.35

12 months ago

5.0.0-0.34

12 months ago

5.0.0-0.33

12 months ago

5.0.0-0.32

12 months ago

5.0.0-0.31

12 months ago

5.0.0-0.29

12 months ago

5.0.0-0.28

1 year ago

5.0.0-0.27

1 year ago

5.0.0-0.26

1 year ago

5.0.0-0.25

1 year ago

5.0.0-0.24

1 year ago

5.0.0-0.23

1 year ago

5.0.0-0.22

1 year ago

5.0.0-0.21

1 year ago

5.0.0-0.20

1 year ago

5.0.0-0.19

1 year ago

5.0.0-0.18

1 year ago

5.0.0-0.17

1 year ago

5.0.0-0.16

1 year ago

5.0.0-0.15

1 year ago

5.0.0-0.14

1 year ago

5.0.0-0.13

1 year ago

5.0.0-0.12

1 year ago

5.0.0-0.11

1 year ago

5.0.0-0.10

1 year ago

4.12.12

1 year ago

4.12.11

2 years ago

4.12.10

2 years ago

4.12.9

2 years ago

4.12.7

2 years ago

4.12.8

2 years ago

4.12.6

2 years ago

4.12.5

2 years ago

4.12.4

2 years ago

4.12.3

3 years ago

4.12.0

3 years ago

4.12.1

3 years ago

4.12.2

3 years ago

4.12.0-alpha.0

3 years ago

4.11.0

3 years ago

4.11.0-rc.0

3 years ago

4.11.0-alpha.1

3 years ago

4.11.0-alpha.0

3 years ago

4.10.0

3 years ago

4.10.0-alpha.0

3 years ago

4.10.0-317aa40db

3 years ago

4.9.2

3 years ago

4.9.1

3 years ago

4.9.0

3 years ago

4.8.0

4 years ago

4.8.0-rc.0

4 years ago

4.7.0

4 years ago

4.6.0

4 years ago

4.5.5

4 years ago

4.5.4

4 years ago

4.5.3

4 years ago

4.5.2

4 years ago

4.5.1

4 years ago

4.6.0-alpha.0

4 years ago

4.5.0

4 years ago

4.5.0-rc.4

4 years ago

4.5.0-rc.3

4 years ago

4.5.0-rc.2

4 years ago

4.5.0-rc.1

4 years ago

4.5.0-rc.0

4 years ago

4.4.5-alpha.0

4 years ago

4.4.4

4 years ago

4.4.3

5 years ago

4.4.2

5 years ago

4.4.2-rc.0

5 years ago

4.4.1

5 years ago

4.4.1-rc.0

5 years ago

4.4.1-alpha.4

5 years ago

4.4.1-alpha.3

5 years ago

4.4.1-alpha.2

5 years ago

4.4.1-alpha.1

5 years ago

4.4.1-alpha.0

5 years ago

4.4.0

5 years ago

4.4.0-rc.1

5 years ago

4.4.0-rc.0

5 years ago

4.4.0-beta.13

5 years ago

4.4.0-beta.12

5 years ago

4.4.0-beta.11

5 years ago

4.4.0-beta.10

5 years ago

4.4.0-beta.9

5 years ago

4.4.0-beta.8

5 years ago

4.4.0-beta.7

5 years ago

4.4.0-beta.6

5 years ago

4.4.0-beta.5

5 years ago

4.4.0-beta.4

5 years ago

4.4.0-beta.3

5 years ago

4.4.0-beta.2

5 years ago

4.4.0-beta.1

5 years ago

4.4.0-beta.0

5 years ago

4.4.0-alpha.0

5 years ago

4.3.4-beta.1

5 years ago

4.3.4-beta.0

5 years ago

4.3.4-alpha.5

5 years ago

4.3.4-alpha.4

5 years ago

4.3.4-alpha.3

5 years ago

4.3.4-alpha.2

5 years ago

4.3.4-alpha.1

5 years ago

4.3.4-alpha.0

5 years ago

4.3.4-rc.0

5 years ago

4.3.3-1

5 years ago

4.3.3-0

5 years ago

4.3.3

5 years ago

4.3.2

5 years ago

4.3.1

5 years ago

4.3.0

5 years ago

4.2.0

5 years ago

4.1.1-canary.1

5 years ago

4.1.1-canary.0

5 years ago

4.1.0

5 years ago

4.1.0-rc.5

5 years ago

4.1.0-rc.4

5 years ago

4.1.0-rc.3

5 years ago

4.1.0-rc.2

5 years ago

4.1.0-rc.1

5 years ago

4.1.0-rc.0

5 years ago

4.1.0-alpha.1

5 years ago

4.1.0-alpha.0

5 years ago

4.0.1

5 years ago

4.0.0

5 years ago

4.0.0-rc.5.1

5 years ago

4.0.0-rc.5

5 years ago

4.0.0-rc.4.1

5 years ago

4.0.0-rc.4

6 years ago

4.0.0-rc.3.4

6 years ago

4.0.0-rc.3.3

6 years ago

4.0.0-rc.3.2

6 years ago

4.0.0-rc.3.1

6 years ago

4.0.0-rc.3

6 years ago

4.0.0-rc.2

6 years ago

4.0.0-rc.1.4

6 years ago

4.0.0-rc.1.3

6 years ago

4.0.0-rc.1.2

6 years ago

4.0.0-rc.1.1

6 years ago

4.0.0-rc.1

6 years ago

4.0.0-beta.10

6 years ago

4.0.0-beta.9.3

6 years ago

4.0.0-beta.9.2

6 years ago

4.0.0-beta.9.1

6 years ago

4.0.0-beta.9

6 years ago

4.0.0-beta.8

6 years ago

4.0.0-beta.7

6 years ago

4.0.0-beta.6

6 years ago

4.0.0-beta.5

6 years ago

4.0.0-beta.4

6 years ago

4.0.0-beta.3

6 years ago

4.0.0-beta.2

6 years ago

4.0.0-beta.1

6 years ago

4.0.0-beta.0

6 years ago

4.0.0-alpha2.33

6 years ago

4.0.0-alpha2.32

6 years ago

4.0.0-alpha2.31

6 years ago

4.0.0-alpha2.30

6 years ago

4.0.0-alpha2.29

6 years ago

4.0.0-alpha2.28

6 years ago

4.0.0-alpha2.27

6 years ago

4.0.0-alpha2.26

6 years ago

4.0.0-alpha2.25

6 years ago

4.0.0-alpha2.24

6 years ago

4.0.0-alpha2.23

6 years ago

4.0.0-alpha2.22

6 years ago

4.0.0-alpha2.21

6 years ago

4.0.0-alpha2.20

6 years ago

4.0.0-alpha2.19

6 years ago

4.0.0-alpha2.18

6 years ago

4.0.0-alpha2.16

6 years ago

4.0.0-alpha2.12

7 years ago

4.0.0-alpha2.10

7 years ago

4.0.0-alpha2.9

7 years ago

4.0.0-alpha2.8

7 years ago