Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PostGraphQL V4 - announcing graphile-build #506

Merged
merged 146 commits into from
Feb 23, 2018
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
146 commits
Select commit Hold shift + click to select a range
21ba9d5
Schema generation provided by postgraphql-build
benjie Jul 5, 2017
a0c4a13
Bump version for major performance and compatibility improvements
benjie Jul 5, 2017
aebcff3
Bump postgraphql-build version for Node sub-8 support
benjie Jul 6, 2017
fc8b963
Bump version for __id support
benjie Jul 7, 2017
1b504ed
Accept non-array schemas
benjie Jul 7, 2017
1a14923
Upgrade to fix issue with procs returning null compound types
benjie Jul 7, 2017
4cfb64a
Allow null in PG arrays
benjie Jul 7, 2017
f68f572
Install babel-polyfill
benjie Jul 7, 2017
ab8bb6a
Switch to yarn
benjie Jul 7, 2017
3da18c6
Delete unused
benjie Jul 7, 2017
b1caa0f
Disable test that hangs due to massive diff
benjie Jul 7, 2017
ba561aa
Upgrade postgraphql-build for better comments
benjie Jul 7, 2017
3528522
Update postgraphql-build to avoid error on mutation edges
benjie Jul 8, 2017
f20e013
Restore watching functionality
benjie Jul 12, 2017
bace492
JWT support
benjie Jul 12, 2017
bc6dc8a
Update createPostGraphQLSchema references
benjie Jul 12, 2017
854187c
'Fix' the tests
benjie Jul 12, 2017
5baad0e
Resolve mocking issues
benjie Jul 12, 2017
9b77170
Merge branch 'master' into benjie/graphql-build
benjie Jul 12, 2017
5c60b12
Domain support
benjie Jul 13, 2017
36f9efe
Merge branch 'master' into benjie/graphql-build
benjie Jul 13, 2017
2b4a344
Lots of compatibility fixes
benjie Jul 13, 2017
7e5b2df
Update graphql-build for many more descriptions
benjie Jul 13, 2017
bdae04b
Update graphql-build for many more descriptions
benjie Jul 13, 2017
ae99a03
Update graphql-build for more tweaks
benjie Jul 13, 2017
0df4bf7
Fix functions returning arrays
benjie Jul 13, 2017
899c74e
Moar descriptions
benjie Jul 13, 2017
4388f1c
Acceptable schema changes
benjie Jul 13, 2017
d1de4cf
Upgrade for a few more tweaks
benjie Jul 13, 2017
9abec13
Update the schema export test
benjie Jul 13, 2017
e65b6f8
Lint fixes
benjie Jul 13, 2017
5091e50
Fix TypeScript types
benjie Jul 13, 2017
62d7dcc
Install lodash peerDependency of postgraphql-build
benjie Jul 13, 2017
105d21a
Sort out dependencies
benjie Jul 13, 2017
dd72183
Switch CI script over to yarn
benjie Jul 13, 2017
a67f7d0
Fix case-sensitivity
benjie Jul 13, 2017
fc958a2
Upgrade for Node4 compat
benjie Jul 13, 2017
d6d90d7
CI: add yarn to cache, node 8 tests
benjie Jul 13, 2017
216a54d
Bump version to avoid yarn engine issue
benjie Jul 13, 2017
7730f91
Bump version number
benjie Jul 13, 2017
9b48f76
Don't crash on empty mutations
benjie Jul 14, 2017
97c9789
Rename various internals
benjie Jul 14, 2017
4b9fd17
Minor bump
benjie Jul 14, 2017
a522cfe
Upgrade so we can pass plugins to postgraphql-build
benjie Jul 15, 2017
80fccd9
Add CLI support for loading modules
benjie Jul 15, 2017
2567220
Switch to comma separated list due to tool restrictions
benjie Jul 15, 2017
7649a7b
Linting
benjie Jul 15, 2017
ede6ca7
Even smaller bundle
benjie Jul 15, 2017
8ade82a
Use prepack over prepublishOnly for pack/publish
benjie Jul 15, 2017
baeb56a
Try and solve babel-polyfill issue
benjie Jul 15, 2017
74855ba
Version bump for setting plugins
benjie Jul 15, 2017
4aea305
Remove need for babel-polyfill
benjie Jul 15, 2017
453aaca
Restore underscores to field names
benjie Jul 16, 2017
1417bc3
Version bump
benjie Jul 16, 2017
bdfddfc
Upgrade to Flow-ified version of graphql-build
benjie Jul 25, 2017
354df59
Bump version
benjie Jul 25, 2017
75243b5
Fix typo
benjie Jul 26, 2017
53e0491
Upgrade postgraphql-build
benjie Jul 26, 2017
c3eff1a
Bump version
benjie Jul 26, 2017
468ffff
Drop Node v6 from tests (just 4 and 8 should cover it)
benjie Jul 26, 2017
768fcc9
Bump
benjie Jul 28, 2017
e30e531
Upgrade graphql-build
benjie Jul 29, 2017
528932c
Mutation computation fix, module rename, bump
benjie Aug 1, 2017
061407f
Version bump to solve description='false'
benjie Aug 1, 2017
72cdeee
Update postgraphile-core to remove temp tables
benjie Aug 1, 2017
7055dc1
Upgrade postgraphile-core
benjie Aug 4, 2017
644f49d
Version bump
benjie Aug 4, 2017
c1c1f08
Upgrade postgraphile-core
benjie Aug 8, 2017
9061130
Merge branch 'master' into benjie/graphql-build
benjie Aug 8, 2017
bebb251
pg-minify is updated
benjie Aug 8, 2017
6f134d7
4.0.0-alpha2.6
benjie Aug 8, 2017
e2cb680
Upgrade postgraphile-core: enum capitalisation
benjie Aug 9, 2017
ab26b14
Upgrade postgraphile-core: solve JWT issues
benjie Aug 9, 2017
2c80a18
Version bump
benjie Aug 9, 2017
97b86b9
Upgrade postgraphile-core: remove extraneous nodeIds
benjie Aug 10, 2017
4fc0872
4.0.0-alpha2.9
benjie Aug 10, 2017
5a3b6d8
Fix tests
benjie Aug 10, 2017
ce7fb97
Deprecate --token in favour of --jwt-token-identifier
benjie Aug 11, 2017
5979eaf
Upgrade postgraphile-core - computed column fixes
benjie Aug 14, 2017
68db8ed
Bump postgraphile-core - fix typo
benjie Aug 15, 2017
b8a8cc8
Bump
benjie Aug 15, 2017
9b65130
Merge branch 'master' into benjie/graphql-build
benjie Aug 16, 2017
2a3498e
Fix issue with mutations
benjie Aug 16, 2017
b50fcff
Upgrade postgraphile-core - nullable relations
benjie Aug 20, 2017
4cc3ed0
Fix test snapshots
benjie Aug 20, 2017
2bc04bb
Upgrade postgraphile-core - fix reverse pagination setof procedure bug
benjie Aug 21, 2017
d2a9def
Don't clear the schema because it clears state
benjie Aug 31, 2017
af8cbc4
postgraphile-core bump
benjie Aug 31, 2017
b507e04
Version bump
benjie Aug 31, 2017
8e6a511
Upgrade postgraphile-core - fix procedures returning setof composite
benjie Sep 5, 2017
9742331
Upgrade postgraphile-core: allow overriding inflector
benjie Sep 11, 2017
0d1c04b
Upgrade postgraphile-core - fix UUID with --dynamic-json
benjie Sep 12, 2017
a1661b0
Upgrade postgraphile-core: namespace tweaks
benjie Sep 14, 2017
73be58f
Bump postgraphile-core
benjie Sep 22, 2017
49f7a56
Version bump
benjie Sep 22, 2017
7ca565e
Mutation payloads are nullable again
benjie Sep 22, 2017
689fada
Update README
benjie Sep 22, 2017
cfd6237
Merge branch 'master' into benjie/graphql-build
benjie Oct 29, 2017
51643bd
Upgrade postgraphile-core
benjie Oct 29, 2017
5526c2c
feat(postgraphql): Provide mechanism to add data to resolver context …
alexFaunt Oct 29, 2017
e8b0a1d
4.0.0-alpha2.21
benjie Oct 29, 2017
27d1e1e
Merge branch 'master' into benjie/graphql-build
benjie Nov 1, 2017
7a69eb2
feat(pgSettings): Only use settings of type string or number (#589)
Nov 1, 2017
ac0f4c9
Upgrade postgraphile-core: fix arrays of compound types
benjie Nov 2, 2017
4bf5871
4.0.0-alpha2.22
benjie Nov 2, 2017
3c6e3e7
Merge branch 'master' into benjie/graphql-build
benjie Nov 28, 2017
1b13809
v4.0.0-alpha2.23
benjie Nov 28, 2017
1f11dea
Merge branch 'master' into benjie/graphql-build
benjie Nov 29, 2017
20bd36e
v4.0.0-alpha2.24
benjie Nov 29, 2017
f72df0d
Merge branch 'master' into benjie/graphql-build
benjie Nov 29, 2017
9ac624d
Merge branch 'master' into benjie/graphql-build
benjie Nov 30, 2017
024da2a
v4.0.0-alpha2.25
benjie Nov 30, 2017
4af0238
Merge branch 'master' into benjie/graphql-build
benjie Dec 3, 2017
f15360d
Upgrade postgraphile-core
benjie Dec 3, 2017
865829b
Update tests for arbitrary precision types
benjie Dec 3, 2017
e5a15b6
v4.0.0-alpha2.26
benjie Dec 3, 2017
f6f252b
Merge branch 'master' into benjie/graphql-build
benjie Dec 16, 2017
a0e9f25
Upgrade postgraphile-core
benjie Dec 16, 2017
12846cb
Update snapshots
benjie Dec 16, 2017
0e572f0
v4.0.0-alpha2.27
benjie Dec 16, 2017
1fa4310
feat(additionalGraphQLContextFromRequest): add res to callback (#652)
benjie Dec 19, 2017
6a7d654
Merge branch 'master' into benjie/graphql-build
benjie Jan 12, 2018
5d9feab
Upgrade postgraphile-core
benjie Jan 14, 2018
d68e9cc
Add readCache/writeCache CLI options
benjie Jan 12, 2018
cf7a3c3
4.0.0-alpha2.28
benjie Jan 14, 2018
97a1ecf
Merge branch 'master' into benjie/graphql-build
benjie Jan 30, 2018
a37c59f
4.0.0-alpha2.29
benjie Jan 30, 2018
8acd3c3
Merge branch 'master' into benjie/graphql-build
benjie Jan 30, 2018
1c84813
Pin to GraphQL v0.11 due to graphql/graphql-js#1182
benjie Jan 30, 2018
660d3fb
Downgrade GraphQL to v0.11 due to graphql/graphql-js#1182
benjie Jan 30, 2018
6f91793
4.0.0-alpha2.30
benjie Jan 30, 2018
08133a5
Merge branch 'master' into benjie/graphql-build
benjie Feb 10, 2018
275267d
Widen pg supported range: >=6.1 <8
benjie Feb 17, 2018
7641a26
Upgrade postgraphile-core
benjie Feb 17, 2018
4582135
Update snapshots - fix cursors and array orderBy
benjie Feb 17, 2018
f5e19ba
4.0.0-alpha2.31
benjie Feb 17, 2018
2391185
Duck-type pg.Pool
benjie Feb 19, 2018
cfa4b74
Merge branch 'master' into benjie/graphql-build
benjie Feb 19, 2018
3035edc
fix space-before-function-paren
caub Jan 5, 2018
1cdc0d0
feat(postgraphile) Add support for putting options in .postgraphilerc.js
Jan 19, 2018
00bc27c
feat(postgraphql) Add support for declaring connection string via DAT…
Jan 19, 2018
acb594e
feat(postgraphql): support PGUSER and PGPASSWORD environment variable…
Feb 19, 2018
95a60ee
Upgrade postgraphile-core to remove joi
benjie Feb 19, 2018
7d29878
4.0.0-alpha2.32
benjie Feb 19, 2018
9d05288
Allow BoundPool as a Pool name also
benjie Feb 19, 2018
cdafe55
4.0.0-alpha2.33
benjie Feb 19, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Dockerfile
19 changes: 19 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
FROM node:alpine
LABEL description="A GraphQL API created by reflection over a PostgreSQL schmea https://github.com/postgraphql/postgraphql"

# alpine linux standard doesn't include bash, and postgraphql scripts have #! bash
RUN apk add --update bash && rm -rf /var/cache/apk/*

RUN mkdir -p /postgraphql
WORKDIR /postgraphql

COPY . /postgraphql

RUN npm install
RUN scripts/build
RUN npm pack
RUN npm install -g postgraphql-*.tgz

RUN rm -rf /postgraphql
EXPOSE 5000
ENTRYPOINT ["postgraphql", "-n", "0.0.0.0"]
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,19 @@ createServer(postgraphql())

For more information around using PostGraphQL as a library, and the options the API expects read the [library usage documentation article](https://github.com/calebmer/postgraphql/blob/master/docs/library.md).

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

```bash
docker pull postgraphql/postgraphql
docker run postgraphql/postgraphql --help
```

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

```bash
docker run -p 5000:5000 postgraphql/postgraphql --connection postgres://POSTGRES_USER:POSTGRES_PASSWORD@POSTGRES_HOST:POSTGRES_PORT/POSTGRES_SCHEMA
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't POSTGRES_SCHEMA be POSTGRES_DATABASE?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup

```

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

[forum example]: https://github.com/calebmer/postgraphql/tree/master/examples/forum
Expand Down
2 changes: 1 addition & 1 deletion docs/default-role.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ The `auth_user` will have all the priveleges PostGraphQL might need.

You can also specify a `default_role` with PostGraphQL. The `default_role` will be used by PostGraphQL whenever no authorization token is provided or when the role claim in the authorization token is not specified. So all users that don’t explicitly specify a role will automatically use the `default_role`.

So the `default_role` should have restricted priveleges to only your data that is publicly accessible.
So the `default_role` should have restricted privileges to only your data that is publicly accessible.

After that you could also specify more roles like a `user_role` which should be included in the payload of your authorization tokens which may have more or less permissions then `default_role`.

Expand Down
2 changes: 1 addition & 1 deletion examples/forum/TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ After we insert a profile into `forum_example.person`, we use the `pgcrypto` ext
>
> For an overview of passwords in Postgres past the `pgcrypto` documentation, see the answer to the StackOverflow question “[How can I hash passwords in Postgres?](http://stackoverflow.com/a/18687445/1568890)”

At the end of the implementation you will see `language plpgsql strict security definer`. `language plpgsql` we already understand, but the other words are new. The word `strict` means that if the function gets null input, than the output will be automatically null as well and Postgres won’t call the function. That is `password` cannot be null or `first_name` cannot be null otherwise the result will also be null and nothing will be executed. The words `security definer` mean that this function is executed with the privileges of the Postgres user who created it. Remember how we said users would never be able to insert into `forum_example_private.person_account`? Well this function can insert into `forum_example_private.person_account` because it uses the privileges of the definer.
At the end of the implementation you will see `language plpgsql strict security definer`. `language plpgsql` we already understand, but the other words are new. The word `strict` means that if the function gets null input, then the output will be automatically null as well and Postgres won’t call the function. That is `password` cannot be null or `first_name` cannot be null otherwise the result will also be null and nothing will be executed. The words `security definer` mean that this function is executed with the privileges of the Postgres user who created it. Remember how we said users would never be able to insert into `forum_example_private.person_account`? Well this function can insert into `forum_example_private.person_account` because it uses the privileges of the definer.

> **Warning:** Make sure that when you create a function with `security definer` there are no ‘holes’ a user could use to see or mutate more data than they are not allowed to. Since the above is a simple function, we are fine. If you don’t need `security definer`, try not to use it.

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
"pg-connection-string": "^0.1.3",
"pg-minify": "^0.4.1",
"pluralize": "^3.0.0",
"postgraphql-build": "0.0.1-alpha5.1",
"postgraphql-build": "0.0.1-alpha6.0",
"postgres-interval": "^1.0.2",
"send": "^0.14.1",
"tslib": "^1.5.0"
Expand Down
17 changes: 17 additions & 0 deletions src/postgraphql/__tests__/__mocks__/postgraphql-build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const { GraphQLSchema, GraphQLObjectType, GraphQLInt } = require('graphql');

const dummySchema = new GraphQLSchema({
query: new GraphQLObjectType({
name: 'MockSchemaQuery',
fields: {
foo: {
type: GraphQLInt,
}
}
})
})

module.exports = {
createPostGraphQLSchema: jest.fn(async (a, b, c) => dummySchema),
watchPostGraphQLSchema: jest.fn(async (a, b, c, cb) => cb(dummySchema)),
}
99 changes: 14 additions & 85 deletions src/postgraphql/__tests__/postgraphql-test.js
Original file line number Diff line number Diff line change
@@ -1,67 +1,26 @@
jest.mock('pg')
jest.mock('pg-connection-string')
jest.mock('../schema/createPostGraphQLSchema')
jest.mock('postgraphql-build')
jest.mock('../http/createPostGraphQLHttpRequestHandler')
jest.mock('../watch/watchPgSchemas')

import { Pool } from 'pg'
import { parse as parsePgConnectionString } from 'pg-connection-string'
import createPostGraphQLSchema from '../schema/createPostGraphQLSchema'
import { createPostGraphQLSchema, watchPostGraphQLSchema } from '..'
import createPostGraphQLHttpRequestHandler from '../http/createPostGraphQLHttpRequestHandler'
import watchPgSchemas from '../watch/watchPgSchemas'
import postgraphql from '../postgraphql'

const chalk = require('chalk')

createPostGraphQLHttpRequestHandler.mockImplementation(({ getGqlSchema }) => Promise.resolve(getGqlSchema()).then(() => null))
watchPgSchemas.mockImplementation(() => Promise.resolve())

test('will use the first parameter as the pool if it is an instance of `Pool`', async () => {
const pgPool = new Pool()
await postgraphql(pgPool)
expect(pgPool.connect.mock.calls).toEqual([[]])
expect(createPostGraphQLHttpRequestHandler.mock.calls.length).toBe(1)
expect(createPostGraphQLHttpRequestHandler.mock.calls[0][0].pgPool).toBe(pgPool)
})

test('will use the config to create a new pool', async () => {
Pool.mockClear()
createPostGraphQLHttpRequestHandler.mockClear()
const pgPoolConfig = Symbol('pgPoolConfig')
await postgraphql(pgPoolConfig)
expect(Pool.mock.calls).toEqual([[pgPoolConfig]])
expect(Pool.mock.instances[0].connect.mock.calls).toEqual([[]])
expect(createPostGraphQLHttpRequestHandler.mock.calls.length).toBe(1)
expect(createPostGraphQLHttpRequestHandler.mock.calls[0][0].pgPool).toBe(Pool.mock.instances[0])
})

test('will parse a string config before creating a new pool', async () => {
Pool.mockClear()
createPostGraphQLHttpRequestHandler.mockClear()
parsePgConnectionString.mockClear()
const pgPoolConnectionString = 'abcdefghijklmnopqrstuvwxyz'
const pgPoolConfig = Symbol('pgPoolConfig')
parsePgConnectionString.mockReturnValueOnce(pgPoolConfig)
await postgraphql(pgPoolConnectionString)
expect(parsePgConnectionString.mock.calls).toEqual([[pgPoolConnectionString]])
expect(Pool.mock.calls).toEqual([[pgPoolConfig]])
expect(Pool.mock.instances[0].connect.mock.calls).toEqual([[]])
expect(createPostGraphQLHttpRequestHandler.mock.calls.length).toBe(1)
expect(createPostGraphQLHttpRequestHandler.mock.calls[0][0].pgPool).toBe(Pool.mock.instances[0])
})

test('will use a connected client from the pool, the schemas, and options to create a GraphQL schema', async () => {
createPostGraphQLSchema.mockClear()
createPostGraphQLHttpRequestHandler.mockClear()
const pgPool = new Pool()
const schemas = [Symbol('schemas')]
const options = Symbol('options')
const pgClient = { release: jest.fn() }
pgPool.connect.mockReturnValue(Promise.resolve(pgClient))
await postgraphql(pgPool, schemas, options)
expect(pgPool.connect.mock.calls).toEqual([[]])
expect(createPostGraphQLSchema.mock.calls).toEqual([[pgClient, schemas, options]])
expect(pgClient.release.mock.calls).toEqual([[]])
expect(createPostGraphQLSchema.mock.calls).toEqual([[pgPool, schemas, options]])
})

test('will use a connected client from the pool, the default schema, and options to create a GraphQL schema', async () => {
Expand All @@ -70,14 +29,12 @@ test('will use a connected client from the pool, the default schema, and options
const pgPool = new Pool()
const options = Symbol('options')
const pgClient = { release: jest.fn() }
pgPool.connect.mockReturnValue(Promise.resolve(pgClient))
await postgraphql(pgPool, options)
expect(pgPool.connect.mock.calls).toEqual([[]])
expect(createPostGraphQLSchema.mock.calls).toEqual([[pgClient, ['public'], options]])
expect(pgClient.release.mock.calls).toEqual([[]])
expect(createPostGraphQLSchema.mock.calls).toEqual([[pgPool, ['public'], options]])
})

test('will use a created GraphQL schema to create the HTTP request handler and pass down options', async () => {
createPostGraphQLSchema.mockClear()
createPostGraphQLHttpRequestHandler.mockClear()
const pgPool = new Pool()
const gqlSchema = Symbol('gqlSchema')
Expand All @@ -95,48 +52,20 @@ test('will use a created GraphQL schema to create the HTTP request handler and p
})

test('will watch Postgres schemas when `watchPg` is true', async () => {
createPostGraphQLSchema.mockClear()
watchPostGraphQLSchema.mockClear()
const pgPool = new Pool()
const pgSchemas = [Symbol('a'), Symbol('b'), Symbol('c')]
await postgraphql(pgPool, pgSchemas, { watchPg: false })
await postgraphql(pgPool, pgSchemas, { watchPg: true })
expect(watchPgSchemas.mock.calls.length).toBe(1)
expect(watchPgSchemas.mock.calls[0].length).toBe(1)
expect(Object.keys(watchPgSchemas.mock.calls[0][0])).toEqual(['pgPool', 'pgSchemas', 'onChange'])
expect(watchPgSchemas.mock.calls[0][0].pgPool).toBe(pgPool)
expect(watchPgSchemas.mock.calls[0][0].pgSchemas).toBe(pgSchemas)
expect(typeof watchPgSchemas.mock.calls[0][0].onChange).toBe('function')
})
expect(createPostGraphQLSchema.mock.calls).toEqual([[pgPool, pgSchemas, { watchPg: false }]])

test('will create a new PostGraphQL schema on when `watchPgSchemas` emits a change', async () => {
watchPgSchemas.mockClear()
createPostGraphQLHttpRequestHandler.mockClear()
const gqlSchemas = [Symbol('a'), Symbol('b'), Symbol('c')]
let gqlSchemaI = 0
createPostGraphQLSchema.mockClear()
createPostGraphQLSchema.mockImplementation(() => Promise.resolve(gqlSchemas[gqlSchemaI++]))
const pgPool = new Pool()
const pgClient = { release: jest.fn() }
pgPool.connect.mockReturnValue(Promise.resolve(pgClient))
const mockLog = jest.fn()
const origLog = console.log
console.log = mockLog
await postgraphql(pgPool, [], { watchPg: true })
const { onChange } = watchPgSchemas.mock.calls[0][0]
const { getGqlSchema } = createPostGraphQLHttpRequestHandler.mock.calls[0][0]
expect(pgPool.connect.mock.calls).toEqual([[]])
expect(pgClient.release.mock.calls).toEqual([[]])
expect(await getGqlSchema()).toBe(gqlSchemas[0])
onChange({ commands: ['a', 'b', 'c'] })
expect(await getGqlSchema()).toBe(gqlSchemas[1])
onChange({ commands: ['d', 'e'] })
expect(await getGqlSchema()).toBe(gqlSchemas[2])
expect(pgPool.connect.mock.calls).toEqual([[], [], []])
expect(pgClient.release.mock.calls).toEqual([[], [], []])
expect(mockLog.mock.calls).toEqual([
[`Rebuilding PostGraphQL API after Postgres command(s): ️${chalk.bold.cyan('a')}, ${chalk.bold.cyan('b')}, ${chalk.bold.cyan('c')}`],
[`Rebuilding PostGraphQL API after Postgres command(s): ️${chalk.bold.cyan('d')}, ${chalk.bold.cyan('e')}`],
])
console.log = origLog
expect(watchPostGraphQLSchema.mock.calls.length).toBe(1);
expect(watchPostGraphQLSchema.mock.calls[0].length).toBe(4);
expect(watchPostGraphQLSchema.mock.calls[0][0]).toEqual(pgPool)
expect(watchPostGraphQLSchema.mock.calls[0][1]).toEqual(pgSchemas)
expect(watchPostGraphQLSchema.mock.calls[0][2]).toEqual({ watchPg: true })
expect(typeof watchPostGraphQLSchema.mock.calls[0][3]).toBe('function')
})

test('will not error if jwtSecret is provided without jwtPgTypeIdentifier', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
jest.unmock('postgraphql-build')

import { resolve as resolvePath } from 'path'
import { readFile, readdirSync } from 'fs'
import { graphql } from 'graphql'
import withPgClient from '../../__tests__/utils/withPgClient'
import { $$pgClient } from '../../postgres/inventory/pgClientFromContext'
import createPostGraphQLSchema from '../schema/createPostGraphQLSchema'
import { createPostGraphQLSchema } from '..'

// This test suite can be flaky. Increase it’s timeout.
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 20
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
jest.unmock('postgraphql-build')

import { resolve as resolvePath } from 'path'
import { readFile, readdirSync } from 'fs'
import { graphql } from 'graphql'
import withPgClient from '../../__tests__/utils/withPgClient'
import { $$pgClient } from '../../postgres/inventory/pgClientFromContext'
import createPostGraphQLSchema from '../schema/createPostGraphQLSchema'
import { createPostGraphQLSchema } from '..'


// This test suite can be flaky. Increase it’s timeout.
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 20
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
// TODO: There may be some excessive waste, if we could somehow filter what
// these guys see, that would be great 👍

jest.unmock('postgraphql-build')

import { printSchema } from 'graphql'
import withPgClient from '../../__tests__/utils/withPgClient'
import createPostGraphQLSchema from '../schema/createPostGraphQLSchema'
import { createPostGraphQLSchema } from '..'

// This test suite can be flaky. Increase it’s timeout.
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000 * 20
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
jest.mock('fs')
jest.unmock('postgraphql-build')

import withPgClient from '../../__tests__/utils/withPgClient'
import createPostGraphQLSchema from '../schema/createPostGraphQLSchema'
import { createPostGraphQLSchema } from '..'
import exportPostGraphQLSchema from '../schema/exportPostGraphQLSchema'
import { writeFile } from 'fs'

Expand Down
17 changes: 6 additions & 11 deletions src/postgraphql/postgraphql.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,26 @@ export default function postgraphql (
//
// This is not a constant because when we are in watch mode, we want to swap
// out the `gqlSchema`.
let gqlSchema = createGqlSchema();
let gqlSchema
let gqlSchemaPromise = createGqlSchema()

// Finally create our Http request handler using our options, the Postgres
// pool, and GraphQL schema. Return the final result.
return createPostGraphQLHttpRequestHandler(Object.assign({}, options, {
getGqlSchema: () => gqlSchema,
getGqlSchema: () => gqlSchema || gqlSchemaPromise,
pgPool,
_emitter,
}))

async function createGqlSchema (): Promise<GraphQLSchema> {
try {
if (options.watchPg) {
let firstSchema = true;
await watchPostGraphQLSchema(pgPool, pgSchemas, options, schema => {
gqlSchema = schema
if (firstSchema) {
firstSchema = false;
} else {
_emitter.emit('schemas:changed')
}
_emitter.emit('schemas:changed')
exportGqlSchema(gqlSchema)
});
if (firstSchema) {
if (!gqlSchema) {
throw new Error("Consistency error: watchPostGraphQLSchema promises to call the callback before the promise resolves; but this hasn't happened")
}
} else {
Expand All @@ -146,8 +142,7 @@ export default function postgraphql (
}

function handleFatalError (error: Error): never {
// tslint:disable-next-line no-console
console.error(`${error.stack}\n`)
process.stderr.write(`${error.stack}\n`) // console.error fails under the tests
process.exit(1)

// `process.exit` will mean all code below it will never get called.
Expand Down
25 changes: 13 additions & 12 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2678,20 +2678,21 @@ graphiql@^0.11.2:
codemirror-graphql "^0.6.8"
marked "0.3.6"

graphql-build-pg@^0.0.1-alpha5.1:
version "0.0.1-alpha5.1"
resolved "https://registry.yarnpkg.com/graphql-build-pg/-/graphql-build-pg-0.0.1-alpha5.1.tgz#1b093d16402d62c49b8d7ac96a40c27fccb9b0f4"
graphql-build-pg@^0.0.1-alpha6.0:
version "0.0.1-alpha6.0"
resolved "https://registry.yarnpkg.com/graphql-build-pg/-/graphql-build-pg-0.0.1-alpha6.0.tgz#09ccbf1a6d78a2c84b8651b7be65ac881a40ea13"
dependencies:
graphql-iso-date "^3.2.0"
graphql-type-json "^0.1.4"
jsonwebtoken "^7.4.1"
lru-cache "4.1.1"
pg-range-parser "^1.0.0"
pg-sql2 "^0.0.1-alpha5.1"
pluralize "^5.0.0"

graphql-build@^0.0.1-alpha5.1:
version "0.0.1-alpha5.1"
resolved "https://registry.yarnpkg.com/graphql-build/-/graphql-build-0.0.1-alpha5.1.tgz#ee634a4d34611e5f52ffb59a07bab9964d244e94"
graphql-build@^0.0.1-alpha6.0:
version "0.0.1-alpha6.0"
resolved "https://registry.yarnpkg.com/graphql-build/-/graphql-build-0.0.1-alpha6.0.tgz#c4c389681acfb16557e313acf7586012ae410e37"
dependencies:
graphql-parse-resolve-info "^0.0.1-alpha5.1"
graphql-type-json "^0.1.4"
Expand Down Expand Up @@ -3612,7 +3613,7 @@ jsonpointer@^4.0.0:
version "4.0.1"
resolved "https://registry.yarnpkg.com/jsonpointer/-/jsonpointer-4.0.1.tgz#4fd92cb34e0e9db3c89c8622ecf51f9b978c6cb9"

jsonwebtoken@^7.1.9:
jsonwebtoken@^7.1.9, jsonwebtoken@^7.4.1:
version "7.4.1"
resolved "https://registry.yarnpkg.com/jsonwebtoken/-/jsonwebtoken-7.4.1.tgz#7ca324f5215f8be039cd35a6c45bb8cb74a448fb"
dependencies:
Expand Down Expand Up @@ -4819,12 +4820,12 @@ postcss@^6.0.1:
source-map "^0.5.6"
supports-color "^4.1.0"

postgraphql-build@0.0.1-alpha5.1:
version "0.0.1-alpha5.1"
resolved "https://registry.yarnpkg.com/postgraphql-build/-/postgraphql-build-0.0.1-alpha5.1.tgz#e2c3de4c61454b3d5fcb67efad5cb5a20a96d71b"
postgraphql-build@0.0.1-alpha6.0:
version "0.0.1-alpha6.0"
resolved "https://registry.yarnpkg.com/postgraphql-build/-/postgraphql-build-0.0.1-alpha6.0.tgz#fb039c710584de9bc2b6447520fc9ca0cf0f2abd"
dependencies:
graphql-build "^0.0.1-alpha5.1"
graphql-build-pg "^0.0.1-alpha5.1"
graphql-build "^0.0.1-alpha6.0"
graphql-build-pg "^0.0.1-alpha6.0"

postgres-array@~1.0.0:
version "1.0.2"
Expand Down