signature
stringlengths
8
3.44k
body
stringlengths
0
1.41M
docstring
stringlengths
1
122k
id
stringlengths
5
17
def setUp(self):
self.maxDiff = None<EOL>self.schema = get_schema()<EOL>_, sqlalchemy_metadata = get_animal_schema_sql_metadata()<EOL>self.sql_metadata = SqlMetadata(sqlite.dialect.name, sqlalchemy_metadata)<EOL>
Disable max diff limits for all tests.
f12632:c0:m0
def get_function_names_from_module(module):
return {<EOL>member<EOL>for member, member_type in getmembers(module)<EOL>if isfunction(member_type)<EOL>}<EOL>
Return a set of function names present in a given module.
f12633:m0
def load_schema(client):
project_root = path.dirname(path.dirname(path.abspath(__file__)))<EOL>file_path = path.join(project_root, '<STR_LIT>')<EOL>sql_files = glob(file_path)<EOL>if len(sql_files) > <NUM_LIT:1>:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>')<EOL><DEDENT>if len(sql_files) == <NUM_LIT:0> or sql_files[<NUM_LIT:0>] != file_path:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>')<EOL><DEDENT>with open(file_path, '<STR_LIT:r>') as update_file:<EOL><INDENT>for line in update_file:<EOL><INDENT>sanitized = line.strip()<EOL>if len(sanitized) == <NUM_LIT:0> or sanitized[<NUM_LIT:0>] == '<STR_LIT:#>':<EOL><INDENT>continue<EOL><DEDENT>client.command(sanitized)<EOL><DEDENT><DEDENT>
Read the schema file and apply the specified SQL updates to the client.
f12635:m0
def generate_orient_snapshot_data(client):
project_root = path.dirname(path.abspath(__file__))<EOL>sql_files = glob(path.join(project_root, '<STR_LIT>'))<EOL>_load_sql_files_to_orient_client(client, sql_files)<EOL>
Create OrientDB test DB from the SQL commands file for snapshot testing.
f12636:m0
def generate_orient_integration_data(client):
project_root = path.dirname(path.abspath(__file__))<EOL>sql_files = glob(path.join(project_root, '<STR_LIT>'))<EOL>_load_sql_files_to_orient_client(client, sql_files)<EOL>
Create OrientDB test DB from the SQL commands file for snapshot testing.
f12636:m1
def _load_sql_files_to_orient_client(client, sql_files):
for filepath in sql_files:<EOL><INDENT>with open(filepath) as f:<EOL><INDENT>for command in f.readlines():<EOL><INDENT>sanitized_command = command.strip()<EOL>if len(sanitized_command) == <NUM_LIT:0> or sanitized_command[<NUM_LIT:0>] == '<STR_LIT:#>':<EOL><INDENT>continue<EOL><DEDENT>client.command(sanitized_command)<EOL><DEDENT><DEDENT><DEDENT>
Load list of supplied SQL files into the supplied OrientDB client.
f12636:m2
def get_animal_schema_sql_metadata():
metadata = MetaData()<EOL>animal_table = Table(<EOL>'<STR_LIT>',<EOL>metadata,<EOL>Column('<STR_LIT>', String(<NUM_LIT>), primary_key=True),<EOL>Column('<STR_LIT:name>', String(length=<NUM_LIT:12>), nullable=False),<EOL>Column('<STR_LIT>', Numeric, nullable=False),<EOL>Column('<STR_LIT>', Date, nullable=False),<EOL>)<EOL>event_table = Table(<EOL>'<STR_LIT>',<EOL>metadata,<EOL>Column('<STR_LIT>', String(<NUM_LIT>), primary_key=True),<EOL>Column('<STR_LIT>', DateTime, nullable=False),<EOL>)<EOL>entity_table = Table(<EOL>'<STR_LIT>',<EOL>metadata,<EOL>Column('<STR_LIT>', String(<NUM_LIT>), primary_key=True),<EOL>Column('<STR_LIT:name>', String(length=<NUM_LIT:12>), nullable=False),<EOL>)<EOL>table_name_to_table = {<EOL>animal_table.name: animal_table,<EOL>event_table.name: event_table,<EOL>entity_table.name: entity_table,<EOL>}<EOL>return table_name_to_table, metadata<EOL>
Return Dict[str, Table] table lookup, and associated metadata, for the Animal test schema.
f12636:m6
def get_orientdb_url(database_name):
template = '<STR_LIT>'<EOL>return template.format(ORIENTDB_SERVER, database_name)<EOL>
Return an OrientDB path for the specified database on the ORIENTDB_SERVER.
f12638:m0
def recursive_field_type_is_subtype_of_parent_field():
graphql_input = '''<STR_LIT>'''<EOL>expected_output_metadata = {<EOL>'<STR_LIT>': OutputMetadata(type=GraphQLString, optional=False),<EOL>}<EOL>expected_input_metadata = {}<EOL>type_equivalence_hints = {<EOL>'<STR_LIT>': '<STR_LIT>',<EOL>}<EOL>return CommonTestData(<EOL>graphql_input=graphql_input,<EOL>expected_output_metadata=expected_output_metadata,<EOL>expected_input_metadata=expected_input_metadata,<EOL>type_equivalence_hints=type_equivalence_hints)<EOL>
Ensure that recursion is allowed along an edge linked to a supertype of the parent field.
f12640:m98
def transform(emitted_output):
return WHITESPACE_PATTERN.sub(u'<STR_LIT>', emitted_output)<EOL>
Transform emitted_output into a unique representation, regardless of lines / indentation.
f12641:m0
def _get_mismatch_message(expected_blocks, received_blocks):
pretty_expected = pformat(expected_blocks)<EOL>pretty_received = pformat(received_blocks)<EOL>return u'<STR_LIT>'.format(pretty_expected, pretty_received)<EOL>
Create a well-formated error message indicating that two lists of blocks are mismatched.
f12641:m1
def get_schema():
ast = parse(SCHEMA_TEXT)<EOL>schema = build_ast_schema(ast)<EOL>return schema<EOL>
Get a schema object for testing.
f12641:m8
def generate_schema_graph(graph_client):
schema_records = graph_client.command(ORIENTDB_SCHEMA_RECORDS_QUERY)<EOL>schema_data = [x.oRecordData for x in schema_records]<EOL>return SchemaGraph(schema_data)<EOL>
Generate SchemaGraph from a pyorient client
f12641:m9
def generate_schema(graph_client, class_to_field_type_overrides=None, hidden_classes=None):
schema_records = graph_client.command(ORIENTDB_SCHEMA_RECORDS_QUERY)<EOL>schema_data = [x.oRecordData for x in schema_records]<EOL>return get_graphql_schema_from_orientdb_schema_data(schema_data, class_to_field_type_overrides,<EOL>hidden_classes)<EOL>
Generate schema and type equivalence dict from a pyorient client
f12641:m10
def construct_location_types(location_types_as_strings):
schema = get_schema()<EOL>return {<EOL>location: schema.get_type(type_name)<EOL>for location, type_name in six.iteritems(location_types_as_strings)<EOL>}<EOL>
Convert the supplied dict into a proper location_types dict with GraphQL types as values.
f12641:m11
def setUp(self):
self.schema = get_schema()<EOL>
Initialize the test schema once for all tests.
f12642:c0:m0
def convert_decimals_to_strings(value):
if isinstance(value, tuple):<EOL><INDENT>return tuple(str(element) for element in value)<EOL><DEDENT>elif isinstance(value, Decimal):<EOL><INDENT>return str(value)<EOL><DEDENT>elif isinstance(value, six.string_types):<EOL><INDENT>return value<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(type(value).__name__, value))<EOL><DEDENT>
Convert all decimals to strings in the given scalar or tuple.
f12643:m0
def setUp(self):
self.maxDiff = None<EOL>self.schema = get_schema()<EOL>
Initialize the test schema once for all tests, and disable max diff limits.
f12643:c0:m0
def setUp(self):
self.maxDiff = None<EOL>self.schema = get_schema()<EOL>
Initialize the test schema once for all tests, and disable max diff limits.
f12643:c1:m0
def sort_db_results(results):
sort_order = []<EOL>if len(results) > <NUM_LIT:0>:<EOL><INDENT>sort_order = sorted(six.iterkeys(results[<NUM_LIT:0>]))<EOL><DEDENT>def sort_key(result):<EOL><INDENT>"""<STR_LIT>"""<EOL>return tuple((result[col] is not None, result[col]) for col in sort_order)<EOL><DEDENT>return sorted(results, key=sort_key)<EOL>
Deterministically sort DB results. Args: results: List[Dict], results from a DB. Returns: List[Dict], sorted DB results.
f12644:m0
def try_convert_decimal_to_string(value):
if isinstance(value, list):<EOL><INDENT>return [try_convert_decimal_to_string(subvalue) for subvalue in value]<EOL><DEDENT>if isinstance(value, Decimal):<EOL><INDENT>return str(value)<EOL><DEDENT>return value<EOL>
Return Decimals as string if value is a Decimal, return value otherwise.
f12644:m1
def compile_and_run_match_query(schema, graphql_query, parameters, graph_client):
<EOL>converted_parameters = {<EOL>name: try_convert_decimal_to_string(value)<EOL>for name, value in six.iteritems(parameters)<EOL>}<EOL>compilation_result = graphql_to_match(schema, graphql_query, converted_parameters)<EOL>query = compilation_result.query<EOL>results = [row.oRecordData for row in graph_client.command(query)]<EOL>return results<EOL>
Compiles and runs a MATCH query against the supplied graph client.
f12644:m2
def compile_and_run_sql_query(schema, graphql_query, parameters, engine, metadata):
dialect_name = engine.dialect.name<EOL>sql_metadata = SqlMetadata(dialect_name, metadata)<EOL>compilation_result = graphql_to_sql(schema, graphql_query, parameters, sql_metadata, None)<EOL>query = compilation_result.query<EOL>results = []<EOL>connection = engine.connect()<EOL>with connection.begin() as trans:<EOL><INDENT>for result in connection.execute(query):<EOL><INDENT>results.append(dict(result))<EOL><DEDENT>trans.rollback()<EOL><DEDENT>return results<EOL>
Compiles and runs a SQL query against the supplied SQL backend.
f12644:m3
@classmethod<EOL><INDENT>def setUpClass(cls):<DEDENT>
cls.maxDiff = None<EOL>cls.schema = get_schema()<EOL>
Initialize the test schema once for all tests, and disable max diff limits.
f12646:c0:m0
def assertResultsEqual(self, graphql_query, parameters, backend_name, expected_results):
backend_results = self.compile_and_run_query(graphql_query, parameters, backend_name)<EOL>try:<EOL><INDENT>self.assertListEqual(sort_db_results(expected_results),<EOL>sort_db_results(backend_results))<EOL><DEDENT>except AssertionError as error:<EOL><INDENT>args = [u'<STR_LIT>'.format(backend_name, error.args[<NUM_LIT:0>])]<EOL>args.extend(error.args[<NUM_LIT:1>:])<EOL>error.args = tuple(args)<EOL>raise<EOL><DEDENT>
Assert that two lists of DB results are equal, independent of order.
f12646:c0:m1
@classmethod<EOL><INDENT>def compile_and_run_query(cls, graphql_query, parameters, backend_name):<DEDENT>
if backend_name in SQL_BACKENDS:<EOL><INDENT>engine = cls.sql_backend_name_to_engine[backend_name]<EOL>results = compile_and_run_sql_query(<EOL>cls.schema, graphql_query, parameters, engine, cls.sql_metadata)<EOL><DEDENT>elif backend_name in MATCH_BACKENDS:<EOL><INDENT>results = compile_and_run_match_query(<EOL>cls.schema, graphql_query, parameters, cls.graph_client)<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'.format(backend_name))<EOL><DEDENT>return results<EOL>
Compiles and runs the graphql query with the supplied parameters against all backends. Args: graphql_query: str, GraphQL query string to run against every backend. parameters: Dict[str, Any], input parameters to the query. backend_name: str, the name of the test backend to get results from. Returns: List[Dict[str, Any]], backend results as a list of dictionaries.
f12646:c0:m2
def setUp(self):
self.maxDiff = None<EOL>
Disable max diff limits for all tests.
f12648:c0:m0
def setUp(self):
self.maxDiff = None<EOL>
Disable max diff limits for all tests.
f12648:c1:m0
def _unused_function(*args, **kwargs):
raise NotImplementedError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(args, kwargs))<EOL>
Must not be called. Placeholder for functions that are required but aren't used.
f12649:m0
def _serialize_date(value):
if not isinstance(value, date):<EOL><INDENT>raise ValueError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(type(value), value))<EOL><DEDENT>return value.isoformat()<EOL>
Serialize a Date object to its proper ISO-8601 representation.
f12649:m1
def _parse_date_value(value):
return arrow.get(value, '<STR_LIT>').date()<EOL>
Deserialize a Date object from its proper ISO-8601 representation.
f12649:m2
def _serialize_datetime(value):
if not isinstance(value, (datetime, arrow.Arrow)):<EOL><INDENT>raise ValueError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(type(value), value))<EOL><DEDENT>return value.isoformat()<EOL>
Serialize a DateTime object to its proper ISO-8601 representation.
f12649:m3
def _parse_datetime_value(value):
if value.endswith('<STR_LIT>'):<EOL><INDENT>value = value[:-<NUM_LIT:1>] + '<STR_LIT>'<EOL><DEDENT>return arrow.get(value, '<STR_LIT>').datetime<EOL>
Deserialize a DateTime object from its proper ISO-8601 representation.
f12649:m4
def is_meta_field(field_name):
return field_name in ALL_SUPPORTED_META_FIELDS<EOL>
Return True if the field is considered a meta field in the schema, and False otherwise.
f12649:m5
def insert_meta_fields_into_existing_schema(graphql_schema):
root_type_name = graphql_schema.get_query_type().name<EOL>for type_name, type_obj in six.iteritems(graphql_schema.get_type_map()):<EOL><INDENT>if type_name.startswith('<STR_LIT>') or type_name == root_type_name:<EOL><INDENT>continue<EOL><DEDENT>if not isinstance(type_obj, (GraphQLObjectType, GraphQLInterfaceType)):<EOL><INDENT>continue<EOL><DEDENT>for meta_field_name, meta_field in six.iteritems(EXTENDED_META_FIELD_DEFINITIONS):<EOL><INDENT>if meta_field_name in type_obj.fields:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(meta_field_name))<EOL><DEDENT>type_obj.fields[meta_field_name] = meta_field<EOL><DEDENT><DEDENT>
Add compiler-specific meta-fields into all interfaces and types of the specified schema. It is preferable to use the EXTENDED_META_FIELD_DEFINITIONS constant above to directly inject the meta-fields during the initial process of building the schema, as that approach is more robust. This function does its best to not mutate unexpected definitions, but may break unexpectedly as the GraphQL standard is extended and the underlying GraphQL library is updated. Use this function at your own risk. Don't say you haven't been warned. Properties added include: - "_x_count", which allows filtering folds based on the number of elements they capture. Args: graphql_schema: GraphQLSchema object describing the schema that is going to be used with the compiler. N.B.: MUTATED IN-PLACE in this method.
f12649:m6
def validate_supported_property_type_id(property_name, property_type_id):
if property_type_id not in PROPERTY_TYPE_ID_TO_NAME:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(property_name, property_type_id))<EOL><DEDENT>
Ensure that the given property type_id is supported by the graph.
f12650:m0
def _parse_bool_default_value(property_name, default_value_string):
lowercased_value_string = default_value_string.lower()<EOL>if lowercased_value_string in {'<STR_LIT:0>', '<STR_LIT:false>'}:<EOL><INDENT>return False<EOL><DEDENT>elif lowercased_value_string in {'<STR_LIT:1>', '<STR_LIT:true>'}:<EOL><INDENT>return True<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(property_name, default_value_string))<EOL><DEDENT>
Parse and return the default value for a boolean property.
f12650:m1
def _parse_datetime_default_value(property_name, default_value_string):
<EOL>parsed_value = time.strptime(default_value_string, ORIENTDB_DATETIME_FORMAT)<EOL>return datetime.datetime(<EOL>parsed_value.tm_year, parsed_value.tm_mon, parsed_value.tm_mday,<EOL>parsed_value.tm_hour, parsed_value.tm_min, parsed_value.tm_sec, <NUM_LIT:0>, None)<EOL>
Parse and return the default value for a datetime property.
f12650:m2
def _parse_date_default_value(property_name, default_value_string):
<EOL>parsed_value = time.strptime(default_value_string, ORIENTDB_DATE_FORMAT)<EOL>return datetime.date(parsed_value.tm_year, parsed_value.tm_mon, parsed_value.tm_mday)<EOL>
Parse and return the default value for a date property.
f12650:m3
def parse_default_property_value(property_name, property_type_id, default_value_string):
if property_type_id == PROPERTY_TYPE_EMBEDDED_SET_ID and default_value_string == '<STR_LIT:{}>':<EOL><INDENT>return set()<EOL><DEDENT>elif property_type_id == PROPERTY_TYPE_EMBEDDED_LIST_ID and default_value_string == '<STR_LIT>':<EOL><INDENT>return list()<EOL><DEDENT>elif (property_type_id == PROPERTY_TYPE_STRING_ID and<EOL>isinstance(default_value_string, six.string_types)):<EOL><INDENT>return default_value_string<EOL><DEDENT>elif property_type_id == PROPERTY_TYPE_BOOLEAN_ID:<EOL><INDENT>return _parse_bool_default_value(property_name, default_value_string)<EOL><DEDENT>elif property_type_id == PROPERTY_TYPE_DATETIME_ID:<EOL><INDENT>return _parse_datetime_default_value(property_name, default_value_string)<EOL><DEDENT>elif property_type_id == PROPERTY_TYPE_DATE_ID:<EOL><INDENT>return _parse_date_default_value(property_name, default_value_string)<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(property_name, property_type_id, default_value_string))<EOL><DEDENT>
Parse the default value string into its proper form given the property type ID. Args: property_name: string, the name of the property whose default value is being parsed. Used primarily to construct meaningful error messages, should the default value prove invalid. property_type_id: int, one of the property type ID constants defined in this file that OrientDB uses to designate the native type of a given property. default_value_string: string, the textual representation of the default value for for the property, as returned by OrientDB's schema introspection code. Returns: an object of type matching the property that can be used as the property's default value. For example, if the property is of string type, the return type will be a string, and if the property is of list type, the return type will be a list. Raises: AssertionError, if the default value is not supported or does not match the property's declared type (e.g. if a default of "[]" is set on an integer property).
f12650:m4
def _get_referenced_type_equivalences(graphql_types, type_equivalence_hints):
referenced_types = set()<EOL>for graphql_type in graphql_types.values():<EOL><INDENT>if isinstance(graphql_type, (GraphQLObjectType, GraphQLInterfaceType)):<EOL><INDENT>for _, field in graphql_type.fields.items():<EOL><INDENT>if isinstance(field.type, GraphQLList):<EOL><INDENT>referenced_types.add(field.type.of_type.name)<EOL><DEDENT><DEDENT><DEDENT><DEDENT>return {<EOL>original: union<EOL>for original, union in type_equivalence_hints.items()<EOL>if union.name in referenced_types<EOL>}<EOL>
Filter union types with no edges from the type equivalence hints dict.
f12651:m0
def _get_inherited_field_types(class_to_field_type_overrides, schema_graph):
inherited_field_type_overrides = dict()<EOL>for superclass_name, field_type_overrides in class_to_field_type_overrides.items():<EOL><INDENT>for subclass_name in schema_graph.get_subclass_set(superclass_name):<EOL><INDENT>inherited_field_type_overrides.setdefault(subclass_name, dict())<EOL>inherited_field_type_overrides[subclass_name].update(field_type_overrides)<EOL><DEDENT><DEDENT>return inherited_field_type_overrides<EOL>
Return a dictionary describing the field type overrides in subclasses.
f12651:m1
def _validate_overriden_fields_are_not_defined_in_superclasses(class_to_field_type_overrides,<EOL>schema_graph):
for class_name, field_type_overrides in six.iteritems(class_to_field_type_overrides):<EOL><INDENT>for superclass_name in schema_graph.get_inheritance_set(class_name):<EOL><INDENT>if superclass_name != class_name:<EOL><INDENT>superclass = schema_graph.get_element_by_class_name(superclass_name)<EOL>for field_name in field_type_overrides:<EOL><INDENT>if field_name in superclass.properties:<EOL><INDENT>raise AssertionError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(field_name, class_name, superclass_name))<EOL><DEDENT><DEDENT><DEDENT><DEDENT><DEDENT>
Assert that the fields we want to override are not defined in superclasses.
f12651:m2
def _property_descriptor_to_graphql_type(property_obj):
property_type = property_obj.type_id<EOL>scalar_types = {<EOL>PROPERTY_TYPE_BOOLEAN_ID: GraphQLBoolean,<EOL>PROPERTY_TYPE_DATE_ID: GraphQLDate,<EOL>PROPERTY_TYPE_DATETIME_ID: GraphQLDateTime,<EOL>PROPERTY_TYPE_DECIMAL_ID: GraphQLDecimal,<EOL>PROPERTY_TYPE_DOUBLE_ID: GraphQLFloat,<EOL>PROPERTY_TYPE_FLOAT_ID: GraphQLFloat,<EOL>PROPERTY_TYPE_INTEGER_ID: GraphQLInt,<EOL>PROPERTY_TYPE_STRING_ID: GraphQLString,<EOL>}<EOL>result = scalar_types.get(property_type, None)<EOL>if result:<EOL><INDENT>return result<EOL><DEDENT>mapping_types = {<EOL>PROPERTY_TYPE_EMBEDDED_SET_ID: GraphQLList,<EOL>PROPERTY_TYPE_EMBEDDED_LIST_ID: GraphQLList,<EOL>}<EOL>wrapping_type = mapping_types.get(property_type, None)<EOL>if wrapping_type:<EOL><INDENT>linked_property_obj = property_obj.qualifier<EOL>if linked_property_obj in scalar_types:<EOL><INDENT>return wrapping_type(scalar_types[linked_property_obj])<EOL><DEDENT><DEDENT>return None<EOL>
Return the best GraphQL type representation for an OrientDB property descriptor.
f12651:m3
def _get_union_type_name(type_names_to_union):
if not type_names_to_union:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(type_names_to_union))<EOL><DEDENT>return u'<STR_LIT>' + u'<STR_LIT>'.join(sorted(type_names_to_union))<EOL>
Construct a unique union type name based on the type names being unioned.
f12651:m4
def _get_fields_for_class(schema_graph, graphql_types, field_type_overrides, hidden_classes,<EOL>cls_name):
properties = schema_graph.get_element_by_class_name(cls_name).properties<EOL>all_properties = {<EOL>property_name: _property_descriptor_to_graphql_type(property_obj)<EOL>for property_name, property_obj in six.iteritems(properties)<EOL>}<EOL>result = {<EOL>property_name: graphql_representation<EOL>for property_name, graphql_representation in six.iteritems(all_properties)<EOL>if graphql_representation is not None<EOL>}<EOL>schema_element = schema_graph.get_element_by_class_name(cls_name)<EOL>outbound_edges = (<EOL>('<STR_LIT>'.format(out_edge_name),<EOL>schema_graph.get_element_by_class_name(out_edge_name).properties[<EOL>EDGE_DESTINATION_PROPERTY_NAME].qualifier)<EOL>for out_edge_name in schema_element.out_connections<EOL>)<EOL>inbound_edges = (<EOL>('<STR_LIT>'.format(in_edge_name),<EOL>schema_graph.get_element_by_class_name(in_edge_name).properties[<EOL>EDGE_SOURCE_PROPERTY_NAME].qualifier)<EOL>for in_edge_name in schema_element.in_connections<EOL>)<EOL>for field_name, to_type_name in chain(outbound_edges, inbound_edges):<EOL><INDENT>edge_endpoint_type_name = None<EOL>subclasses = schema_graph.get_subclass_set(to_type_name)<EOL>to_type_abstract = schema_graph.get_element_by_class_name(to_type_name).abstract<EOL>if not to_type_abstract and len(subclasses) > <NUM_LIT:1>:<EOL><INDENT>type_names_to_union = [<EOL>subclass<EOL>for subclass in subclasses<EOL>if subclass not in hidden_classes<EOL>]<EOL>if type_names_to_union:<EOL><INDENT>edge_endpoint_type_name = _get_union_type_name(type_names_to_union)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>if to_type_name not in hidden_classes:<EOL><INDENT>edge_endpoint_type_name = to_type_name<EOL><DEDENT><DEDENT>if edge_endpoint_type_name is not None:<EOL><INDENT>result[field_name] = GraphQLList(graphql_types[edge_endpoint_type_name])<EOL><DEDENT><DEDENT>for field_name, field_type in six.iteritems(field_type_overrides):<EOL><INDENT>if field_name not in result:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(field_name, cls_name))<EOL><DEDENT>else:<EOL><INDENT>result[field_name] = field_type<EOL><DEDENT><DEDENT>return result<EOL>
Return a dict from field name to GraphQL field type, for the specified graph class.
f12651:m5
def _create_field_specification(schema_graph, graphql_types, field_type_overrides,<EOL>hidden_classes, cls_name):
def field_maker_func():<EOL><INDENT>"""<STR_LIT>"""<EOL>result = EXTENDED_META_FIELD_DEFINITIONS.copy()<EOL>result.update(OrderedDict([<EOL>(name, GraphQLField(value))<EOL>for name, value in sorted(six.iteritems(_get_fields_for_class(<EOL>schema_graph, graphql_types, field_type_overrides, hidden_classes, cls_name)),<EOL>key=lambda x: x[<NUM_LIT:0>])<EOL>]))<EOL>return result<EOL><DEDENT>return field_maker_func<EOL>
Return a function that specifies the fields present on the given type.
f12651:m6
def _create_interface_specification(schema_graph, graphql_types, hidden_classes, cls_name):
def interface_spec():<EOL><INDENT>"""<STR_LIT>"""<EOL>abstract_inheritance_set = (<EOL>superclass_name<EOL>for superclass_name in sorted(list(schema_graph.get_inheritance_set(cls_name)))<EOL>if (superclass_name not in hidden_classes and<EOL>schema_graph.get_element_by_class_name(superclass_name).abstract)<EOL>)<EOL>return [<EOL>graphql_types[x]<EOL>for x in abstract_inheritance_set<EOL>if x not in hidden_classes<EOL>]<EOL><DEDENT>return interface_spec<EOL>
Return a function that specifies the interfaces implemented by the given type.
f12651:m7
def _create_union_types_specification(schema_graph, graphql_types, hidden_classes, base_name):
<EOL>def types_spec():<EOL><INDENT>"""<STR_LIT>"""<EOL>return [<EOL>graphql_types[x]<EOL>for x in sorted(list(schema_graph.get_subclass_set(base_name)))<EOL>if x not in hidden_classes<EOL>]<EOL><DEDENT>return types_spec<EOL>
Return a function that gives the types in the union type rooted at base_name.
f12651:m8
def get_graphql_schema_from_schema_graph(schema_graph, class_to_field_type_overrides,<EOL>hidden_classes):
_validate_overriden_fields_are_not_defined_in_superclasses(class_to_field_type_overrides,<EOL>schema_graph)<EOL>inherited_field_type_overrides = _get_inherited_field_types(class_to_field_type_overrides,<EOL>schema_graph)<EOL>if not schema_graph.get_element_by_class_name(ORIENTDB_BASE_VERTEX_CLASS_NAME).properties:<EOL><INDENT>hidden_classes.add(ORIENTDB_BASE_VERTEX_CLASS_NAME)<EOL><DEDENT>graphql_types = OrderedDict()<EOL>type_equivalence_hints = OrderedDict()<EOL>for vertex_cls_name in sorted(schema_graph.vertex_class_names):<EOL><INDENT>vertex_cls = schema_graph.get_element_by_class_name(vertex_cls_name)<EOL>if vertex_cls_name in hidden_classes:<EOL><INDENT>continue<EOL><DEDENT>inherited_field_type_overrides.setdefault(vertex_cls_name, dict())<EOL>field_type_overrides = inherited_field_type_overrides[vertex_cls_name]<EOL>field_specification_lambda = _create_field_specification(<EOL>schema_graph, graphql_types, field_type_overrides, hidden_classes, vertex_cls_name)<EOL>current_graphql_type = None<EOL>if vertex_cls.abstract:<EOL><INDENT>current_graphql_type = GraphQLInterfaceType(vertex_cls_name,<EOL>fields=field_specification_lambda)<EOL><DEDENT>else:<EOL><INDENT>interface_specification_lambda = _create_interface_specification(<EOL>schema_graph, graphql_types, hidden_classes, vertex_cls_name)<EOL>current_graphql_type = GraphQLObjectType(vertex_cls_name,<EOL>field_specification_lambda,<EOL>interfaces=interface_specification_lambda,<EOL>is_type_of=lambda: None)<EOL><DEDENT>graphql_types[vertex_cls_name] = current_graphql_type<EOL><DEDENT>for vertex_cls_name in sorted(schema_graph.vertex_class_names):<EOL><INDENT>vertex_cls = schema_graph.get_element_by_class_name(vertex_cls_name)<EOL>if vertex_cls_name in hidden_classes:<EOL><INDENT>continue<EOL><DEDENT>vertex_cls_subclasses = schema_graph.get_subclass_set(vertex_cls_name)<EOL>if not vertex_cls.abstract and len(vertex_cls_subclasses) > <NUM_LIT:1>:<EOL><INDENT>union_type_name = _get_union_type_name(vertex_cls_subclasses)<EOL>type_specification_lambda = _create_union_types_specification(<EOL>schema_graph, graphql_types, hidden_classes, vertex_cls_name)<EOL>union_type = GraphQLUnionType(union_type_name, types=type_specification_lambda)<EOL>graphql_types[union_type_name] = union_type<EOL>type_equivalence_hints[graphql_types[vertex_cls_name]] = union_type<EOL><DEDENT><DEDENT>for non_graph_cls_name in sorted(schema_graph.non_graph_class_names):<EOL><INDENT>if non_graph_cls_name in hidden_classes:<EOL><INDENT>continue<EOL><DEDENT>if not schema_graph.get_element_by_class_name(non_graph_cls_name).abstract:<EOL><INDENT>continue<EOL><DEDENT>cls_subclasses = schema_graph.get_subclass_set(non_graph_cls_name)<EOL>if len(cls_subclasses) > <NUM_LIT:1>:<EOL><INDENT>all_non_abstract_subclasses_are_vertices = True<EOL>for subclass_name in cls_subclasses:<EOL><INDENT>subclass = schema_graph.get_element_by_class_name(subclass_name)<EOL>if subclass_name != non_graph_cls_name:<EOL><INDENT>if not subclass.abstract and not subclass.is_vertex:<EOL><INDENT>all_non_abstract_subclasses_are_vertices = False<EOL>break<EOL><DEDENT><DEDENT><DEDENT>if all_non_abstract_subclasses_are_vertices:<EOL><INDENT>inherited_field_type_overrides.setdefault(non_graph_cls_name, dict())<EOL>field_type_overrides = inherited_field_type_overrides[non_graph_cls_name]<EOL>field_specification_lambda = _create_field_specification(<EOL>schema_graph, graphql_types, field_type_overrides, hidden_classes,<EOL>non_graph_cls_name)<EOL>graphql_type = GraphQLInterfaceType(non_graph_cls_name,<EOL>fields=field_specification_lambda)<EOL>graphql_types[non_graph_cls_name] = graphql_type<EOL><DEDENT><DEDENT><DEDENT>if not graphql_types:<EOL><INDENT>raise EmptySchemaError(u'<STR_LIT>'<EOL>u'<STR_LIT>')<EOL><DEDENT>RootSchemaQuery = GraphQLObjectType('<STR_LIT>', OrderedDict([<EOL>(name, GraphQLField(value))<EOL>for name, value in sorted(six.iteritems(graphql_types), key=lambda x: x[<NUM_LIT:0>])<EOL>if not isinstance(value, GraphQLUnionType)<EOL>]))<EOL>schema = GraphQLSchema(RootSchemaQuery, directives=DIRECTIVES)<EOL>return schema, _get_referenced_type_equivalences(graphql_types,<EOL>type_equivalence_hints)<EOL>
Return a GraphQL schema object corresponding to the schema of the given schema graph. Args: schema_graph: SchemaGraph class_to_field_type_overrides: dict, class name -> {field name -> field type}, (string -> {string -> GraphQLType}). Used to override the type of a field in the class where it's first defined and all the class's subclasses. hidden_classes: set of strings, classes to not include in the GraphQL schema. Returns: tuple of (GraphQL schema object, GraphQL type equivalence hints dict). The tuple is of type (GraphQLSchema, {GraphQLObjectType -> GraphQLUnionType}).
f12651:m9
def _validate_non_abstract_edge_has_defined_endpoint_types(class_name, properties):
edge_source = properties.get(EDGE_SOURCE_PROPERTY_NAME, None)<EOL>edge_destination = properties.get(EDGE_DESTINATION_PROPERTY_NAME, None)<EOL>has_defined_endpoint_types = all((<EOL>edge_source is not None and edge_source.type_id == PROPERTY_TYPE_LINK_ID,<EOL>edge_destination is not None and edge_destination.type_id == PROPERTY_TYPE_LINK_ID,<EOL>))<EOL>if not has_defined_endpoint_types:<EOL><INDENT>raise IllegalSchemaStateError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(class_name, properties))<EOL><DEDENT>
Validate that the non-abstract edge properties dict has defined in/out link properties.
f12653:m0
def _validate_non_edges_do_not_have_edge_like_properties(class_name, properties):
has_source = EDGE_SOURCE_PROPERTY_NAME in properties<EOL>has_destination = EDGE_DESTINATION_PROPERTY_NAME in properties<EOL>if has_source or has_destination:<EOL><INDENT>raise IllegalSchemaStateError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(class_name, properties))<EOL><DEDENT>for property_name, property_descriptor in six.iteritems(properties):<EOL><INDENT>if property_descriptor.type_id == PROPERTY_TYPE_LINK_ID:<EOL><INDENT>raise IllegalSchemaStateError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(class_name, property_name))<EOL><DEDENT><DEDENT>
Validate that non-edges do not have the in/out properties defined.
f12653:m1
def _validate_edges_do_not_have_extra_links(class_name, properties):
for property_name, property_descriptor in six.iteritems(properties):<EOL><INDENT>if property_name in {EDGE_SOURCE_PROPERTY_NAME, EDGE_DESTINATION_PROPERTY_NAME}:<EOL><INDENT>continue<EOL><DEDENT>if property_descriptor.type_id == PROPERTY_TYPE_LINK_ID:<EOL><INDENT>raise IllegalSchemaStateError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(class_name, property_name))<EOL><DEDENT><DEDENT>
Validate that edges do not have properties of Link type that aren't the edge endpoints.
f12653:m2
def _validate_property_names(class_name, properties):
for property_name in properties:<EOL><INDENT>if not property_name or property_name.startswith(ILLEGAL_PROPERTY_NAME_PREFIXES):<EOL><INDENT>raise IllegalSchemaStateError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(class_name, property_name))<EOL><DEDENT><DEDENT>
Validate that properties do not have names that may cause problems in the GraphQL schema.
f12653:m3
def _validate_collections_have_default_values(class_name, property_name, property_descriptor):
<EOL>if property_descriptor.type_id in COLLECTION_PROPERTY_TYPES:<EOL><INDENT>if property_descriptor.default is None:<EOL><INDENT>raise IllegalSchemaStateError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(class_name, property_name))<EOL><DEDENT><DEDENT>
Validate that if the property is of collection type, it has a specified default value.
f12653:m4
def get_superclasses_from_class_definition(class_definition):
<EOL>superclasses = class_definition.get('<STR_LIT>', None)<EOL>if superclasses:<EOL><INDENT>return list(superclasses)<EOL><DEDENT>superclass = class_definition.get('<STR_LIT>', None)<EOL>if superclass:<EOL><INDENT>return [superclass]<EOL><DEDENT>return []<EOL>
Extract a list of all superclass names from a class definition dict.
f12653:m5
def __init__(self, class_name, kind, abstract, properties, class_fields):
if kind == SchemaElement.ELEMENT_KIND_EDGE:<EOL><INDENT>_validate_edges_do_not_have_extra_links(class_name, properties)<EOL>if not abstract:<EOL><INDENT>_validate_non_abstract_edge_has_defined_endpoint_types(class_name, properties)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>_validate_non_edges_do_not_have_edge_like_properties(class_name, properties)<EOL><DEDENT>_validate_property_names(class_name, properties)<EOL>self._class_name = class_name<EOL>self._kind = kind<EOL>self._abstract = abstract<EOL>self._properties = properties<EOL>self._class_fields = class_fields<EOL>self.in_connections = set()<EOL>self.out_connections = set()<EOL>
Create a new SchemaElement object. Args: class_name: string, the name of the schema element class. kind: string, one of the values of ELEMENT_KIND_VERTEX, ELEMENT_KIND_EDGE or ELEMENT_KIND_NON_GRAPH. Describes whether the schema class is a vertex, edge, or non-graph class. abstract: bool, True if the class is abstract, and False otherwise. properties: dict, property name -> PropertyDescriptor describing the properties of the schema element. class_fields: dict, class field name -> class field value, both strings Returns: a SchemaElement with the given parameters
f12653:c0:m0
@property<EOL><INDENT>def abstract(self):<DEDENT>
return self._abstract<EOL>
Return True if the represented type is abstract, and False otherwise.
f12653:c0:m1
@property<EOL><INDENT>def class_name(self):<DEDENT>
return self._class_name<EOL>
Return the name of the type that the schema element represents.
f12653:c0:m2
@property<EOL><INDENT>def properties(self):<DEDENT>
return self._properties<EOL>
Return a dict of property name to property descriptor for this schema element.
f12653:c0:m3
@property<EOL><INDENT>def class_fields(self):<DEDENT>
return self._class_fields<EOL>
Return a dict containing all class fields defined on the schema element.
f12653:c0:m4
@property<EOL><INDENT>def is_vertex(self):<DEDENT>
return self._kind == SchemaElement.ELEMENT_KIND_VERTEX<EOL>
Return True if the schema element represents a vertex type, and False otherwise.
f12653:c0:m5
@property<EOL><INDENT>def is_edge(self):<DEDENT>
return self._kind == SchemaElement.ELEMENT_KIND_EDGE<EOL>
Return True if the schema element represents an edge type, and False otherwise.
f12653:c0:m6
@property<EOL><INDENT>def is_non_graph(self):<DEDENT>
return self._kind == SchemaElement.ELEMENT_KIND_NON_GRAPH<EOL>
Return True if the schema element represents a non-graph type, and False otherwise.
f12653:c0:m7
def freeze(self):
self.in_connections = frozenset(self.in_connections)<EOL>self.out_connections = frozenset(self.out_connections)<EOL>
Make the SchemaElement's connections immutable.
f12653:c0:m8
def __str__(self):
return (<EOL>'<STR_LIT>'<EOL>.format(self._class_name, self._kind, self._abstract, self._properties,<EOL>self.in_connections, self.out_connections)<EOL>)<EOL>
Stringify the SchemaElement.
f12653:c0:m9
def __init__(self, schema_data):
toposorted_schema_data = toposort_classes(schema_data)<EOL>self._elements = dict()<EOL>self._inheritance_sets = dict()<EOL>self._subclass_sets = dict()<EOL>self._vertex_class_names = set()<EOL>self._edge_class_names = set()<EOL>self._non_graph_class_names = set()<EOL>self._set_up_inheritance_and_subclass_sets(toposorted_schema_data)<EOL>class_name_to_definition = {<EOL>class_definition['<STR_LIT:name>']: class_definition<EOL>for class_definition in toposorted_schema_data<EOL>}<EOL>self._split_classes_by_kind(class_name_to_definition)<EOL>kind_to_class_names = {<EOL>SchemaElement.ELEMENT_KIND_NON_GRAPH: self._non_graph_class_names,<EOL>SchemaElement.ELEMENT_KIND_VERTEX: self._vertex_class_names,<EOL>SchemaElement.ELEMENT_KIND_EDGE: self._edge_class_names,<EOL>}<EOL>for kind, class_names in six.iteritems(kind_to_class_names):<EOL><INDENT>self._set_up_schema_elements_of_kind(class_name_to_definition, kind, class_names)<EOL><DEDENT>self._link_vertex_and_edge_types()<EOL>for element in six.itervalues(self._elements):<EOL><INDENT>element.freeze()<EOL><DEDENT>
Create a new SchemaGraph from the OrientDB schema. Args: schema_data: list of dicts describing the classes in the OrientDB schema. The following format is the way the data is structured in OrientDB 2. See the README.md file for an example of how to query this data. Each dict has the following string fields: - name: string, the name of the class. - superClasses (optional): list of strings, the name of the class's superclasses. - superClass (optional): string, the name of the class's superclass. May be used instead of superClasses if there is only one superClass. Used for backwards compatibility with OrientDB. - customFields (optional): dict, string -> string, data defined on the class instead of instances of the class. - abstract: bool, true if the class is abstract. - properties: list of dicts, describing the class's properties. Each property dictionary has the following string fields: - name: string, the name of the property. - type: int, builtin OrientDB type ID of the field. See schema_properties.py for the mapping. - linkedType (optional): int, if the property is a collection of builtin OrientDB objects, then it indicates their type ID. - linkedClass (optional): string, if the property is a collection of class instances, then it indicates the name of the class. If class is an edge class, and the field name is either 'in' or 'out', then it describes the name of an endpoint of the edge. - defaultValue: string, the textual representation of the default value for the property, as returned by OrientDB's schema introspection code, e.g., '{}' for the embedded set type. Note that if the property is a collection type, it must have a default value. Returns: fully-constructed SchemaGraph object
f12653:c1:m0
def get_element_by_class_name(self, class_name):
return self._elements[class_name]<EOL>
Return the SchemaElement for the specified class name
f12653:c1:m1
def get_inheritance_set(self, cls):
return self._inheritance_sets[cls]<EOL>
Return all class names that the given class inherits from, including itself.
f12653:c1:m2
def get_subclass_set(self, cls):
return self._subclass_sets[cls]<EOL>
Return all class names that inherit from this class, including itself.
f12653:c1:m3
def get_default_property_values(self, classname):
schema_element = self.get_element_by_class_name(classname)<EOL>result = {<EOL>property_name: property_descriptor.default<EOL>for property_name, property_descriptor in six.iteritems(schema_element.properties)<EOL>}<EOL>if schema_element.is_edge:<EOL><INDENT>result.pop(EDGE_SOURCE_PROPERTY_NAME, None)<EOL>result.pop(EDGE_DESTINATION_PROPERTY_NAME, None)<EOL><DEDENT>return result<EOL>
Return a dict with default values for all properties declared on this class.
f12653:c1:m4
def _get_property_values_with_defaults(self, classname, property_values):
<EOL>final_values = self.get_default_property_values(classname)<EOL>final_values.update(property_values)<EOL>return final_values<EOL>
Return the property values for the class, with default values applied where needed.
f12653:c1:m5
def get_element_by_class_name_or_raise(self, class_name):
if class_name not in self._elements:<EOL><INDENT>raise InvalidClassError(u'<STR_LIT>'.format(class_name))<EOL><DEDENT>return self._elements[class_name]<EOL>
Return the SchemaElement for the specified class name, asserting that it exists.
f12653:c1:m6
def get_vertex_schema_element_or_raise(self, vertex_classname):
schema_element = self.get_element_by_class_name_or_raise(vertex_classname)<EOL>if not schema_element.is_vertex:<EOL><INDENT>raise InvalidClassError(u'<STR_LIT>'.format(vertex_classname))<EOL><DEDENT>return schema_element<EOL>
Return the schema element with the given name, asserting that it's of vertex type.
f12653:c1:m7
def get_edge_schema_element_or_raise(self, edge_classname):
schema_element = self.get_element_by_class_name_or_raise(edge_classname)<EOL>if not schema_element.is_edge:<EOL><INDENT>raise InvalidClassError(u'<STR_LIT>'.format(edge_classname))<EOL><DEDENT>return schema_element<EOL>
Return the schema element with the given name, asserting that it's of edge type.
f12653:c1:m8
def validate_is_vertex_type(self, vertex_classname):
self.get_vertex_schema_element_or_raise(vertex_classname)<EOL>
Validate that a vertex classname indeed corresponds to a vertex class.
f12653:c1:m9
def validate_is_edge_type(self, edge_classname):
self.get_edge_schema_element_or_raise(edge_classname)<EOL>
Validate that a edge classname indeed corresponds to a edge class.
f12653:c1:m10
def validate_is_non_abstract_vertex_type(self, vertex_classname):
element = self.get_vertex_schema_element_or_raise(vertex_classname)<EOL>if element.abstract:<EOL><INDENT>raise InvalidClassError(u'<STR_LIT>'<EOL>.format(vertex_classname))<EOL><DEDENT>
Validate that a vertex classname corresponds to a non-abstract vertex class.
f12653:c1:m11
def validate_is_non_abstract_edge_type(self, edge_classname):
element = self.get_edge_schema_element_or_raise(edge_classname)<EOL>if element.abstract:<EOL><INDENT>raise InvalidClassError(u'<STR_LIT>'<EOL>.format(edge_classname))<EOL><DEDENT>
Validate that a edge classname corresponds to a non-abstract edge class.
f12653:c1:m12
def validate_properties_exist(self, classname, property_names):
schema_element = self.get_element_by_class_name(classname)<EOL>requested_properties = set(property_names)<EOL>available_properties = set(schema_element.properties.keys())<EOL>non_existent_properties = requested_properties - available_properties<EOL>if non_existent_properties:<EOL><INDENT>raise InvalidPropertyError(<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(classname, non_existent_properties, property_names))<EOL><DEDENT>
Validate that the specified property names are indeed defined on the given class.
f12653:c1:m13
@property<EOL><INDENT>def class_names(self):<DEDENT>
return set(six.iterkeys(self._elements))<EOL>
Return the set of all class names
f12653:c1:m14
@property<EOL><INDENT>def vertex_class_names(self):<DEDENT>
return self._vertex_class_names<EOL>
Return the set of vertex class names in the SchemaGraph.
f12653:c1:m15
@property<EOL><INDENT>def edge_class_names(self):<DEDENT>
return self._edge_class_names<EOL>
Return the set of edge class names in the SchemaGraph.
f12653:c1:m16
@property<EOL><INDENT>def non_graph_class_names(self):<DEDENT>
return self._non_graph_class_names<EOL>
Return the set of non-graph class names in the SchemaGraph.
f12653:c1:m17
def _set_up_inheritance_and_subclass_sets(self, schema_data):
<EOL>for class_definition in schema_data:<EOL><INDENT>class_name = class_definition['<STR_LIT:name>']<EOL>immediate_superclass_names = get_superclasses_from_class_definition(<EOL>class_definition)<EOL>inheritance_set = set(immediate_superclass_names)<EOL>inheritance_set.add(class_name)<EOL>inheritance_set.update(chain.from_iterable(<EOL>self._inheritance_sets[superclass_name]<EOL>for superclass_name in immediate_superclass_names<EOL>))<EOL>self._inheritance_sets[class_name] = frozenset(inheritance_set)<EOL><DEDENT>for subclass_name, superclass_names in six.iteritems(self._inheritance_sets):<EOL><INDENT>for superclass_name in superclass_names:<EOL><INDENT>self._subclass_sets.setdefault(<EOL>superclass_name, set()).add(subclass_name)<EOL><DEDENT><DEDENT>for class_name in list(six.iterkeys(self._subclass_sets)):<EOL><INDENT>self._subclass_sets[class_name] = frozenset(self._subclass_sets[class_name])<EOL><DEDENT>
Load all inheritance data from the OrientDB schema. Used as part of __init__.
f12653:c1:m18
def _split_classes_by_kind(self, class_name_to_definition):
for class_name in class_name_to_definition:<EOL><INDENT>inheritance_set = self._inheritance_sets[class_name]<EOL>is_vertex = ORIENTDB_BASE_VERTEX_CLASS_NAME in inheritance_set<EOL>is_edge = ORIENTDB_BASE_EDGE_CLASS_NAME in inheritance_set<EOL>if is_vertex and is_edge:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(class_name, inheritance_set))<EOL><DEDENT>elif is_vertex:<EOL><INDENT>self._vertex_class_names.add(class_name)<EOL><DEDENT>elif is_edge:<EOL><INDENT>self._edge_class_names.add(class_name)<EOL><DEDENT>else:<EOL><INDENT>self._non_graph_class_names.add(class_name)<EOL><DEDENT><DEDENT>self._vertex_class_names = frozenset(self._vertex_class_names)<EOL>self._edge_class_names = frozenset(self._edge_class_names)<EOL>self._non_graph_class_names = frozenset(self._non_graph_class_names)<EOL>
Assign each class to the vertex, edge or non-graph type sets based on its kind.
f12653:c1:m19
def _set_up_schema_elements_of_kind(self, class_name_to_definition, kind, class_names):
allowed_duplicated_edge_property_names = frozenset({<EOL>EDGE_DESTINATION_PROPERTY_NAME, EDGE_SOURCE_PROPERTY_NAME<EOL>})<EOL>orientdb_base_classes = frozenset({<EOL>ORIENTDB_BASE_VERTEX_CLASS_NAME,<EOL>ORIENTDB_BASE_EDGE_CLASS_NAME,<EOL>})<EOL>for class_name in class_names:<EOL><INDENT>class_definition = class_name_to_definition[class_name]<EOL>class_fields = class_definition.get('<STR_LIT>')<EOL>if class_fields is None:<EOL><INDENT>class_fields = dict()<EOL><DEDENT>abstract = class_definition['<STR_LIT>']<EOL>if class_name in orientdb_base_classes:<EOL><INDENT>abstract = True<EOL><DEDENT>property_name_to_descriptor = {}<EOL>all_property_lists = (<EOL>class_name_to_definition[inherited_class_name]['<STR_LIT>']<EOL>for inherited_class_name in self._inheritance_sets[class_name]<EOL>)<EOL>links = {EDGE_DESTINATION_PROPERTY_NAME: [], EDGE_SOURCE_PROPERTY_NAME: []}<EOL>for property_definition in chain.from_iterable(all_property_lists):<EOL><INDENT>property_name = property_definition['<STR_LIT:name>']<EOL>duplication_allowed = all((<EOL>property_name in allowed_duplicated_edge_property_names,<EOL>kind == SchemaElement.ELEMENT_KIND_EDGE<EOL>))<EOL>if not duplication_allowed and property_name in property_name_to_descriptor:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(property_name, class_name))<EOL><DEDENT>property_descriptor = self._create_descriptor_from_property_definition(<EOL>class_name, property_definition, class_name_to_definition)<EOL>if property_name in allowed_duplicated_edge_property_names:<EOL><INDENT>links[property_name].append(property_descriptor)<EOL><DEDENT>else:<EOL><INDENT>property_name_to_descriptor[property_name] = property_descriptor<EOL><DEDENT><DEDENT>for property_name in allowed_duplicated_edge_property_names:<EOL><INDENT>elements = {<EOL>property_descriptor.qualifier<EOL>for property_descriptor in links[property_name]<EOL>}<EOL>for property_descriptor in links[property_name]:<EOL><INDENT>subclass_set = self._subclass_sets[property_descriptor.qualifier]<EOL>if len(elements.intersection(subclass_set)) == <NUM_LIT:1>:<EOL><INDENT>current_descriptor = property_name_to_descriptor.get(property_name, None)<EOL>if current_descriptor and current_descriptor != property_descriptor:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(current_descriptor,<EOL>property_descriptor, class_name))<EOL><DEDENT>property_name_to_descriptor[property_name] = property_descriptor<EOL><DEDENT><DEDENT>if (property_name not in property_name_to_descriptor and not abstract and<EOL>kind == SchemaElement.ELEMENT_KIND_EDGE):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(property_name, class_name))<EOL><DEDENT><DEDENT>self._elements[class_name] = SchemaElement(class_name, kind, abstract,<EOL>property_name_to_descriptor, class_fields)<EOL><DEDENT>
Load all schema classes of the given kind. Used as part of __init__.
f12653:c1:m20
def _create_descriptor_from_property_definition(self, class_name, property_definition,<EOL>class_name_to_definition):
name = property_definition['<STR_LIT:name>']<EOL>type_id = property_definition['<STR_LIT:type>']<EOL>linked_class = property_definition.get('<STR_LIT>', None)<EOL>linked_type = property_definition.get('<STR_LIT>', None)<EOL>qualifier = None<EOL>validate_supported_property_type_id(name, type_id)<EOL>if type_id == PROPERTY_TYPE_LINK_ID:<EOL><INDENT>if class_name not in self._edge_class_names:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(name, class_name))<EOL><DEDENT>if name not in {EDGE_SOURCE_PROPERTY_NAME, EDGE_DESTINATION_PROPERTY_NAME}:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(name, class_name))<EOL><DEDENT>if linked_class is None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(name, property_definition))<EOL><DEDENT>if linked_class not in self._vertex_class_names:<EOL><INDENT>is_linked_class_abstract = class_name_to_definition[linked_class]['<STR_LIT>']<EOL>all_subclasses_are_vertices = True<EOL>for subclass in self._subclass_sets[linked_class]:<EOL><INDENT>if subclass != linked_class and subclass not in self.vertex_class_names:<EOL><INDENT>all_subclasses_are_vertices = False<EOL>break<EOL><DEDENT><DEDENT>if not (is_linked_class_abstract and all_subclasses_are_vertices):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(name, linked_class))<EOL><DEDENT><DEDENT>qualifier = linked_class<EOL><DEDENT>elif type_id in COLLECTION_PROPERTY_TYPES:<EOL><INDENT>if linked_class is not None and linked_type is not None:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(name, property_definition))<EOL><DEDENT>elif linked_type is not None and linked_class is None:<EOL><INDENT>validate_supported_property_type_id(name + '<STR_LIT>', linked_type)<EOL>qualifier = linked_type<EOL><DEDENT>elif linked_class is not None and linked_type is None:<EOL><INDENT>if linked_class not in self._non_graph_class_names:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(name, linked_class))<EOL><DEDENT>qualifier = linked_class<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(name, property_definition))<EOL><DEDENT><DEDENT>default_value = None<EOL>default_value_string = property_definition.get('<STR_LIT>', None)<EOL>if default_value_string is not None:<EOL><INDENT>default_value = parse_default_property_value(name, type_id, default_value_string)<EOL><DEDENT>descriptor = PropertyDescriptor(type_id=type_id, qualifier=qualifier, default=default_value)<EOL>_validate_collections_have_default_values(class_name, name, descriptor)<EOL>return descriptor<EOL>
Return a PropertyDescriptor corresponding to the given OrientDB property definition.
f12653:c1:m21
def _link_vertex_and_edge_types(self):
for edge_class_name in self._edge_class_names:<EOL><INDENT>edge_element = self._elements[edge_class_name]<EOL>if (EDGE_SOURCE_PROPERTY_NAME not in edge_element.properties or<EOL>EDGE_DESTINATION_PROPERTY_NAME not in edge_element.properties):<EOL><INDENT>if edge_element.abstract:<EOL><INDENT>continue<EOL><DEDENT>else:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(edge_element))<EOL><DEDENT><DEDENT>from_class_name = edge_element.properties[EDGE_SOURCE_PROPERTY_NAME].qualifier<EOL>to_class_name = edge_element.properties[EDGE_DESTINATION_PROPERTY_NAME].qualifier<EOL>edge_schema_element = self._elements[edge_class_name]<EOL>for from_class in self._subclass_sets[from_class_name]:<EOL><INDENT>from_schema_element = self._elements[from_class]<EOL>from_schema_element.out_connections.add(edge_class_name)<EOL>edge_schema_element.in_connections.add(from_class)<EOL><DEDENT>for to_class in self._subclass_sets[to_class_name]:<EOL><INDENT>to_schema_element = self._elements[to_class]<EOL>edge_schema_element.out_connections.add(to_class)<EOL>to_schema_element.in_connections.add(edge_class_name)<EOL><DEDENT><DEDENT>
For each edge, link it to the vertex types it connects to each other.
f12653:c1:m22
def toposort_classes(classes):
def get_class_topolist(class_name, name_to_class, processed_classes, current_trace):<EOL><INDENT>"""<STR_LIT>"""<EOL>if class_name in processed_classes:<EOL><INDENT>return []<EOL><DEDENT>if class_name in current_trace:<EOL><INDENT>raise AssertionError(<EOL>'<STR_LIT>'.format(class_name))<EOL><DEDENT>cls = name_to_class[class_name]<EOL>dependencies = _list_superclasses(cls)<EOL>properties = cls['<STR_LIT>'] if '<STR_LIT>' in cls else []<EOL>for prop in properties:<EOL><INDENT>if '<STR_LIT>' in prop:<EOL><INDENT>dependencies.append(prop['<STR_LIT>'])<EOL><DEDENT><DEDENT>class_list = []<EOL>current_trace.add(class_name)<EOL>for dependency in dependencies:<EOL><INDENT>class_list.extend(get_class_topolist(<EOL>dependency, name_to_class, processed_classes, current_trace))<EOL><DEDENT>current_trace.remove(class_name)<EOL>class_list.append(name_to_class[class_name])<EOL>processed_classes.add(class_name)<EOL>return class_list<EOL><DEDENT>class_map = {c['<STR_LIT:name>']: c for c in classes}<EOL>seen_classes = set()<EOL>toposorted = []<EOL>for name in class_map.keys():<EOL><INDENT>toposorted.extend(get_class_topolist(name, class_map, seen_classes, set()))<EOL><DEDENT>return toposorted<EOL>
Sort class metadatas so that a superclass is always before the subclass
f12654:m0
def _list_superclasses(class_def):
superclasses = class_def.get('<STR_LIT>', [])<EOL>if superclasses:<EOL><INDENT>return list(superclasses)<EOL><DEDENT>sup = class_def.get('<STR_LIT>', None)<EOL>if sup:<EOL><INDENT>return [sup]<EOL><DEDENT>else:<EOL><INDENT>return []<EOL><DEDENT>
Return a list of the superclasses of the given class
f12654:m1
def _construct_location_stack_entry(location, num_traverses):
if not isinstance(num_traverses, int) or num_traverses < <NUM_LIT:0>:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(num_traverses))<EOL><DEDENT>if not isinstance(location, Location):<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(location))<EOL><DEDENT>return LocationStackEntry(location=location, num_traverses=num_traverses)<EOL>
Return a LocationStackEntry namedtuple with the specified parameters.
f12656:m0
def _get_fields(ast):
if not ast.selection_set:<EOL><INDENT>return [], []<EOL><DEDENT>property_fields = []<EOL>vertex_fields = []<EOL>seen_field_names = set()<EOL>switched_to_vertices = False <EOL>for field_ast in ast.selection_set.selections:<EOL><INDENT>if not isinstance(field_ast, Field):<EOL><INDENT>continue<EOL><DEDENT>name = get_ast_field_name(field_ast)<EOL>if name in seen_field_names:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'.format(name))<EOL><DEDENT>seen_field_names.add(name)<EOL>if is_vertex_field_name(name):<EOL><INDENT>switched_to_vertices = True<EOL>vertex_fields.append(field_ast)<EOL><DEDENT>else:<EOL><INDENT>if switched_to_vertices:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(name))<EOL><DEDENT>property_fields.append(field_ast)<EOL><DEDENT><DEDENT>return vertex_fields, property_fields<EOL>
Return a list of vertex fields, and a list of property fields, for the given AST node. Also verifies that all property fields for the AST node appear before all vertex fields, raising GraphQLCompilationError if that is not the case. Args: ast: GraphQL AST node, obtained from the graphql library Returns: tuple of two lists - the first list contains ASTs for vertex fields - the second list contains ASTs for property fields
f12656:m1
def _get_inline_fragment(ast):
if not ast.selection_set:<EOL><INDENT>return None<EOL><DEDENT>fragments = [<EOL>ast_node<EOL>for ast_node in ast.selection_set.selections<EOL>if isinstance(ast_node, InlineFragment)<EOL>]<EOL>if not fragments:<EOL><INDENT>return None<EOL><DEDENT>if len(fragments) > <NUM_LIT:1>:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>')<EOL><DEDENT>return fragments[<NUM_LIT:0>]<EOL>
Return the inline fragment at the current AST node, or None if no fragment exists.
f12656:m2
def _mark_location(location):
return blocks.MarkLocation(location)<EOL>
Return a MarkLocation basic block that marks the present location in the query.
f12656:m3
def _process_output_source_directive(schema, current_schema_type, ast,<EOL>location, context, local_unique_directives):
<EOL>output_source_directive = local_unique_directives.get('<STR_LIT>', None)<EOL>if output_source_directive:<EOL><INDENT>if has_encountered_output_source(context):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>')<EOL><DEDENT>if is_in_optional_scope(context):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>')<EOL><DEDENT>set_output_source_data(context, location)<EOL>return blocks.OutputSource()<EOL><DEDENT>else:<EOL><INDENT>return None<EOL><DEDENT>
Process the output_source directive, modifying the context as appropriate. Args: schema: GraphQL schema object, obtained from the graphql library current_schema_type: GraphQLType, the schema type at the current location ast: GraphQL AST node, obtained from the graphql library location: Location object representing the current location in the query context: dict, various per-compilation data (e.g. declared tags, whether the current block is optional, etc.). May be mutated in-place in this function! local_unique_directives: dict, directive name string -> directive object, containing unique directives present on the current AST node *only* Returns: an OutputSource block, if one should be emitted, or None otherwise
f12656:m4
def _compile_property_ast(schema, current_schema_type, ast, location,<EOL>context, unique_local_directives):
validate_property_directives(unique_local_directives)<EOL>if location.field == COUNT_META_FIELD_NAME:<EOL><INDENT>if not is_in_fold_scope(context):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(COUNT_META_FIELD_NAME, location))<EOL><DEDENT><DEDENT>tag_directive = unique_local_directives.get('<STR_LIT>', None)<EOL>if tag_directive:<EOL><INDENT>if is_in_fold_scope(context):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(location))<EOL><DEDENT>if location.field == COUNT_META_FIELD_NAME:<EOL><INDENT>raise AssertionError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>.format(COUNT_META_FIELD_NAME, location))<EOL><DEDENT>tag_name = tag_directive.arguments[<NUM_LIT:0>].value.value<EOL>if tag_name in context['<STR_LIT>']:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'.format(tag_name))<EOL><DEDENT>validate_safe_string(tag_name)<EOL>context['<STR_LIT>'][tag_name] = {<EOL>'<STR_LIT:location>': location,<EOL>'<STR_LIT>': is_in_optional_scope(context),<EOL>'<STR_LIT:type>': strip_non_null_from_type(current_schema_type),<EOL>}<EOL>context['<STR_LIT>'].record_tag_info(tag_name, TagInfo(location=location))<EOL><DEDENT>output_directive = unique_local_directives.get('<STR_LIT>', None)<EOL>if output_directive:<EOL><INDENT>output_name = output_directive.arguments[<NUM_LIT:0>].value.value<EOL>if output_name in context['<STR_LIT>']:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(output_name, context))<EOL><DEDENT>validate_safe_string(output_name)<EOL>validate_output_name(output_name)<EOL>graphql_type = strip_non_null_from_type(current_schema_type)<EOL>if is_in_fold_scope(context):<EOL><INDENT>set_fold_innermost_scope(context)<EOL>if location.field != COUNT_META_FIELD_NAME:<EOL><INDENT>graphql_type = GraphQLList(graphql_type)<EOL><DEDENT><DEDENT>context['<STR_LIT>'][output_name] = {<EOL>'<STR_LIT:location>': location,<EOL>'<STR_LIT>': is_in_optional_scope(context),<EOL>'<STR_LIT:type>': graphql_type,<EOL>'<STR_LIT>': context.get('<STR_LIT>', None),<EOL>}<EOL><DEDENT>
Process property directives at this AST node, updating the query context as appropriate. Args: schema: GraphQL schema object, obtained from the graphql library current_schema_type: GraphQLType, the schema type at the current location ast: GraphQL AST node, obtained from the graphql library. Only for function signature uniformity at the moment -- it is currently not used. location: Location object representing the current location in the query context: dict, various per-compilation data (e.g. declared tags, whether the current block is optional, etc.). May be mutated in-place in this function! unique_local_directives: dict, directive name string -> directive object, containing unique directives present on the current AST node *only*
f12656:m5
def _get_recurse_directive_depth(field_name, field_directives):
recurse_directive = field_directives['<STR_LIT>']<EOL>optional_directive = field_directives.get('<STR_LIT>', None)<EOL>if optional_directive:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'.format(field_name))<EOL><DEDENT>recurse_args = get_uniquely_named_objects_by_name(recurse_directive.arguments)<EOL>recurse_depth = int(recurse_args['<STR_LIT>'].value.value)<EOL>if recurse_depth < <NUM_LIT:1>:<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(recurse_depth))<EOL><DEDENT>return recurse_depth<EOL>
Validate and return the depth parameter of the recurse directive.
f12656:m6
def _validate_recurse_directive_types(current_schema_type, field_schema_type, context):
<EOL>type_hints = context['<STR_LIT>'].get(field_schema_type)<EOL>type_hints_inverse = context['<STR_LIT>'].get(field_schema_type)<EOL>allowed_current_types = {field_schema_type}<EOL>if type_hints and isinstance(type_hints, GraphQLUnionType):<EOL><INDENT>allowed_current_types.update(type_hints.types)<EOL><DEDENT>if type_hints_inverse and isinstance(type_hints_inverse, GraphQLUnionType):<EOL><INDENT>allowed_current_types.update(type_hints_inverse.types)<EOL><DEDENT>current_scope_is_allowed = current_schema_type in allowed_current_types<EOL>is_implemented_interface = (<EOL>isinstance(field_schema_type, GraphQLInterfaceType) and<EOL>isinstance(current_schema_type, GraphQLObjectType) and<EOL>field_schema_type in current_schema_type.interfaces<EOL>)<EOL>if not any((current_scope_is_allowed, is_implemented_interface)):<EOL><INDENT>raise GraphQLCompilationError(u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT>'<EOL>u'<STR_LIT:{}>'.format(current_schema_type, field_schema_type))<EOL><DEDENT>
Perform type checks on the enclosing type and the recursed type for a recurse directive. Args: current_schema_type: GraphQLType, the schema type at the current location field_schema_type: GraphQLType, the schema type at the inner scope context: dict, various per-compilation data (e.g. declared tags, whether the current block is optional, etc.). May be mutated in-place in this function!
f12656:m7
def _compile_vertex_ast(schema, current_schema_type, ast,<EOL>location, context, unique_local_directives, fields):
basic_blocks = []<EOL>query_metadata_table = context['<STR_LIT>']<EOL>current_location_info = query_metadata_table.get_location_info(location)<EOL>vertex_fields, property_fields = fields<EOL>validate_vertex_directives(unique_local_directives)<EOL>for field_ast in property_fields:<EOL><INDENT>field_name = get_ast_field_name(field_ast)<EOL>property_schema_type = get_field_type_from_schema(current_schema_type, field_name)<EOL>inner_location = location.navigate_to_field(field_name)<EOL>inner_basic_blocks = _compile_ast_node_to_ir(schema, property_schema_type, field_ast,<EOL>inner_location, context)<EOL>basic_blocks.extend(inner_basic_blocks)<EOL><DEDENT>basic_blocks.append(_mark_location(location))<EOL>output_source = _process_output_source_directive(schema, current_schema_type, ast,<EOL>location, context, unique_local_directives)<EOL>if output_source:<EOL><INDENT>basic_blocks.append(output_source)<EOL><DEDENT>for field_ast in vertex_fields:<EOL><INDENT>field_name = get_ast_field_name(field_ast)<EOL>validate_context_for_visiting_vertex_field(location, field_name, context)<EOL>field_schema_type = get_vertex_field_type(current_schema_type, field_name)<EOL>hinted_base = context['<STR_LIT>'].get(field_schema_type, None)<EOL>if hinted_base:<EOL><INDENT>field_schema_type = hinted_base<EOL><DEDENT>inner_unique_directives = get_unique_directives(field_ast)<EOL>validate_vertex_field_directive_interactions(location, field_name, inner_unique_directives)<EOL>validate_vertex_field_directive_in_context(<EOL>location, field_name, inner_unique_directives, context)<EOL>recurse_directive = inner_unique_directives.get('<STR_LIT>', None)<EOL>optional_directive = inner_unique_directives.get('<STR_LIT>', None)<EOL>fold_directive = inner_unique_directives.get('<STR_LIT>', None)<EOL>in_topmost_optional_block = False<EOL>edge_traversal_is_optional = optional_directive is not None<EOL>edge_traversal_is_folded = fold_directive is not None<EOL>edge_traversal_is_recursive = recurse_directive is not None<EOL>within_optional_scope = is_in_optional_scope(context)<EOL>if edge_traversal_is_optional:<EOL><INDENT>non_fold_child_locations = {<EOL>child_location<EOL>for child_location in query_metadata_table.get_child_locations(location)<EOL>if not isinstance(child_location, FoldScopeLocation)<EOL>}<EOL>if non_fold_child_locations:<EOL><INDENT>location = query_metadata_table.revisit_location(location)<EOL>basic_blocks.append(_mark_location(location))<EOL><DEDENT><DEDENT>if fold_directive:<EOL><INDENT>inner_location = location.navigate_to_fold(field_name)<EOL><DEDENT>else:<EOL><INDENT>inner_location = location.navigate_to_subpath(field_name)<EOL><DEDENT>inner_location_info = LocationInfo(<EOL>parent_location=location,<EOL>type=strip_non_null_from_type(field_schema_type),<EOL>coerced_from_type=None,<EOL>optional_scopes_depth=(<EOL>current_location_info.optional_scopes_depth + edge_traversal_is_optional),<EOL>recursive_scopes_depth=(<EOL>current_location_info.recursive_scopes_depth + edge_traversal_is_recursive),<EOL>is_within_fold=(current_location_info.is_within_fold or edge_traversal_is_folded),<EOL>)<EOL>query_metadata_table.register_location(inner_location, inner_location_info)<EOL>if edge_traversal_is_optional:<EOL><INDENT>topmost_optional = get_optional_scope_or_none(context)<EOL>if topmost_optional is None:<EOL><INDENT>set_optional_scope_data(context, inner_location)<EOL>in_topmost_optional_block = True<EOL><DEDENT><DEDENT>edge_direction, edge_name = get_edge_direction_and_name(field_name)<EOL>if fold_directive:<EOL><INDENT>fold_block = blocks.Fold(inner_location)<EOL>basic_blocks.append(fold_block)<EOL>set_fold_scope_data(context, inner_location)<EOL><DEDENT>elif recurse_directive:<EOL><INDENT>_validate_recurse_directive_types(current_schema_type, field_schema_type, context)<EOL>recurse_depth = _get_recurse_directive_depth(field_name, inner_unique_directives)<EOL>basic_blocks.append(blocks.Recurse(edge_direction,<EOL>edge_name,<EOL>recurse_depth,<EOL>within_optional_scope=within_optional_scope))<EOL>query_metadata_table.record_recurse_info(location,<EOL>RecurseInfo(edge_direction=edge_direction,<EOL>edge_name=edge_name,<EOL>depth=recurse_depth))<EOL><DEDENT>else:<EOL><INDENT>basic_blocks.append(blocks.Traverse(edge_direction, edge_name,<EOL>optional=edge_traversal_is_optional,<EOL>within_optional_scope=within_optional_scope))<EOL><DEDENT>inner_basic_blocks = _compile_ast_node_to_ir(schema, field_schema_type, field_ast,<EOL>inner_location, context)<EOL>basic_blocks.extend(inner_basic_blocks)<EOL>if edge_traversal_is_folded:<EOL><INDENT>has_count_filter = has_fold_count_filter(context)<EOL>_validate_fold_has_outputs_or_count_filter(<EOL>get_context_fold_info(context), has_count_filter, context['<STR_LIT>'])<EOL>basic_blocks.append(blocks.Unfold())<EOL>unmark_context_fold_scope(context)<EOL>if has_count_filter:<EOL><INDENT>unmark_fold_count_filter(context)<EOL><DEDENT>if is_in_fold_innermost_scope(context):<EOL><INDENT>unmark_fold_innermost_scope(context)<EOL><DEDENT><DEDENT>if in_topmost_optional_block:<EOL><INDENT>unmark_optional_scope(context)<EOL><DEDENT>backtracking_required = (<EOL>(not fold_directive) and<EOL>(not has_encountered_output_source(context))<EOL>)<EOL>if backtracking_required:<EOL><INDENT>if edge_traversal_is_optional:<EOL><INDENT>basic_blocks.append(blocks.EndOptional())<EOL>basic_blocks.append(blocks.Backtrack(location, optional=True))<EOL>location = query_metadata_table.revisit_location(location)<EOL>basic_blocks.append(_mark_location(location))<EOL><DEDENT>else:<EOL><INDENT>basic_blocks.append(blocks.Backtrack(location))<EOL><DEDENT><DEDENT><DEDENT>return basic_blocks<EOL>
Return a list of basic blocks corresponding to the vertex AST node. Args: schema: GraphQL schema object, obtained from the graphql library current_schema_type: GraphQLType, the schema type at the current location ast: GraphQL AST node, obtained from the graphql library location: Location object representing the current location in the query context: dict, various per-compilation data (e.g. declared tags, whether the current block is optional, etc.). May be mutated in-place in this function! unique_local_directives: dict, directive name string -> directive object, containing unique directives present on the current AST node *only* fields: tuple of lists (property_fields, vertex_fields), with lists of field objects present on the current vertex AST node Returns: list of basic blocks, the compiled output of the vertex AST node
f12656:m8