Beispiel #1
0
def document_all_deprecated_functions(schema_library: str,
                                      doc_filepath: str) -> None:
    deprecated_queries = []
    deprecated_mutations = []
    deprecated_fields = []
    schema = compile_schema_library(schema_library)
    for query_name, query_field in schema.query_type.fields.items():
        if query_field.is_deprecated:
            deprecated_queries.append(" - ".join(
                [f"`{query_name}`", query_field.deprecation_reason]))
    for mutation_name, mutation_field in schema.mutation_type.fields.items():
        if query_field.is_deprecated:
            deprecated_mutations.append(" - ".join(
                [f"`{mutation_name}`", mutation_field.deprecation_reason]))
    for type_name, type_object in schema.type_map.items():
        if isinstance(
                type_object,
            (GraphQLScalarType, GraphQLEnumType,
             GraphQLInputObjectType)) or type_name in ("Query", "Mutation"):
            continue
        for field_name, field_object in type_object.fields.items():
            if field_object.is_deprecated:
                field_long_name = ".".join([type_name, field_name])
                deprecated_fields.append(" - ".join(
                    [f"`{field_long_name}`", field_object.deprecation_reason]))
    with open(doc_filepath, "w") as f:
        f.write(
            DEPRECATED_DOC.format(
                "@" +
                "generated This file was created by cli/extract_graphql_deprecations.py"
                + "do not change it manually",
                "\n".join(map(lambda s: "* " + s, deprecated_queries)),
                "\n".join(map(lambda s: "* " + s, deprecated_mutations)),
                "\n".join(map(lambda s: "* " + s, deprecated_fields)),
            ))
Beispiel #2
0
def test_graphql_compilation(schema_library: str, graphql_library: str) -> None:
    schema = compile_schema_library(schema_library)
    fragment_library = read_fragment_queries(graphql_library)
    file_names = glob.glob(
        os.path.join(graphql_library, "**/*.graphql"), recursive=True
    )

    query_parser = QueryParser(schema)
    query_renderer = DataclassesRenderer(schema)

    for file_name in file_names:
        process_file(
            file_name, schema, query_parser, query_renderer, fragment_library, True
        )
Beispiel #3
0
def test_graphql_breaking_changes(schema_library: str) -> None:
    graphql_schema_versions = "graphql_schema_versions"
    zip_filepath = os.path.join(graphql_schema_versions, "old_schemas.zip")
    schema = compile_schema_library(schema_library)
    schemas = extract_zip(zip_filepath)
    minimal_version = get_minimal_supported_version(graphql_schema_versions)
    for ver, schema_str in schemas.items():
        if LooseVersion(ver) < LooseVersion(minimal_version):
            continue
        schema_str = schema_str.decode("utf-8")
        old_schema = build_ast_schema(parse(schema_str))
        breaking_changes = find_breaking_changes(old_schema, schema)
        assert len(breaking_changes) == 0, ERR_MSG_FORMAT.format(
            ver, breaking_changes)
    def test_graphql_compilation(self, mock_popen, mock_path_exists: MagicMock) -> None:
        schema_library = pkg_resources.resource_filename(__name__, "schema")
        schema = compile_schema_library(schema_library)
        graphql_library = pkg_resources.resource_filename(__name__, "graphql")
        fragment_library = read_fragment_queries(graphql_library)

        query_parser = QueryParser(schema)
        query_renderer = DataclassesRenderer(schema)

        filenames = glob.glob(
            os.path.join(graphql_library, "**/*.graphql"), recursive=True
        )

        for filename in filenames:
            process_file(
                filename, schema, query_parser, query_renderer, fragment_library, True
            )
