@@ -3,10 +3,9 @@ import { parse as parsePgConnectionString } from 'pg-connection-string'
3
3
import { GraphQLSchema } from 'graphql'
4
4
import { EventEmitter } from 'events'
5
5
import chalk = require( 'chalk' )
6
- import createPostGraphQLSchema from './schema/createPostGraphQLSchema '
6
+ import { createPostGraphQLSchema , watchPostGraphQLSchema } from 'postgraphql-build '
7
7
import createPostGraphQLHttpRequestHandler , { HttpRequestHandler } from './http/createPostGraphQLHttpRequestHandler'
8
8
import exportPostGraphQLSchema from './schema/exportPostGraphQLSchema'
9
- import watchPgSchemas from './watch/watchPgSchemas'
10
9
11
10
type PostGraphQLOptions = {
12
11
classicIds ?: boolean ,
@@ -90,39 +89,14 @@ export default function postgraphql (
90
89
: poolOrConfig || { } ,
91
90
)
92
91
92
+ const _emitter = new EventEmitter ( )
93
+
93
94
// Creates a promise which will resolve to a GraphQL schema. Connects a
94
95
// client from our pool to introspect the database.
95
96
//
96
97
// This is not a constant because when we are in watch mode, we want to swap
97
98
// 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 ( ) ;
126
100
127
101
// Finally create our Http request handler using our options, the Postgres
128
102
// pool, and GraphQL schema. Return the final result.
@@ -132,25 +106,27 @@ export default function postgraphql (
132
106
_emitter,
133
107
} ) )
134
108
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
- */
143
109
async function createGqlSchema ( ) : Promise < GraphQLSchema > {
144
110
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 ;
154
130
}
155
131
// If we fail to build our schema, log the error and exit the process.
156
132
catch ( error ) {
0 commit comments