Changeset View
Changeset View
Standalone View
Standalone View
swh/graphql/app.py
# Copyright (C) 2022 The Software Heritage developers | # Copyright (C) 2022 The Software Heritage developers | ||||
# See the AUTHORS file at the top-level directory of this distribution | # See the AUTHORS file at the top-level directory of this distribution | ||||
# License: GNU General Public License version 3, or any later version | # License: GNU General Public License version 3, or any later version | ||||
# See top-level LICENSE file for more information | # See top-level LICENSE file for more information | ||||
# import pkg_resources | # import pkg_resources | ||||
import os | import os | ||||
from pathlib import Path | from pathlib import Path | ||||
from ariadne import gql, load_schema_from_path, make_executable_schema | from ariadne import gql, load_schema_from_path, make_executable_schema | ||||
from ariadne.validation import cost_validator | |||||
from .resolvers import resolvers, scalars | from .resolvers import resolvers, scalars | ||||
type_defs = gql( | type_defs = gql( | ||||
# pkg_resources.resource_string("swh.graphql", "schema/schema.graphql").decode() | # pkg_resources.resource_string("swh.graphql", "schema/schema.graphql").decode() | ||||
load_schema_from_path( | load_schema_from_path( | ||||
os.path.join(Path(__file__).parent.resolve(), "schema", "schema.graphql") | os.path.join(Path(__file__).parent.resolve(), "schema", "schema.graphql") | ||||
) | ) | ||||
Show All 17 Lines | schema = make_executable_schema( | ||||
resolvers.directory_entry_target, | resolvers.directory_entry_target, | ||||
resolvers.search_result_target, | resolvers.search_result_target, | ||||
resolvers.binary_string, | resolvers.binary_string, | ||||
scalars.id_scalar, | scalars.id_scalar, | ||||
scalars.datetime_scalar, | scalars.datetime_scalar, | ||||
scalars.swhid_scalar, | scalars.swhid_scalar, | ||||
scalars.content_hash_scalar, | scalars.content_hash_scalar, | ||||
) | ) | ||||
def validation_rules(context_value, document, data): | |||||
from .server import graphql_cfg | |||||
# add logic to set max_query_cost depending on user type | |||||
max_query_cost = graphql_cfg["max_query_cost"]["anonymous"] | |||||
if max_query_cost: | |||||
return [ | |||||
cost_validator(maximum_cost=max_query_cost, variables=data.get("variables")) | |||||
] | |||||
return None |