diff options
author | John Estabrook <jestabro@vyos.io> | 2023-03-08 13:30:03 -0600 |
---|---|---|
committer | John Estabrook <jestabro@vyos.io> | 2023-03-08 13:39:52 -0600 |
commit | f28c6531c8f81237574501ad09067ec0291fa21d (patch) | |
tree | 102b41c54487be81131ab17c9428939704a1e987 /src/services/api | |
parent | e1ea2f826ce7b9e8bfe6be4d3814a456e04963f1 (diff) | |
download | vyos-1x-f28c6531c8f81237574501ad09067ec0291fa21d.tar.gz vyos-1x-f28c6531c8f81237574501ad09067ec0291fa21d.zip |
graphql: T5068: generate client operations for code generation tools
Diffstat (limited to 'src/services/api')
-rwxr-xr-x | src/services/api/graphql/generate/schema_from_op_mode.py | 89 |
1 files changed, 86 insertions, 3 deletions
diff --git a/src/services/api/graphql/generate/schema_from_op_mode.py b/src/services/api/graphql/generate/schema_from_op_mode.py index 5e00e66bc..cb7b0fd21 100755 --- a/src/services/api/graphql/generate/schema_from_op_mode.py +++ b/src/services/api/graphql/generate/schema_from_op_mode.py @@ -39,8 +39,10 @@ else: OP_MODE_PATH = directories['op_mode'] SCHEMA_PATH = directories['api_schema'] +CLIENT_OP_PATH = directories['api_client_op'] DATA_DIR = directories['data'] + op_mode_include_file = os.path.join(DATA_DIR, 'op-mode-standardized.json') op_mode_error_schema = 'op_mode_error.graphql' @@ -118,6 +120,40 @@ type {{ name }} implements OpModeError { {%- endfor %} """ +op_query_template = """ +query {{ op_name }} ({{ op_sig }}) { + {{ op_name }} (data: { {{ op_arg }} }) { + success + errors + op_mode_error { + name + message + vyos_code + } + data { + result + } + } +} +""" + +op_mutation_template = """ +mutation {{ op_name }} ({{ op_sig }}) { + {{ op_name }} (data: { {{ op_arg }} }) { + success + errors + op_mode_error { + name + message + vyos_code + } + data { + result + } + } +} +""" + def create_schema(func_name: str, base_name: str, func: callable, enums: dict) -> str: sig = signature(func) @@ -152,6 +188,44 @@ def create_schema(func_name: str, base_name: str, func: callable, return res +def create_client_op(func_name: str, base_name: str, func: callable, + enums: dict) -> str: + sig = signature(func) + + for k in sig.parameters: + t = get_literal_values(sig.parameters[k].annotation) + if t: + enums[t] = snake_to_pascal_case(sig.parameters[k].name + '_' + base_name) + + field_dict = {} + for k in sig.parameters: + field_dict[sig.parameters[k].name] = map_type_name(sig.parameters[k].annotation, enums) + + # It is assumed that if one is generating a schema for a 'show_*' + # function, that 'get_raw_data' is present and 'raw' is desired. + if 'raw' in list(field_dict): + del field_dict['raw'] + + op_sig = ['$key: String'] + op_arg = ['key: $key'] + for k,v in field_dict.items(): + op_sig.append('$'+k+': '+v) + op_arg.append(k+': $'+k) + + op_data = {} + op_data['op_name'] = snake_to_pascal_case(func_name + '_' + base_name) + op_data['op_sig'] = ', '.join(op_sig) + op_data['op_arg'] = ', '.join(op_arg) + + if is_show_function_name(func_name): + j2_template = Template(op_query_template) + else: + j2_template = Template(op_mutation_template) + + res = j2_template.render(op_data) + + return res + def create_enums(enums: dict) -> str: enum_data = [] for k, v in enums.items(): @@ -186,6 +260,8 @@ def create_error_schema(): return res def generate_op_mode_definitions(): + os.makedirs(CLIENT_OP_PATH, exist_ok=True) + out = create_error_schema() with open(f'{SCHEMA_PATH}/{op_mode_error_schema}', 'w') as f: f.write(out) @@ -204,16 +280,23 @@ def generate_op_mode_definitions(): for (name, thunk) in funcs: funcs_dict[name] = thunk - results = [] + schema = [] + client_op = [] enums = {} # gather enums from function Literal type args for name,func in funcs_dict.items(): res = create_schema(name, basename, func, enums) - results.append(res) + schema.append(res) + res = create_client_op(name, basename, func, enums) + client_op.append(res) out = create_enums(enums) - out += '\n'.join(results) + out += '\n'.join(schema) with open(f'{SCHEMA_PATH}/{basename}.graphql', 'w') as f: f.write(out) + out = '\n'.join(client_op) + with open(f'{CLIENT_OP_PATH}/{basename}.graphql', 'w') as f: + f.write(out) + if __name__ == '__main__': generate_op_mode_definitions() |