11import debugFactory from "debug" ;
22import { Plugin } from "graphile-build" ;
3- import { GraphQLFieldConfig } from "graphql" ;
3+ import { GraphQLBoolean , GraphQLFieldConfig } from "graphql" ;
44import { PubSub } from "graphql-subscriptions" ;
55
66const debug = debugFactory ( "pg-pubsub" ) ;
@@ -12,6 +12,38 @@ function isPubSub(pubsub: any): pubsub is PubSub {
1212 return ! ! pubsub ;
1313}
1414
15+ const withInitialValue = < T > (
16+ initialVal : T ,
17+ asyncIterator : AsyncIterator < T >
18+ ) : AsyncIterable < T > => {
19+ return {
20+ [ Symbol . asyncIterator ] : async function * ( ) : AsyncIterator < T > {
21+ yield initialVal ;
22+
23+ /* TODO: when we can upgrade to Node 10.3+ we can replace the below with simply:
24+ for await (const val of asyncIterator) {
25+ yield val;
26+ }
27+ */
28+ try {
29+ while ( true ) {
30+ const next = await asyncIterator . next ( ) ;
31+ if ( next . done ) {
32+ return next . value ;
33+ } else {
34+ yield next . value ;
35+ }
36+ }
37+ } finally {
38+ // Terminate the previous iterator
39+ if ( typeof asyncIterator . return === "function" ) {
40+ asyncIterator . return ( ) ;
41+ }
42+ }
43+ } ,
44+ } ;
45+ } ;
46+
1547const PgGenericSubscriptionPlugin : Plugin = function (
1648 builder ,
1749 {
@@ -142,6 +174,12 @@ const PgGenericSubscriptionPlugin: Plugin = function (
142174 topic : {
143175 type : new GraphQLNonNull ( GraphQLString ) ,
144176 } ,
177+ initialEvent : {
178+ defaultValue : false ,
179+ type : new GraphQLNonNull ( GraphQLBoolean ) ,
180+ description :
181+ "If true, this subscription will trigger an event as soon as it initiates." ,
182+ } ,
145183 } ,
146184 subscribe : async ( _parent , args , _context , _resolveInfo ) => {
147185 const { pgClient } = _context ;
@@ -188,7 +226,11 @@ const PgGenericSubscriptionPlugin: Plugin = function (
188226 }
189227 } ) ;
190228 }
191- return asyncIterator ;
229+ if ( args . initialEvent ) {
230+ return withInitialValue ( null , asyncIterator ) ;
231+ } else {
232+ return asyncIterator ;
233+ }
192234 } ,
193235 resolve : async ( payload , _args , _context , _resolveInfo ) => {
194236 const result = { ...payload } ;
0 commit comments