<s> from collections import OrderedDict , defaultdict <EOL> from . . error import GraphQLError <EOL> from . . language import ast <EOL> from . . type . definition import ( GraphQLArgument , GraphQLEnumType , <EOL> GraphQLEnumValue , GraphQLField , <EOL> GraphQLInputObjectField , GraphQLInputObjectType , <EOL> GraphQLInterfaceType , GraphQLList , <EOL> GraphQLNonNull , GraphQLObjectType , <EOL> GraphQLScalarType , GraphQLUnionType ) <EOL> from . . type . scalars import ( GraphQLBoolean , GraphQLFloat , GraphQLID , <EOL> GraphQLInt , GraphQLString ) <EOL> from . . type . schema import GraphQLSchema <EOL> from . value_from_ast import value_from_ast <EOL> def extend_schema ( schema , documentAST = None ) : <EOL> """<STR_LIT>""" <EOL> assert isinstance ( <EOL> schema , GraphQLSchema ) , '<STR_LIT>' <EOL> assert documentAST and isinstance ( <EOL> documentAST , ast . Document ) , '<STR_LIT>' <EOL> type_definition_map = { } <EOL> type_extensions_map = defaultdict ( list ) <EOL> for _def in documentAST . definitions : <EOL> if isinstance ( _def , ( <EOL> ast . ObjectTypeDefinition , <EOL> ast . InterfaceTypeDefinition , <EOL> ast . EnumTypeDefinition , <EOL> ast . UnionTypeDefinition , <EOL> ast . ScalarTypeDefinition , <EOL> ast . InputObjectTypeDefinition , <EOL> ) ) : <EOL> type_name = _def . name . value <EOL> if schema . get_type ( type_name ) : <EOL> raise GraphQLError ( <EOL> ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) . format ( type_name ) , <EOL> [ _def ] <EOL> ) <EOL> type_definition_map [ type_name ] = _def <EOL> elif isinstance ( _def , ast . TypeExtensionDefinition ) : <EOL> extended_type_name = _def . definition . name . value <EOL> existing_type = schema . get_type ( extended_type_name ) <EOL> if not existing_type : <EOL> raise GraphQLError ( <EOL> ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) . format ( extended_type_name ) , <EOL> [ _def . definition ] <EOL> ) <EOL> if not isinstance ( existing_type , GraphQLObjectType ) : <EOL> raise GraphQLError ( <EOL> '<STR_LIT>' . format ( <EOL> extended_type_name ) , <EOL> [ _def . definition ] <EOL> ) <EOL> type_extensions_map [ extended_type_name ] . append ( _def ) <EOL> def get_type_from_def ( type_def ) : <EOL> type = _get_named_type ( type_def . name ) <EOL> assert type , '<STR_LIT>' <EOL> return type <EOL> def get_type_from_AST ( astNode ) : <EOL> type = _get_named_type ( astNode . name . value ) <EOL> if not type : <EOL> raise GraphQLError ( <EOL> ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) . format ( <EOL> astNode . name . value ) , <EOL> [ astNode ] <EOL> ) <EOL> return type <EOL> def _get_named_type ( typeName ) : <EOL> cached_type_def = type_def_cache . get ( typeName ) <EOL> if cached_type_def : <EOL> return cached_type_def <EOL> existing_type = schema . get_type ( typeName ) <EOL> if existing_type : <EOL> type_def = extend_type ( existing_type ) <EOL> type_def_cache [ typeName ] = type_def <EOL> return type_def <EOL> type_ast = type_definition_map . get ( typeName ) <EOL> if type_ast : <EOL> type_def = build_type ( type_ast ) <EOL> type_def_cache [ typeName ] = type_def <EOL> return type_def <EOL> def extend_type ( type ) : <EOL> if isinstance ( type , GraphQLObjectType ) : <EOL> return extend_object_type ( type ) <EOL> if isinstance ( type , GraphQLInterfaceType ) : <EOL> return extend_interface_type ( type ) <EOL> if isinstance ( type , GraphQLUnionType ) : <EOL> return extend_union_type ( type ) <EOL> return type <EOL> def extend_object_type ( type ) : <EOL> return GraphQLObjectType ( <EOL> name = type . name , <EOL> description = type . description , <EOL> interfaces = lambda : extend_implemented_interfaces ( type ) , <EOL> fields = lambda : extend_field_map ( type ) , <EOL> ) <EOL> def extend_interface_type ( type ) : <EOL> return GraphQLInterfaceType ( <EOL> name = type . name , <EOL> description = type . description , <EOL> fields = lambda : extend_field_map ( type ) , <EOL> resolve_type = raise_client_schema_execution_error , <EOL> ) <EOL> def extend_union_type ( type ) : <EOL> return GraphQLUnionType ( <EOL> name = type . name , <EOL> description = type . description , <EOL> types = list ( map ( get_type_from_def , type . get_possible_types ( ) ) ) , <EOL> resolve_type = raise_client_schema_execution_error , <EOL> ) <EOL> def extend_implemented_interfaces ( type ) : <EOL> interfaces = list ( map ( get_type_from_def , type . get_interfaces ( ) ) ) <EOL> extensions = type_extensions_map [ type . name ] <EOL> for extension in extensions : <EOL> for namedType in extension . definition . interfaces : <EOL> interface_name = namedType . name . value <EOL> if any ( [ _def . name == interface_name for _def in interfaces ] ) : <EOL> raise GraphQLError ( <EOL> ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) . format ( <EOL> type . name , interface_name ) , <EOL> [ namedType ] <EOL> ) <EOL> interfaces . append ( get_type_from_AST ( namedType ) ) <EOL> return interfaces <EOL> def extend_field_map ( type ) : <EOL> new_field_map = OrderedDict ( ) <EOL> old_field_map = type . get_fields ( ) <EOL> for field_name , field in old_field_map . items ( ) : <EOL> new_field_map [ field_name ] = GraphQLField ( <EOL> extend_field_type ( field . type ) , <EOL> description = field . description , <EOL> deprecation_reason = field . deprecation_reason , <EOL> args = { arg . name : arg for arg in field . args } , <EOL> resolver = raise_client_schema_execution_error , <EOL> ) <EOL> extensions = type_extensions_map [ type . name ] <EOL> for extension in extensions : <EOL> for field in extension . definition . fields : <EOL> field_name = field . name . value <EOL> if field_name in old_field_map : <EOL> raise GraphQLError ( <EOL> ( '<STR_LIT>' + <EOL> '<STR_LIT>' ) . format ( <EOL> type . name , field_name ) , <EOL> [ field ] <EOL> ) <EOL> new_field_map [ field_name ] = GraphQLField ( <EOL> build_field_type ( field . type ) , <EOL> args = build_input_values ( field . arguments ) , <EOL> resolver = raise_client_schema_execution_error , <EOL> ) <EOL> return new_field_map <EOL> def extend_field_type ( type ) : <EOL> if isinstance ( type , GraphQLList ) : <EOL> return GraphQLList ( extend_field_type ( type . of_type ) ) <EOL> if isinstance ( type , GraphQLNonNull ) : <EOL> return GraphQLNonNull ( extend_field_type ( type . of_type ) ) <EOL> return get_type_from_def ( type ) <EOL> def build_type ( type_ast ) : <EOL> _type_build = { <EOL> ast . ObjectTypeDefinition : build_object_type , <EOL> ast . InterfaceTypeDefinition : build_interface_type , <EOL> ast . UnionTypeDefinition : build_union_type , <EOL> ast . ScalarTypeDefinition : build_scalar_type , <EOL> ast . EnumTypeDefinition : build_enum_type , <EOL> ast . InputObjectTypeDefinition : build_input_object_type <EOL> } <EOL> func = _type_build . get ( type ( type_ast ) ) <EOL> if func : <EOL> return func ( type_ast ) <EOL> def build_object_type ( type_ast ) : <EOL> return GraphQLObjectType ( <EOL> type_ast . name . value , <EOL> interfaces = lambda : build_implemented_interfaces ( type_ast ) , <EOL> fields = lambda : build_field_map ( type_ast ) , <EOL> ) <EOL> def build_interface_type ( type_ast ) : <EOL> return GraphQLInterfaceType ( <EOL> type_ast . name . value , <EOL> fields = lambda : build_field_map ( type_ast ) , <EOL> resolve_type = raise_client_schema_execution_error , <EOL> ) <EOL> def build_union_type ( type_ast ) : <EOL> return GraphQLUnionType ( <EOL> type_ast . name . value , <EOL> types = list ( map ( get_type_from_AST , type_ast . types ) ) , <EOL> resolve_type = raise_client_schema_execution_error , <EOL> ) <EOL> def build_scalar_type ( type_ast ) : <EOL> return GraphQLScalarType ( <EOL> type_ast . name . value , <EOL> serialize = lambda * args , ** kwargs : None , <EOL> parse_value = lambda * args , ** kwargs : False , <EOL> parse_literal = lambda * args , ** kwargs : False , <EOL> ) <EOL> def build_enum_type ( type_ast ) : <EOL> return GraphQLEnumType ( <EOL> type_ast . name . value , <EOL> values = { v . name . value : GraphQLEnumValue ( ) for v in type_ast . values } , <EOL> ) <EOL> def build_input_object_type ( type_ast ) : <EOL> return GraphQLInputObjectType ( <EOL> type_ast . name . value , <EOL> fields = lambda : build_input_values ( <EOL> type_ast . fields , GraphQLInputObjectField ) , <EOL> ) <EOL> def build_implemented_interfaces ( type_ast ) : <EOL> return list ( map ( get_type_from_AST , type_ast . interfaces ) ) <EOL> def build_field_map ( type_ast ) : <EOL> return { <EOL> field . name . value : GraphQLField ( <EOL> build_field_type ( field . type ) , <EOL> args = build_input_values ( field . arguments ) , <EOL> resolver = raise_client_schema_execution_error , <EOL> ) for field in type_ast . fields <EOL> } <EOL> def build_input_values ( values , input_type = GraphQLArgument ) : <EOL> input_values = OrderedDict ( ) <EOL> for value in values : <EOL> type = build_field_type ( value . type ) <EOL> input_values [ value . name . value ] = input_type ( <EOL> type , <EOL> default_value = value_from_ast ( value . default_value , type ) <EOL> ) <EOL> return input_values <EOL> def build_field_type ( type_ast ) : <EOL> if isinstance ( type_ast , ast . ListType ) : <EOL> return GraphQLList ( build_field_type ( type_ast . type ) ) <EOL> if isinstance ( type_ast , ast . NonNullType ) : <EOL> return GraphQLNonNull ( build_field_type ( type_ast . type ) ) <EOL> return get_type_from_AST ( type_ast ) <EOL> if not type_extensions_map and not type_definition_map : <EOL> return schema <EOL> type_def_cache = { <EOL> '<STR_LIT>' : GraphQLString , <EOL> '<STR_LIT>' : GraphQLInt , <EOL> '<STR_LIT>' : GraphQLFloat , <EOL> '<STR_LIT>' : GraphQLBoolean , <EOL> '<STR_LIT>' : GraphQLID , <EOL> } <EOL> query_type = get_type_from_def ( schema . get_query_type ( ) ) <EOL> existing_mutation_type = schema . get_mutation_type ( ) <EOL> mutationType = existing_mutation_type and get_type_from_def ( <EOL> existing_mutation_type ) or None <EOL> existing_subscription_type = schema . get_subscription_type ( ) <EOL> subscription_type = existing_subscription_type and get_type_from_def ( <EOL> existing_subscription_type ) or None <EOL> for typeName , _def in schema . get_type_map ( ) . items ( ) : <EOL> get_type_from_def ( _def ) <EOL> for typeName , _def in type_definition_map . items ( ) : <EOL> get_type_from_AST ( _def ) <EOL> return GraphQLSchema ( <EOL> query = query_type , <EOL> mutation = mutationType , <EOL> subscription = subscription_type , <EOL> directives = schema . get_directives ( ) , <EOL> ) <EOL> def raise_client_schema_execution_error ( * args , ** kwargs ) : <EOL> raise Exception ( '<STR_LIT>' ) </s>
