def execute_graphql(
    schema: GraphQLSchema,
    test_data: CommonTestData,
    client: OrientDB,
    sample_parameters: Dict[str, Any],
) -> FrozenSet[Tuple[FrozenSet[Tuple[str, Any]], int]]:
    """Compile GraphQL query to MATCH, execute it against the test_db, and return the results."""
    schema_based_type_equivalence_hints: Dict[
        Union[GraphQLInterfaceType, GraphQLObjectType], GraphQLUnionType
    ] = {}
    if test_data.type_equivalence_hints:
        # For test convenience, we accept the type equivalence hints in string form.
        # Here, we convert them to the required GraphQL types.
        for key, value in six.iteritems(test_data.type_equivalence_hints):
            key_type = schema.get_type(key)
            value_type = schema.get_type(value)
            if (
                key_type
                and value_type
                and (
                    isinstance(key_type, GraphQLInterfaceType)
                    or isinstance(key_type, GraphQLObjectType)
                )
                and isinstance(value_type, GraphQLUnionType)
            ):
                schema_based_type_equivalence_hints[key_type] = value_type
            else:
                raise AssertionError(
                    "Expected key_type to be of type GraphQLInterfaceType or GraphQLObject Type, "
                    "but received {}; and value_type to be of type GraphQLUnionType, but "
                    "received {}.".format(type(key_type), type(value_type))
                )

    common_schema_info = CommonSchemaInfo(schema, schema_based_type_equivalence_hints)
    result = graphql_to_match(common_schema_info, test_data.graphql_input, sample_parameters)

    # We need to preprocess the results to be agnostic of the returned order.
    # For this we perform the following steps
    # - convert lists (returned from @fold scopes) to tuples to make them hashable
    # - convert each row dict to a frozenset of its items
    # - create a Counter (multi-set) of the row frozensets
    # - convert the multi-set to a frozenset of its items
    row_dicts = [row.oRecordData for row in client.command(result.query)]
    if len(row_dicts) == 0:
        raise AssertionError("Zero records returned. Trivial snapshot not allowed.")
    row_dicts_using_tuples = [
        {
            column_name: convert_decimals_to_strings(
                tuple(value) if isinstance(value, list) else value
            )
            for column_name, value in row.items()
        }
        for row in row_dicts
    ]
    row_frozensets = [frozenset(row_dict.items()) for row_dict in row_dicts_using_tuples]
    rows_multiset = Counter(row_frozensets)
    row_counters_frozenset = frozenset(rows_multiset.items())

    return row_counters_frozenset
Пример #2
0
def graphql_ast_flatten_field(field: Field,
                              fragments: Dict[str, FragmentDefinition], schema,
                              root_schema: GraphQLSchema) -> List[str]:
    results = []
    # is_list = isinstance(schema, GraphQLList)
    list_count = 0
    while True:
        schema_changed = False
        if isinstance(schema, GraphQLNonNull):
            schema = schema.of_type
            schema_changed = True
        if isinstance(schema, GraphQLList):
            list_count += 1
            schema = schema.of_type
            schema_changed = True
        if not schema_changed:
            break
    if getattr(field, 'selection_set', None) is not None:
        for sub_field in field.selection_set.selections:
            if isinstance(sub_field, FragmentSpread):
                sub_field = fragments[sub_field.name.value]
                sub_field_schema = schema
            else:
                sub_field_schema = graphql_ast_get_subfield(
                    schema, sub_field.name.value)
                if type(sub_field_schema) == str:
                    sub_field_schema = root_schema.get_type(sub_field_schema)
            sub_field_results = graphql_ast_flatten_field(
                sub_field, fragments, sub_field_schema, root_schema)
            if isinstance(field, FragmentDefinition):
                results += sub_field_results
            else:
                for result in sub_field_results:
                    results.append(
                        f"{change_case(field.name.value)}{'[*]' * list_count}.{result}"
                    )
    else:
        results.append(change_case(field.name.value))
    return results