Skip to content

Commit f20e013

Browse files
committed
Restore watching functionality
1 parent 3528522 commit f20e013

File tree

6 files changed

+47
-71
lines changed

6 files changed

+47
-71
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
"pg-connection-string": "^0.1.3",
4949
"pg-minify": "^0.4.1",
5050
"pluralize": "^3.0.0",
51-
"postgraphql-build": "0.0.1-alpha3.7",
51+
"postgraphql-build": "0.0.1-alpha5.1",
5252
"postgres-interval": "^1.0.2",
5353
"send": "^0.14.1",
5454
"tslib": "^1.5.0"

src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
1-
import { postgraphql, createPostGraphQLSchema, withPostGraphQLContext } from './postgraphql'
1+
import { postgraphql, createPostGraphQLSchema, watchPostGraphQLSchema, withPostGraphQLContext } from './postgraphql'
22

33
export default postgraphql
44

55
export {
66
postgraphql,
77
createPostGraphQLSchema,
8+
watchPostGraphQLSchema,
89
withPostGraphQLContext,
910
}

src/postgraphql/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import postgraphql from './postgraphql'
2-
import createPostGraphQLSchema from './schema/createPostGraphQLSchema'
2+
import { createPostGraphQLSchema, watchPostGraphQLSchema } from 'postgraphql-build'
33
import withPostGraphQLContext from './withPostGraphQLContext'
44

55
export {
66
postgraphql,
77
createPostGraphQLSchema,
8+
watchPostGraphQLSchema,
89
withPostGraphQLContext,
910
}

src/postgraphql/postgraphql.ts

Lines changed: 23 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ import { parse as parsePgConnectionString } from 'pg-connection-string'
33
import { GraphQLSchema } from 'graphql'
44
import { EventEmitter } from 'events'
55
import chalk = require('chalk')
6-
import createPostGraphQLSchema from './schema/createPostGraphQLSchema'
6+
import { createPostGraphQLSchema, watchPostGraphQLSchema } from 'postgraphql-build'
77
import createPostGraphQLHttpRequestHandler, { HttpRequestHandler } from './http/createPostGraphQLHttpRequestHandler'
88
import exportPostGraphQLSchema from './schema/exportPostGraphQLSchema'
9-
import watchPgSchemas from './watch/watchPgSchemas'
109

1110
type PostGraphQLOptions = {
1211
classicIds?: boolean,
@@ -90,39 +89,14 @@ export default function postgraphql (
9089
: poolOrConfig || {},
9190
)
9291

92+
const _emitter = new EventEmitter()
93+
9394
// Creates a promise which will resolve to a GraphQL schema. Connects a
9495
// client from our pool to introspect the database.
9596
//
9697
// This is not a constant because when we are in watch mode, we want to swap
9798
// out the `gqlSchema`.
98-
let gqlSchema = createGqlSchema()
99-
100-
const _emitter = new EventEmitter()
101-
102-
// If the user wants us to watch the schema, execute the following:
103-
if (options.watchPg) {
104-
watchPgSchemas({
105-
pgPool,
106-
pgSchemas,
107-
onChange: ({ commands }) => {
108-
// tslint:disable-next-line no-console
109-
console.log(`Rebuilding PostGraphQL API after Postgres command(s): ️${commands.map(command => chalk.bold.cyan(command)).join(', ')}`)
110-
111-
_emitter.emit('schemas:changed')
112-
113-
// Actually restart the GraphQL schema by creating a new one. Note that
114-
// `createGqlSchema` returns a promise and we aren’t ‘await’ing it.
115-
gqlSchema = createGqlSchema()
116-
},
117-
})
118-
// If an error occurs when watching the Postgres schemas, log the error and
119-
// exit the process.
120-
.catch(error => {
121-
// tslint:disable-next-line no-console
122-
console.error(`${error.stack}\n`)
123-
process.exit(1)
124-
})
125-
}
99+
let gqlSchema = createGqlSchema();
126100

127101
// Finally create our Http request handler using our options, the Postgres
128102
// pool, and GraphQL schema. Return the final result.
@@ -132,25 +106,27 @@ export default function postgraphql (
132106
_emitter,
133107
}))
134108

