11import { isPromise } from './jsutils/isPromise.js' ;
22import type { PromiseOrValue } from './jsutils/PromiseOrValue.js' ;
33
4+ import type { GraphQLError } from './error/GraphQLError.js' ;
5+
6+ import type { DocumentNode } from './language/ast.js' ;
47import type { ParseOptions } from './language/parser.js' ;
5- import { parse } from './language/parser.js' ;
68import type { Source } from './language/source.js' ;
79
10+ import type { GraphQLSchema } from './type/schema.js' ;
811import { validateSchema } from './type/validate.js' ;
912
1013import type { ValidationOptions } from './validation/validate.js' ;
11- import { validate } from './validation/validate.js' ;
1214import type { ValidationRule } from './validation/ValidationContext.js' ;
1315
1416import type { ExecutionArgs } from './execution/execute.js' ;
15- import { execute } from './execution/execute.js' ;
1617import type { ExecutionResult } from './execution/Executor.js' ;
1718
19+ import type { GraphQLHarness } from './harness.js' ;
20+ import { defaultHarness } from './harness.js' ;
21+
1822/**
1923 * This is the primary entry point function for fulfilling GraphQL operations
2024 * by parsing, validating, and executing a GraphQL document along side a
@@ -60,6 +64,7 @@ export interface GraphQLArgs
6064 extends ParseOptions ,
6165 ValidationOptions ,
6266 Omit < ExecutionArgs , 'document' > {
67+ harness ?: GraphQLHarness | undefined ;
6368 source : string | Source ;
6469 rules ?: ReadonlyArray < ValidationRule > | undefined ;
6570}
@@ -87,6 +92,7 @@ export function graphqlSync(args: GraphQLArgs): ExecutionResult {
8792}
8893
8994function graphqlImpl ( args : GraphQLArgs ) : PromiseOrValue < ExecutionResult > {
95+ const harness = args . harness ?? defaultHarness ;
9096 const { schema, source } = args ;
9197
9298 // Validate Schema
@@ -98,17 +104,55 @@ function graphqlImpl(args: GraphQLArgs): PromiseOrValue<ExecutionResult> {
98104 // Parse
99105 let document ;
100106 try {
101- document = parse ( source , args ) ;
107+ document = harness . parse ( source , args ) ;
102108 } catch ( syntaxError ) {
103109 return { errors : [ syntaxError ] } ;
104110 }
105111
112+ if ( isPromise ( document ) ) {
113+ return document . then (
114+ ( resolvedDocument ) =>
115+ validateAndExecute ( harness , args , schema , resolvedDocument ) ,
116+ ( syntaxError : unknown ) => ( { errors : [ syntaxError as GraphQLError ] } ) ,
117+ ) ;
118+ }
119+
120+ return validateAndExecute ( harness , args , schema , document ) ;
121+ }
122+
123+ function validateAndExecute (
124+ harness : GraphQLHarness ,
125+ args : GraphQLArgs ,
126+ schema : GraphQLSchema ,
127+ document : DocumentNode ,
128+ ) : PromiseOrValue < ExecutionResult > {
106129 // Validate
107- const validationErrors = validate ( schema , document , args . rules , args ) ;
108- if ( validationErrors . length > 0 ) {
109- return { errors : validationErrors } ;
130+ const validationResult = harness . validate ( schema , document , args . rules , args ) ;
131+
132+ if ( isPromise ( validationResult ) ) {
133+ return validationResult . then ( ( resolvedValidationResult ) =>
134+ checkValidationAndExecute (
135+ harness ,
136+ args ,
137+ resolvedValidationResult ,
138+ document ,
139+ ) ,
140+ ) ;
141+ }
142+
143+ return checkValidationAndExecute ( harness , args , validationResult , document ) ;
144+ }
145+
146+ function checkValidationAndExecute (
147+ harness : GraphQLHarness ,
148+ args : GraphQLArgs ,
149+ validationResult : ReadonlyArray < GraphQLError > ,
150+ document : DocumentNode ,
151+ ) : PromiseOrValue < ExecutionResult > {
152+ if ( validationResult . length > 0 ) {
153+ return { errors : validationResult } ;
110154 }
111155
112156 // Execute
113- return execute ( { ...args , document } ) ;
157+ return harness . execute ( { ...args , document } ) ;
114158}
0 commit comments