def render(self, parsed_query: ParsedQuery): # We sort fragment nodes to be first and operations to be last because of dependecies buffer = CodeChunk() buffer.write('# AUTOGENERATED file. Do not Change!') buffer.write('from functools import partial') buffer.write('from typing import Any, Callable, Mapping, List') buffer.write('from enum import Enum') buffer.write('from dataclasses import dataclass, field') buffer.write('from dataclasses_json import dataclass_json') buffer.write('from gql.clients import Client, AsyncIOClient') buffer.write('') if self.config.custom_header: buffer.write_lines(self.config.custom_header.split('\n')) buffer.write('') self.__render_datetime_field(buffer) # Enums if parsed_query.enums: self.__render_enum_field(buffer) for enum in parsed_query.enums: self.__render_enum(buffer, enum) sorted_objects = sorted(parsed_query.objects, key=lambda obj: 1 if isinstance(obj, ParsedOperation) else 0) for obj in sorted_objects: if isinstance(obj, ParsedObject): self.__render_object(parsed_query, buffer, obj) elif isinstance(obj, ParsedOperation): self.__render_operation(parsed_query, buffer, obj) return str(buffer)
def test_codegen_write_lines(module_compiler): lines = ['@staticmethod', 'def sum(a, b):' ' return a + b'] gen = CodeChunk() gen.write('class Math:') gen.indent() gen.write_lines(lines) code = str(gen) m = module_compiler(code) assert m.Math.sum(2, 3) == 5
def test_codegen_write_lines(module_compiler): lines = [ '@staticmethod', 'def sum(a, b):' ' return a + b' ] gen = CodeChunk() gen.write('class Math:') gen.indent() gen.write_lines(lines) code = str(gen) m = module_compiler(code) assert m.Math.sum(2, 3) == 5