@@ -391,239 +391,3 @@ def resolve_type(self, resolve_type_func, type_name, root, info, _type):
391391 return graphql_type
392392
393393 return type_
394-
395-
396- class UnforgivingExecutionContext (ExecutionContext ):
397- """An execution context which doesn't swallow exceptions.
398-
399- The only difference between this execution context and the one it inherits from is
400- that ``except Exception`` is commented out within ``resolve_field_value_or_error``.
401- By removing that exception handling, only ``GraphQLError``'s are caught.
402- """
403-
404- def resolve_field_value_or_error (
405- self , field_def , field_nodes , resolve_fn , source , info
406- ):
407- """Resolve field to a value or an error.
408-
409- Isolates the "ReturnOrAbrupt" behavior to not de-opt the resolve_field()
410- method. Returns the result of resolveFn or the abrupt-return Error object.
411-
412- For internal use only.
413- """
414- try :
415- # Build a dictionary of arguments from the field.arguments AST, using the
416- # variables scope to fulfill any variable references.
417- args = get_argument_values (field_def , field_nodes [0 ], self .variable_values )
418-
419- # Note that contrary to the JavaScript implementation, we pass the context
420- # value as part of the resolve info.
421- result = resolve_fn (source , info , ** args )
422- if self .is_awaitable (result ):
423- # noinspection PyShadowingNames
424- async def await_result ():
425- try :
426- return await result
427- except GraphQLError as error :
428- return error
429- # except Exception as error:
430- # return GraphQLError(str(error), original_error=error)
431-
432- # Yes, this is commented out code. It's been intentionally
433- # _not_ removed to show what has changed from the original
434- # implementation.
435-
436- return await_result ()
437- return result
438- except GraphQLError as error :
439- return error
440- # except Exception as error:
441- # return GraphQLError(str(error), original_error=error)
442-
443- # Yes, this is commented out code. It's been intentionally _not_
444- # removed to show what has changed from the original implementation.
445-
446- def complete_value_catching_error (
447- self , return_type , field_nodes , info , path , result
448- ):
449- """Complete a value while catching an error.
450-
451- This is a small wrapper around completeValue which detects and logs errors in
452- the execution context.
453- """
454- try :
455- if self .is_awaitable (result ):
456-
457- async def await_result ():
458- value = self .complete_value (
459- return_type , field_nodes , info , path , await result
460- )
461- if self .is_awaitable (value ):
462- return await value
463- return value
464-
465- completed = await_result ()
466- else :
467- completed = self .complete_value (
468- return_type , field_nodes , info , path , result
469- )
470- if self .is_awaitable (completed ):
471- # noinspection PyShadowingNames
472- async def await_completed ():
473- try :
474- return await completed
475-
476- # CHANGE WAS MADE HERE
477- # ``GraphQLError`` was swapped in for ``except Exception``
478- except GraphQLError as error :
479- self .handle_field_error (error , field_nodes , path , return_type )
480-
481- return await_completed ()
482- return completed
483-
484- # CHANGE WAS MADE HERE
485- # ``GraphQLError`` was swapped in for ``except Exception``
486- except GraphQLError as error :
487- self .handle_field_error (error , field_nodes , path , return_type )
488- return None
489-
490-
491- class Schema :
492- """Schema Definition.
493-
494- A Graphene Schema can execute operations (query, mutation, subscription) against the defined
495- types. For advanced purposes, the schema can be used to lookup type definitions and answer
496- questions about the types through introspection.
497-
498- Args:
499- query (Type[ObjectType]): Root query *ObjectType*. Describes entry point for fields to *read*
500- data in your Schema.
501- mutation (Optional[Type[ObjectType]]): Root mutation *ObjectType*. Describes entry point for
502- fields to *create, update or delete* data in your API.
503- subscription (Optional[Type[ObjectType]]): Root subscription *ObjectType*. Describes entry point
504- for fields to receive continuous updates.
505- types (Optional[List[Type[ObjectType]]]): List of any types to include in schema that
506- may not be introspected through root types.
507- directives (List[GraphQLDirective], optional): List of custom directives to include in the
508- GraphQL schema. Defaults to only include directives defined by GraphQL spec (@include
509- and @skip) [GraphQLIncludeDirective, GraphQLSkipDirective].
510- auto_camelcase (bool): Fieldnames will be transformed in Schema's TypeMap from snake_case
511- to camelCase (preferred by GraphQL standard). Default True.
512- """
513-
514- def __init__ (
515- self ,
516- query = None ,
517- mutation = None ,
518- subscription = None ,
519- types = None ,
520- directives = None ,
521- auto_camelcase = True ,
522- ):
523- self .query = query
524- self .mutation = mutation
525- self .subscription = subscription
526- type_map = TypeMap (
527- query , mutation , subscription , types , auto_camelcase = auto_camelcase
528- )
529- self .graphql_schema = GraphQLSchema (
530- type_map .query ,
531- type_map .mutation ,
532- type_map .subscription ,
533- type_map .types ,
534- directives ,
535- )
536-
537- def __str__ (self ):
538- return print_schema (self .graphql_schema )
539-
540- def __getattr__ (self , type_name ):
541- """
542- This function let the developer select a type in a given schema
543- by accessing its attrs.
544-
545- Example: using schema.Query for accessing the "Query" type in the Schema
546- """
547- _type = self .graphql_schema .get_type (type_name )
548- if _type is None :
549- raise AttributeError (f'Type "{ type_name } " not found in the Schema' )
550- if isinstance (_type , GrapheneGraphQLType ):
551- return _type .graphene_type
552- return _type
553-
554- def lazy (self , _type ):
555- return lambda : self .get_type (_type )
556-
557- def execute (self , * args , ** kwargs ):
558- """Execute a GraphQL query on the schema.
559-
560- Use the `graphql_sync` function from `graphql-core` to provide the result
561- for a query string. Most of the time this method will be called by one of the Graphene
562- :ref:`Integrations` via a web request.
563-
564- Args:
565- request_string (str or Document): GraphQL request (query, mutation or subscription)
566- as string or parsed AST form from `graphql-core`.
567- root_value (Any, optional): Value to use as the parent value object when resolving
568- root types.
569- context_value (Any, optional): Value to be made available to all resolvers via
570- `info.context`. Can be used to share authorization, dataloaders or other
571- information needed to resolve an operation.
572- variable_values (dict, optional): If variables are used in the request string, they can
573- be provided in dictionary form mapping the variable name to the variable value.
574- operation_name (str, optional): If multiple operations are provided in the
575- request_string, an operation name must be provided for the result to be provided.
576- middleware (List[SupportsGraphQLMiddleware]): Supply request level middleware as
577- defined in `graphql-core`.
578- execution_context_class (ExecutionContext, optional): The execution context class
579- to use when resolving queries and mutations.
580-
581- Returns:
582- :obj:`ExecutionResult` containing any data and errors for the operation.
583- """
584- kwargs = normalize_execute_kwargs (kwargs )
585- return graphql_sync (self .graphql_schema , * args , ** kwargs )
586-
587- async def execute_async (self , * args , ** kwargs ):
588- """Execute a GraphQL query on the schema asynchronously.
589-
590- Same as `execute`, but uses `graphql` instead of `graphql_sync`.
591- """
592- kwargs = normalize_execute_kwargs (kwargs )
593- return await graphql (self .graphql_schema , * args , ** kwargs )
594-
595- async def subscribe (self , query , * args , ** kwargs ):
596- """Execute a GraphQL subscription on the schema asynchronously."""
597- # Do parsing
598- try :
599- document = parse (query )
600- except GraphQLError as error :
601- return ExecutionResult (data = None , errors = [error ])
602-
603- # Do validation
604- validation_errors = validate (self .graphql_schema , document )
605- if validation_errors :
606- return ExecutionResult (data = None , errors = validation_errors )
607-
608- # Execute the query
609- kwargs = normalize_execute_kwargs (kwargs )
610- return await subscribe (self .graphql_schema , document , * args , ** kwargs )
611-
612- def introspect (self ):
613- introspection = self .execute (introspection_query )
614- if introspection .errors :
615- raise introspection .errors [0 ]
616- return introspection .data
617-
618-
619- def normalize_execute_kwargs (kwargs ):
620- """Replace alias names in keyword arguments for graphql()"""
621- if "root" in kwargs and "root_value" not in kwargs :
622- kwargs ["root_value" ] = kwargs .pop ("root" )
623- if "context" in kwargs and "context_value" not in kwargs :
624- kwargs ["context_value" ] = kwargs .pop ("context" )
625- if "variables" in kwargs and "variable_values" not in kwargs :
626- kwargs ["variable_values" ] = kwargs .pop ("variables" )
627- if "operation" in kwargs and "operation_name" not in kwargs :
628- kwargs ["operation_name" ] = kwargs .pop ("operation" )
629- return kwargs
0 commit comments