@@ -155,6 +155,7 @@ def _parse_and_validate(
155155 allowed_operation_types : Optional [Set [OperationType ]],
156156 validation_rules : Optional [tuple [type [ASTValidationRule ], ...]] = None ,
157157 operation_name : Optional [str ] = None ,
158+ validate_document : Optional [bool ] = None ,
158159 # extensions_runner: SchemaExtensionsRunner
159160) -> DocumentNode :
160161 if allowed_operation_types is None :
@@ -171,8 +172,14 @@ def _parse_and_validate(
171172 try :
172173 if isinstance (query , str ):
173174 document_node = parse (query )
175+ if validate_document is None :
176+ # Validate the document by default for string queries
177+ validate_document = True
174178 else :
175179 document_node = query
180+ if validate_document is None :
181+ # Don't validate the document by default for DocumentNode queries
182+ validate_document = False
176183 except GraphQLError as e :
177184 raise GraphQLValidationError ([e ]) from e
178185
@@ -211,6 +218,7 @@ async def execute(
211218 custom_context_kwargs : Optional [dict [str , Any ]] = None ,
212219 execution_context_class : type [ExecutionContext ] | None = None ,
213220 validation_rules : Optional [tuple [type [ASTValidationRule ], ...]] = None ,
221+ validate_document : Optional [bool ] = None ,
214222) -> ExecutionResult :
215223 if allowed_operation_types is None :
216224 allowed_operation_types = DEFAULT_ALLOWED_OPERATION_TYPES
@@ -234,6 +242,7 @@ async def execute(
234242 allowed_operation_types ,
235243 validation_rules ,
236244 operation_name ,
245+ validate_document ,
237246 )
238247
239248 # async with extensions_runner.executing():
@@ -274,6 +283,7 @@ def execute_sync(
274283 custom_context_kwargs : Optional [dict [str , Any ]] = None ,
275284 execution_context_class : type [ExecutionContext ] | None = None ,
276285 validation_rules : Optional [tuple [type [ASTValidationRule ], ...]] = None ,
286+ validate_document : Optional [bool ] = None ,
277287) -> ExecutionResult :
278288 if custom_context_kwargs is None :
279289 custom_context_kwargs = {}
@@ -296,6 +306,7 @@ def execute_sync(
296306 allowed_operation_types ,
297307 validation_rules ,
298308 operation_name ,
309+ validate_document ,
299310 )
300311
301312 # with extensions_runner.executing():
@@ -344,6 +355,7 @@ async def subscribe(
344355 execution_context_class : Optional [type [ExecutionContext ]] = None ,
345356 operation_extensions : Optional [dict [str , Any ]] = None ,
346357 validation_rules : Optional [tuple [type [ASTValidationRule ], ...]] = None ,
358+ validate_document : Optional [bool ] = None ,
347359) -> AsyncGenerator [ExecutionResult , None ]:
348360 allowed_operation_types = {
349361 OperationType .SUBSCRIPTION ,
@@ -354,6 +366,7 @@ async def subscribe(
354366 allowed_operation_types ,
355367 validation_rules ,
356368 operation_name ,
369+ validate_document ,
357370 )
358371 return _subscribe_generator (
359372 schema ,
0 commit comments