Beispiel #5
0
    def test_graphql_breaking_changes(self, mock_popen,
                                      mock_path_exists) -> None:
        schema_versions_library = pkg_resources.resource_filename(
            __name__, "schema_versions")
        schema_library = pkg_resources.resource_filename(__name__, "schema")

        zip_filepath = os.path.join(schema_versions_library, "old_schemas.zip")
        schema = compile_schema_library(schema_library)
        schemas = extract_zip(zip_filepath)
        minimal_version = get_minimal_supported_version(
            schema_versions_library)
        for ver, schema_str in schemas.items():
            if LooseVersion(ver) < LooseVersion(minimal_version):
                continue
            schema_str = schema_str.decode("utf-8")
            old_schema = build_ast_schema(parse(schema_str))
            breaking_changes = find_breaking_changes(old_schema, schema)
            assert len(breaking_changes) == 0, ERR_MSG_FORMAT.format(
                ver, breaking_changes)
Beispiel #6
0
    def test_graphql_compilation(self, mock_popen, mock_path_exists):
        schema_library = pkg_resources.resource_filename(__name__, "schema")
        schema = compile_schema_library(schema_library)

        graphql_library = pkg_resources.resource_filename(__name__, "graphql")
        filenames = glob.glob(
            os.path.join(graphql_library, "**/*.graphql"), recursive=True
        )

        query_parser = QueryParser(schema)
        query_renderer = DataclassesRenderer(schema)

        for filename in filenames:
            with open(filename) as f:
                query = parse(f.read())
                usages = find_deprecated_usages(schema, query)
                assert len(usages) == 0, (
                    f"Graphql file name {filename} uses " f"deprecated fields {usages}"
                )
            self.verify_file(filename, query_parser, query_renderer)
def document_all_deprecated_functions(schema_library: str,
                                      doc_filepath: str) -> None:
    deprecated_queries = []
    deprecated_mutations = []
    deprecated_inputs = []
    deprecated_fields = []
    deprecated_enums = []
    schema = compile_schema_library(schema_library)

    for query_name, query_field in schema.query_type.fields.items():
        if query_field.is_deprecated:
            deprecated_queries.append(" - ".join(
                [f"`{query_name}`", query_field.deprecation_reason]))
    for mutation_name, mutation_field in schema.mutation_type.fields.items():
        if mutation_field.is_deprecated:
            deprecated_mutations.append(" - ".join(
                [f"`{mutation_name}`", mutation_field.deprecation_reason]))
    for type_name, type_object in schema.type_map.items():
        if isinstance(type_object, GraphQLScalarType) or type_name in (
                "Query",
                "Mutation",
        ):
            continue
        if isinstance(type_object, GraphQLInputObjectType):
            for field_name, field_object in type_object.fields.items():
                directives = field_object.ast_node.directives
                for directive in directives:
                    if directive.name.value == "deprecatedInput":
                        field_long_name = ".".join([type_name, field_name])
                        for argument in directive.arguments:
                            if argument.name.value == "reason":
                                deprecation_reason = (argument.value.value
                                                      if directive.arguments
                                                      else None)
                                deprecated_inputs.append(" - ".join([
                                    f"`{field_long_name}`", deprecation_reason
                                ]))
            continue
        if isinstance(type_object, GraphQLEnumType):
            for value_name, value_object in type_object.values.items():
                if value_object.is_deprecated:
                    value_long_name = ".".join([type_name, value_name])
                    deprecated_enums.append(" - ".join([
                        f"`{value_long_name}`", value_object.deprecation_reason
                    ]))
            continue
        for field_name, field_object in type_object.fields.items():
            if field_object.is_deprecated:
                field_long_name = ".".join([type_name, field_name])
                deprecated_fields.append(" - ".join(
                    [f"`{field_long_name}`", field_object.deprecation_reason]))
    with open(doc_filepath, "w") as f:
        f.write(
            DEPRECATED_DOC.format(
                "@" +
                "generated This file was created by cli/extract_graphql_deprecations.py"
                + "do not change it manually",
                "\n".join(map(lambda s: "* " + s, deprecated_queries)),
                "\n".join(map(lambda s: "* " + s, deprecated_mutations)),
                "\n".join(map(lambda s: "* " + s, deprecated_fields)),
                "\n".join(map(lambda s: "* " + s, deprecated_inputs)),
                "\n".join(map(lambda s: "* " + s, deprecated_enums)),
            ))