Skip to content

Commit 4a3456d

Browse files
committed
tutorial_section-2.md finished
1 parent a63adb4 commit 4a3456d

File tree

7 files changed

+3696
-2
lines changed

7 files changed

+3696
-2
lines changed

.env

+1
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,4 @@ POSTGRES_PASSWORD=SAMPLE_PASSWORD
33
POSTGRES_DB=todo_db
44
POSTGRES_PORT=5432
55
POSTGRES_HOST=127.0.0.1
6+
PORT=3000

package.json

+11-2
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,23 @@
22
"name": "pg-todo-tutorial",
33
"version": "0.0.1",
44
"description": "Postgraphile TODO Tutorial",
5-
"main": "index.js",
5+
"main": "server/index.js",
66
"repository": "https://github.com/dkkloimwieder/pg-todo-tutorial",
77
"author": "d kloimwieder <[email protected]>",
88
"license": "MIT",
99
"dependencies": {
1010
"@graphile-contrib/pg-simplify-inflector": "^6.1.0",
1111
"db-migrate": "^1.0.0-beta.16",
1212
"db-migrate-pg": "^1.2.2",
13-
"dotenv": "^10.0.0"
13+
"express": "^4.17.1",
14+
"postgraphile": "^4.12.4"
15+
},
16+
"devDependencies": {
17+
"dotenv": "^10.0.0",
18+
"nodemon": "^2.0.13"
19+
},
20+
"scripts": {
21+
"start": "node server/index.js",
22+
"watch": "nodemon server/index.js"
1423
}
1524
}

pg-todo-tutorial_section-2.md

+77
Original file line numberDiff line numberDiff line change
@@ -241,3 +241,80 @@ mutation {
241241
```
242242
243243
</details>
244+
245+
At this point we will go ahead and setup Express.js so that we have a more permanent solution than running postgraphile from the command line.
246+
247+
```sh
248+
yarn add express
249+
yarn add nodemon -D
250+
```
251+
252+
We will `mkdir server` for our express server to live in and then two files, and index.js and a postgraphile.js
253+
254+
```js
255+
/* server/index.js */
256+
257+
const path = require('path')
258+
require('dotenv').config({ path: path.resolve(__dirname, '../.env') })
259+
const express = require('express');
260+
const postgraphile = require('./postgraphile');
261+
262+
const app = express();
263+
app.use(postgraphile);
264+
265+
app.listen(process.env.PORT)
266+
267+
```
268+
269+
Let's also define the `PORT` we will be using to serve content in our `.env`: `echo 'PORT=3333' >> .env`
270+
271+
`server/index.js` is pretty straightforward at this point. We need `server/postgraphile.js` to define out postgraphile [middleware][middleware].
272+
273+
[middleware]:<https://www.graphile.org/postgraphile/usage-library/>
274+
275+
```js
276+
/* server/postgraphile.js */
277+
278+
const { postgraphile } = require('postgraphile');
279+
const PgSimplifyInflectorPlugin = require('@graphile-contrib/pg-simplify-inflector');
280+
281+
const {POSTGRES_DB, POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_PORT} = process.env
282+
283+
module.exports = postgraphile(
284+
{
285+
database: POSTGRES_DB,
286+
host: POSTGRES_HOST,
287+
user: POSTGRES_USER,
288+
password: POSTGRES_PASSWORD,
289+
port: POSTGRES_PORT
290+
},
291+
'todo_public',
292+
{
293+
appendPlugins: [PgSimplifyInflectorPlugin],
294+
watchPg: true,
295+
graphiql: true,
296+
enhanceGraphiql: true
297+
}
298+
)
299+
300+
```
301+
302+
This file is also very basic and just assigns all the `.env` variables and CLI options we have already been using.
303+
304+
Then we just define a `watch` command in `package.json`
305+
306+
```json
307+
/* package.json */
308+
309+
{
310+
"name": "pg-todo-tutorial",
311+
...
312+
"scripts": {
313+
"watch": "nodemon server/index.js"
314+
}
315+
}
316+
```
317+
318+
`yarn watch` and browse to `localhost:3333/graphiql` We are now serving our database via node. Oh happy day.
319+
320+

server/index.js

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const path = require('path')
2+
require('dotenv').config({ path: path.resolve(__dirname, '../.env') })
3+
const express = require('express');
4+
const postgraphile = require('./postgraphile');
5+
6+
const app = express();
7+
app.use(postgraphile);
8+
9+
app.listen(process.env.PORT)

server/postgraphile.js

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const { postgraphile } = require('postgraphile');
2+
const PgSimplifyInflectorPlugin = require('@graphile-contrib/pg-simplify-inflector');
3+
4+
const {POSTGRES_DB, POSTGRES_HOST, POSTGRES_USER, POSTGRES_PASSWORD, POSTGRES_PORT} = process.env
5+
6+
module.exports = postgraphile(
7+
{
8+
database: POSTGRES_DB,
9+
host: POSTGRES_HOST,
10+
user: POSTGRES_USER,
11+
password: POSTGRES_PASSWORD,
12+
port: POSTGRES_PORT
13+
},
14+
'todo_public',
15+
{
16+
appendPlugins: [PgSimplifyInflectorPlugin],
17+
watchPg: true,
18+
graphiql: true,
19+
enhanceGraphiql: true
20+
}
21+
)
22+

0 commit comments

Comments
 (0)