Skip to content

Commit

Permalink
Add introspection result to file
Browse files Browse the repository at this point in the history
  • Loading branch information
omar2535 committed Sep 28, 2023
1 parent bc61a82 commit f692517
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 25 deletions.
29 changes: 17 additions & 12 deletions compiler/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,36 @@
"""

from pathlib import Path
from utils.utils import send_graphql_request
from compiler.utils import send_graphql_request, write_json_to_file
from compiler.introspection_query import introspection_query

import constants
import requests


class Compiler:
def __init__(self, save_path: str, url: str):
"""Initializes the compiler,
creates all necessary file paths for run if doesn't already exist
creates all necessary file paths for run if doesn't already exist
Args:
save_path (str): Save directory path
url (str): URL for graphql introspection query to hit
"""
Path(save_path).mkdir(parents=True, exist_ok=True)
open(Path(save_path) / constants.FUNCTION_LIST_FILE_NAME, "a").close()
open(Path(save_path) / constants.MUTATION_PARAMETER_FILE_NAME, "a").close()
open(Path(save_path) / constants.QUERY_PARAMETER_FILE_NAME, "a").close()
open(Path(save_path) / constants.SCHEMA_FILE_NAME, "a").close()

self.path = save_path
self.save_path = save_path
self.introspection_result_save_path = Path(save_path) / constants.INTROSPECTION_RESULT_FILE_NAME
self.function_list_save_path = Path(save_path) / constants.FUNCTION_LIST_FILE_NAME
self.mutation_parameter_save_path = Path(save_path) / constants.MUTATION_PARAMETER_FILE_NAME
self.query_parameter_save_path = Path(save_path) / constants.QUERY_PARAMETER_FILE_NAME
self.schema_save_path = Path(save_path) / constants.SCHEMA_FILE_NAME
self.url = url

Path(self.save_path).mkdir(parents=True, exist_ok=True)
open(self.introspection_result_save_path, "a").close()
open(self.function_list_save_path, "a").close()
open(self.mutation_parameter_save_path, "a").close()
open(self.query_parameter_save_path, "a").close()
open(self.schema_save_path, "a").close()

def run(self):
"""The only function required to be run from the caller, will perform:
1. Introspection query running
Expand All @@ -37,5 +42,5 @@ def run(self):

def get_introspection_query(self):
"""Run the introspection query, grab results and output to file"""
send_graphql_request(self.url, introspection_query)
# breakpoint()
result = send_graphql_request(self.url, introspection_query)
write_json_to_file(result, self.introspection_result_save_path)
37 changes: 37 additions & 0 deletions compiler/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
from typing import Callable

import requests
import constants
import json


def send_graphql_request(url: str, query: str, next: Callable[[dict], dict] = None) -> dict:
"""Send GraphQL request to the specified endpoint
Args:
url (str): URL of the graphql server
query (str): Query string to hit the server with
next (Callable[[dict], dict], optional): Callback function in case there is action to be done after. Defaults to None.
Returns:
dict: _description_
"""
body = {"query": query}

x = requests.post(url=url, json=body)

if next:
return next(json.loads(x.text))

return json.loads(x.text)


def write_json_to_file(contents: dict, output_file: str):
"""Write JSON to a file
Args:
contents (dict): Contents of the JSON
output_file (str): Output file path
"""
with open(output_file, "w") as file_handle:
json.dump(contents, file_handle, indent=4)
1 change: 1 addition & 0 deletions constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

"""For the compiler / parser"""
RAW_INTROSPECTION_FILE_NAME = "introspection.json"
INTROSPECTION_RESULT_FILE_NAME = "introspection_result.json"
SCHEMA_FILE_NAME = "schema.json"
FUNCTION_LIST_FILE_NAME = "mutation_function_list.yml"
QUERY_PARAMETER_FILE_NAME = "query_parameter_list.yml"
Expand Down
13 changes: 0 additions & 13 deletions utils/utils.py

This file was deleted.

0 comments on commit f692517

Please sign in to comment.