135-
/**
136-
* Creates a GraphQL schema by connecting a client from our pool which will
137-
* be used to introspect our Postgres database. If this function fails, we
138-
* will log the error and exit the process.
139-
*
140-
* This may only be executed once, at startup. However, if we are in watch
141-
* mode this will be updated whenever there is a change in our schema.
142-
*/
143109
async function createGqlSchema (): Promise<GraphQLSchema> {
144110
try {
145-
const pgClient = await pgPool.connect()
146-
const newGqlSchema = await createPostGraphQLSchema(pgClient, pgSchemas, options)
147-
exportGqlSchema(newGqlSchema)
148-
149-
// If no release function exists, don’t release. This is just for tests.
150-
if (pgClient && pgClient.release)
151-
pgClient.release()
152-
153-
return newGqlSchema
111+
if (options.watchPg) {
112+
let firstSchema = true;
113+
await watchPostGraphQLSchema(pgPool, pgSchemas, options, schema => {
114+
gqlSchema = schema
115+
if (firstSchema) {
116+
firstSchema = false;
117+
} else {
118+
_emitter.emit('schemas:changed')
119+
}
120+
exportGqlSchema(gqlSchema)
121+
});
122+
if (firstSchema) {
123+
throw new Error("Consistency error: watchPostGraphQLSchema promises to call the callback before the promise resolves; but this hasn't happened")
124+
}
125+
} else {
126+
gqlSchema = await createPostGraphQLSchema(pgPool, pgSchemas, options)
127+
exportGqlSchema(gqlSchema)
128+
}
129+
return gqlSchema;
154130
}
155131
// If we fail to build our schema, log the error and exit the process.
156132
catch (error) {

src/postgraphql/schema/createPostGraphQLSchema.ts

Lines changed: 0 additions & 2 deletions
This file was deleted.

yarn.lock

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2678,22 +2678,22 @@ graphiql@^0.11.2:
26782678
codemirror-graphql "^0.6.8"
26792679
marked "0.3.6"
26802680

2681-
graphql-build-pg@^0.0.1-alpha3.7:
2682-
version "0.0.1-alpha3.7"
2683-
resolved "https://registry.yarnpkg.com/graphql-build-pg/-/graphql-build-pg-0.0.1-alpha3.7.tgz#daffaa6f5e0302da2506fe4f2ecce4185fa93517"
2681+
graphql-build-pg@^0.0.1-alpha5.1:
2682+
version "0.0.1-alpha5.1"
2683+
resolved "https://registry.yarnpkg.com/graphql-build-pg/-/graphql-build-pg-0.0.1-alpha5.1.tgz#1b093d16402d62c49b8d7ac96a40c27fccb9b0f4"
26842684
dependencies:
26852685
graphql-iso-date "^3.2.0"
26862686
graphql-type-json "^0.1.4"
26872687
lru-cache "4.1.1"
26882688
pg-range-parser "^1.0.0"
2689-
pg-sql2 "^0.0.1-alpha3.0"
2689+
pg-sql2 "^0.0.1-alpha5.1"
26902690
pluralize "^5.0.0"
26912691

2692-
graphql-build@^0.0.1-alpha3.6:
2693-
version "0.0.1-alpha3.6"
2694-
resolved "https://registry.yarnpkg.com/graphql-build/-/graphql-build-0.0.1-alpha3.6.tgz#2f09d8db9cb364a6b2d77a81a911831a7b42427c"
2692+
graphql-build@^0.0.1-alpha5.1:
2693+
version "0.0.1-alpha5.1"
2694+
resolved "https://registry.yarnpkg.com/graphql-build/-/graphql-build-0.0.1-alpha5.1.tgz#ee634a4d34611e5f52ffb59a07bab9964d244e94"
26952695
dependencies:
2696-
graphql-parse-resolve-info "^0.0.1-alpha3.1"
2696+
graphql-parse-resolve-info "^0.0.1-alpha5.1"
26972697
graphql-type-json "^0.1.4"
26982698

26992699
graphql-iso-date@^3.2.0:
@@ -2735,9 +2735,9 @@ [email protected]:
27352735
graphql "^0.10.1"
27362736
graphql-language-service-types "0.0.21"
27372737

2738-
graphql-parse-resolve-info@^0.0.1-alpha3.1:
2739-
version "0.0.1-alpha3.1"
2740-
resolved "https://registry.yarnpkg.com/graphql-parse-resolve-info/-/graphql-parse-resolve-info-0.0.1-alpha3.1.tgz#9d724c41a0503e64433787bf871ad2957d542bc0"
2738+
graphql-parse-resolve-info@^0.0.1-alpha5.1:
2739+
version "0.0.1-alpha5.1"
2740+
resolved "https://registry.yarnpkg.com/graphql-parse-resolve-info/-/graphql-parse-resolve-info-0.0.1-alpha5.1.tgz#6421c259bad75c14a3ede99f609affbe8f1da659"
27412741

27422742
graphql-type-json@^0.1.4:
27432743
version "0.1.4"
@@ -4471,9 +4471,9 @@ pg-range-parser@^1.0.0:
44714471
version "1.0.0"
44724472
resolved "https://registry.yarnpkg.com/pg-range-parser/-/pg-range-parser-1.0.0.tgz#561e8c4c3dee98a1caa8288c5c7e4300861819bb"
44734473

4474-
pg-sql2@^0.0.1-alpha3.0:
4475-
version "0.0.1-alpha3.0"
4476-
resolved "https://registry.yarnpkg.com/pg-sql2/-/pg-sql2-0.0.1-alpha3.0.tgz#ec66d1011a8394295d386e8911d25f1b51841989"
4474+
pg-sql2@^0.0.1-alpha5.1:
4475+
version "0.0.1-alpha5.1"
4476+
resolved "https://registry.yarnpkg.com/pg-sql2/-/pg-sql2-0.0.1-alpha5.1.tgz#ea6dfc99a1bfc4717724b58d1c72ba74f2f4a900"
44774477

44784478
pg-types@1.*:
44794479
version "1.12.0"
@@ -4819,12 +4819,12 @@ postcss@^6.0.1:
48194819
source-map "^0.5.6"
48204820
supports-color "^4.1.0"
48214821

4822-
4823-
version "0.0.1-alpha3.7"
4824-
resolved "https://registry.yarnpkg.com/postgraphql-build/-/postgraphql-build-0.0.1-alpha3.7.tgz#bf07445371e5d5fb45946302de411e8b5c7daffc"
4822+
4823+
version "0.0.1-alpha5.1"
4824+
resolved "https://registry.yarnpkg.com/postgraphql-build/-/postgraphql-build-0.0.1-alpha5.1.tgz#e2c3de4c61454b3d5fcb67efad5cb5a20a96d71b"
48254825
dependencies:
4826-
graphql-build "^0.0.1-alpha3.6"
4827-
graphql-build-pg "^0.0.1-alpha3.7"
4826+
graphql-build "^0.0.1-alpha5.1"
4827+
graphql-build-pg "^0.0.1-alpha5.1"
48284828

48294829
postgres-array@~1.0.0:
48304830
version "1.0.2"

0 commit comments

Comments
 (0)