|
| 1 | +<p align="center"> |
| 2 | + <a href="https://www.graphile.org/postgraphile/"> |
| 3 | + <img alt="PostGraphile" src="https://www.graphile.org/images/postgraphile.optimized.svg" width="60" /> |
| 4 | + </a> |
| 5 | + <img alt="Graphile Heart" src="https://www.graphile.org/images/graphile.optimized.svg" width="60" /> |
| 6 | + <a href="https://www.gatsbyjs.org"> |
| 7 | + <img alt="Gatsby" src="https://www.gatsbyjs.org/monogram.svg" width="60" /> |
| 8 | + </a> |
| 9 | +</p> |
| 10 | + |
| 11 | +<h1 align="center"> |
| 12 | + gatsby-source-pg |
| 13 | +</h1> |
| 14 | + |
| 15 | +This module helps you pull your PostgreSQL database into Gatsby with minimal effort. |
| 16 | + |
| 17 | +To install: |
| 18 | + |
| 19 | +``` |
| 20 | +yarn add gatsby-source-pg |
| 21 | +``` |
| 22 | + |
| 23 | +(or `npm install --save gatsby-source-pg`) |
| 24 | + |
| 25 | +Then add the config to your `gatsby-config.js`: |
| 26 | + |
| 27 | +```js |
| 28 | +module.exports = { |
| 29 | + /* ... */ |
| 30 | + plugins: [ |
| 31 | + /* ... */ |
| 32 | + |
| 33 | + { |
| 34 | + resolve: "gatsby-source-pg", |
| 35 | + options: { |
| 36 | + connectionString: "postgres:///mydb", |
| 37 | + schema: "public", |
| 38 | + refetchInterval: 60, // Refetch data every 60 seconds |
| 39 | + }, |
| 40 | + }, |
| 41 | + ], |
| 42 | +}; |
| 43 | +``` |
| 44 | + |
| 45 | +The `connectionString` can be any valid PostgreSQL connection string, a full |
| 46 | +connection string might look like: |
| 47 | +`postgres://pg_user:pg_pass@pg_host:pg_port/pg_db?ssl=1` |
| 48 | + |
| 49 | +## How to query |
| 50 | + |
| 51 | +```graphql |
| 52 | +{ |
| 53 | + postgres { |
| 54 | + allPostsList { |
| 55 | + id |
| 56 | + authorId |
| 57 | + userByAuthorId { |
| 58 | + id |
| 59 | + username |
| 60 | + } |
| 61 | + title |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +## Example |
| 68 | + |
| 69 | +For a working example of `gatsby-source-pg`, see |
| 70 | +[gatsby-source-pg-example](https://github.com/graphile/gatsby-source-pg-example). |
| 71 | + |
| 72 | +## Thanks |
| 73 | + |
| 74 | +This plugin uses |
| 75 | +[`gatsby-source-graphql`](https://github.com/gatsbyjs/gatsby/tree/master/packages/gatsby-source-graphql#readme) |
| 76 | +to merge the PostGraphile GraphQL schema into Gatsby's. |
| 77 | + |
| 78 | +## Customising |
| 79 | + |
| 80 | +This plugin is powered by PostGraphile, which is built on the highly flexible |
| 81 | +and customisable Graphile Engine. |
| 82 | + |
| 83 | +You can add to `options` most of the [PostGraphile schema-only options](https://www.graphile.org/postgraphile/usage-schema/#api-createpostgraphileschemapgconfig-schemaname-options) |
| 84 | + |
| 85 | +In addition, we accept the `typeName` and `fieldName` options from |
| 86 | +`gatsby-source-graphql` which affect how the schema is namespaced, and the `refetchInterval` setting to trigger refetching data every X seconds. |
| 87 | + |
| 88 | +A common thing you might want to do is to shorten the names that PostGraphile uses by default, you can do this using a plugin such as `@graphile-contrib/pg-simplify-inflector`: |
| 89 | + |
| 90 | +```js |
| 91 | +// gatsby-config.js |
| 92 | +module.exports = { |
| 93 | + /* ... */ |
| 94 | + plugins: [ |
| 95 | + /* ... */ |
| 96 | + { |
| 97 | + resolve: "gatsby-source-pg", |
| 98 | + options: { |
| 99 | + connectionString: "postgres:///mydb", |
| 100 | + schema: "public", |
| 101 | + |
| 102 | + /* 👇 */ |
| 103 | + appendPlugins: [require("@graphile-contrib/pg-simplify-inflector")], |
| 104 | + /* 👆 */ |
| 105 | + }, |
| 106 | + }, |
| 107 | + ], |
| 108 | +}; |
| 109 | +``` |
| 110 | + |
| 111 | +## Getting help |
| 112 | + |
| 113 | +Pop into the Graphile Discord: [http://discord.gg/graphile](http://discord.gg/graphile) |
| 114 | + |
| 115 | +## Helpful links |
| 116 | + |
| 117 | +- [Gatsby documentation](https://www.gatsbyjs.org/) |
| 118 | +- [gatsby-source-pg-example](https://github.com/graphile/gatsby-source-pg-example/) |
| 119 | +- [PostGraphile documentation](https://www.graphile.org/postgraphile/) |
0 commit comments