Skip to content

Commit

Permalink
PoC for typed gql queries
Browse files Browse the repository at this point in the history
  • Loading branch information
fishi0x01 committed Apr 29, 2022
1 parent 29db107 commit 6b927b2
Show file tree
Hide file tree
Showing 8 changed files with 25,357 additions and 1 deletion.
6 changes: 6 additions & 0 deletions reconcile/service_dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from reconcile.utils import gql
from reconcile import queries
from reconcile.utils.typed_gql_queries import query_saas_files


APPS_QUERY = """
Expand Down Expand Up @@ -63,6 +64,10 @@ def get_desired_dependency_names(app, dependency_map):
if github_urls:
required_dep_names.update(get_dependency_names(dependency_map, "github"))

for saas_file in query_saas_files():
# Do stuff on types :)
print(f"{saas_file.name} {saas_file.pipelines_provider.name}")

jenkins_configs = app.get("jenkinsConfigs")
if jenkins_configs:
instances = {jc["instance"]["name"] for jc in jenkins_configs}
Expand All @@ -89,6 +94,7 @@ def get_desired_dependency_names(app, dependency_map):
def run(dry_run):
settings = queries.get_app_interface_settings()
dependency_map = settings.get("dependencies")

if not dependency_map:
sys.exit()

Expand Down
47 changes: 47 additions & 0 deletions reconcile/utils/typed_gql_queries.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
########################################
# Very basic and dirty PoC for querying code-generated types
########################################
from schema.qontract_schema import App_v1, SaasFile_v2
from schema.saas_file_query import Operations as saas_file_operations

# TODO: remove type ignore
from sgqlc.endpoint.http import HTTPEndpoint # type: ignore[import]

from reconcile.utils.config import get_config
from urllib.parse import urlparse


_endpoint = None


def _get_endpoint():
global _endpoint
if _endpoint:
return _endpoint

config = get_config()
server_url = urlparse(config["graphql"]["server"])
server = server_url.geturl()
token = config["graphql"].get("token")
headers = {}
if token:
headers["Authorization"] = token
_endpoint = HTTPEndpoint(server, headers)
return _endpoint


def query_saas_files() -> list[SaasFile_v2]:
# Query defined in saas_files_query.gql
op = saas_file_operations.query.list_saas_files_v2

# Query the data
data = _get_endpoint()(op)

# Bring data into desired format - there is probably a more elegant way to do this
apps: list[App_v1] = (op + data).apps
all_saas_files: list[SaasFile_v2] = []
for app in apps:
for app_saas_file in app.saas_files_v2:
all_saas_files.append(app_saas_file)

return all_saas_files
Empty file added schema/__init__.py
Empty file.
Loading

0 comments on commit 6b927b2

Please sign in to comment.