@@ -13,8 +13,14 @@ import { parse } from './language/parser';
13
13
import { validate } from './validation/validate' ;
14
14
import { execute } from './execution/execute' ;
15
15
import type { GraphQLSchema } from './type/schema' ;
16
- import type { ExecutionResult } from './execution/execute' ;
16
+ // import type { ExecutionResult } from './execution/execute';
17
17
18
+ type onNextBlock = ( any ) => void ;
19
+ type onError = ( Error , boolean ) => void ;
20
+ type onEnd = ( any ) => void ;
21
+
22
+ type UniversalCallbackFunction =
23
+ ( onNext : onNextBlock , onError : onError , onEnd : onEnd ) => void ;
18
24
19
25
/**
20
26
* This is the primary entry point function for fulfilling GraphQL operations
@@ -40,33 +46,64 @@ import type { ExecutionResult } from './execution/execute';
40
46
* possible operations. Can be omitted if requestString contains only
41
47
* one operation.
42
48
*/
43
- export function graphql (
49
+ export function graphql$UCCF (
44
50
schema : GraphQLSchema ,
45
51
requestString : string ,
46
52
rootValue ?: mixed ,
47
53
contextValue ?: mixed ,
48
54
variableValues ?: ?{ [ key : string ] : mixed } ,
49
55
operationName ?: ?string
50
- ) : Promise < ExecutionResult > {
51
- return new Promise ( resolve => {
56
+ ) : UniversalCallbackFunction {
57
+
58
+ return function ( onNext ) {
52
59
const source = new Source ( requestString || '' , 'GraphQL request' ) ;
53
60
const documentAST = parse ( source ) ;
54
61
const validationErrors = validate ( schema , documentAST ) ;
55
62
if ( validationErrors . length > 0 ) {
56
- resolve ( { errors : validationErrors } ) ;
57
- } else {
58
- resolve (
59
- execute (
60
- schema ,
61
- documentAST ,
62
- rootValue ,
63
- contextValue ,
64
- variableValues ,
65
- operationName
66
- )
67
- ) ;
63
+ onNext ( { errors : validationErrors } ) ;
64
+ return ;
65
+ }
66
+ try {
67
+ // !!!:
68
+ // Currently, although graphql$UCCF adopts UCC design, the graphql$UCCF is
69
+ // not reactive due to the upstream (execute) isn't reactive (as Promise).
70
+ // We'll refactor `execute()` later.
71
+ execute (
72
+ schema ,
73
+ documentAST ,
74
+ rootValue ,
75
+ contextValue ,
76
+ variableValues ,
77
+ operationName
78
+ ) . then ( data => {
79
+ onNext ( data ) ;
80
+ } ) ;
81
+ } catch ( error ) {
82
+ onNext ( { errors : [ error ] } ) ;
68
83
}
69
- } ) . then ( undefined , error => {
70
- return { errors : [ error ] } ;
71
- } ) ;
84
+ } ;
85
+ }
86
+
87
+ /**
88
+ * Turn a UCC function into a Promise.
89
+ *
90
+ * @see https://gist.github.com/xareelee/e85c8b2134ff1805ab1ab2f1c8a037ce
91
+ */
92
+ function promisify ( uccf ) {
93
+ return function ( ...args : any [ ] ) {
94
+ return new Promise ( ( resolve , reject ) => {
95
+ uccf ( ...args ) (
96
+ x => resolve ( x ) ,
97
+ err => reject ( err ) ,
98
+ end => resolve ( end )
99
+ ) ;
100
+ } ) ;
101
+ } ;
72
102
}
103
+
104
+ /**
105
+ * We export `graphql` as a Promise from `graphql$UCCF`.
106
+ *
107
+ * @see graphql$UCCF
108
+ */
109
+ export const graphql = promisify ( graphql$UCCF ) ;
0 commit comments