def create_grapple_input_field(graphql_field: Any) -> GrappleField: check.isinst(graphql_field, InputValueDefinition) return construct_field( name=graphql_field.name.value, type_ref=create_type_ref(graphql_field.type), args=[], )
def get_field_varietal(graphql_field: FieldDefinition) -> FieldVarietal: check.isinst(graphql_field, FieldDefinition) for directive_name, varietal_enum in FIELD_VARIETAL_MAPPING.items(): if has_directive(graphql_field, directive_name): return varietal_enum return FieldVarietal.VANILLA
def req_int_argument(directive_ast: Directive, name: str) -> int: check.isinst(directive_ast, Directive) for argument in directive_ast.arguments: if argument.name.value == name: check.isinst(argument.value, IntValue) return int(argument.value.value) raise Exception('argument required')
def create_grapple_field_arg( graphql_arg: InputValueDefinition) -> GrappleFieldArgument: check.isinst(graphql_arg, InputValueDefinition) return GrappleFieldArgument( name=graphql_arg.name.value, type_ref=create_type_ref(graphql_arg.type), default_value=value_from_ast(graphql_arg.default_value), )
def req_string_argument(directive_ast: Directive, name: str) -> str: check.isinst(directive_ast, Directive) for argument in directive_ast.arguments: if argument.name.value == name: if not isinstance(argument.value, StringValue): raise Exception('must be str') return str(argument.value.value) raise Exception('argument required')
async def gen_list(cls: Type[TPent], context: PentContext, obj_ids: List[UUID]) -> List[TPent]: """Load a list of pents by ID. Ensures that each list member matches the calling class users = await TodoUser.gen_list(context, obj_ids) """ pents = await context.loader.load_many(obj_ids) for pent in pents: check.isinst(pent, cls) return cast(List[TPent], list(pents))
def create_grapple_enum_type( enum_type_ast: EnumTypeDefinition) -> GrappleTypeDef: check.isinst(enum_type_ast, EnumTypeDefinition) grapple_type_name = enum_type_ast.name.value values = [value_ast.name.value for value_ast in enum_type_ast.values] return create_enum_type_def( name=grapple_type_name, values=values, )
def create_grapple_type_definition(type_ast: TypeDefinition) -> GrappleTypeDef: check.isinst(type_ast, TypeDefinition) if isinstance(type_ast, ObjectTypeDefinition): return create_grapple_object_type(type_ast) elif isinstance(type_ast, InputObjectTypeDefinition): return create_grapple_input_type(type_ast) elif isinstance(type_ast, EnumTypeDefinition): return create_grapple_enum_type(type_ast) check.invariant(False, 'node not supported: ' + str(type_ast)) return None
async def gen(cls: Type[TPent], context: PentContext, obj_id: UUID) -> TPent: """Load a pent by ID. Ensures that the return value matches the calling class user = await TodoUser.gen(context, obj_id) pent = await Pent.gen(context, obj_id) """ pent = await context.loader.load(obj_id) if not pent: return None check.isinst(pent, cls) return cast(TPent, pent)
def create_grapple_input_type( input_type_ast: InputObjectTypeDefinition) -> GrappleTypeDef: check.isinst(input_type_ast, InputObjectTypeDefinition) grapple_type_name = input_type_ast.name.value grapple_fields = [ create_grapple_input_field(field) for field in input_type_ast.fields ] input_directive = get_directive(input_type_ast, 'pentMutationData') return create_input_type_def(name=grapple_type_name, fields=grapple_fields, is_pent_mutation_data=input_directive is not None)
def value_from_ast(ast: Value) -> Any: if not ast: return None check.isinst(ast, Value) if isinstance(ast, IntValue): return ast.value if isinstance(ast, StringValue): return '"' + ast.value + '"' if isinstance(ast, BooleanValue): return "True" if ast.value else "False" check.failed('Unsupported ast value: ' + repr(ast))
def create_grapple_field(graphql_field: FieldDefinition) -> GrappleField: check.isinst(graphql_field, FieldDefinition) field_varietal = get_field_varietal(graphql_field) return construct_field( name=graphql_field.name.value, type_ref=create_type_ref(graphql_field.type), args=[ create_grapple_field_arg(graphql_arg) for graphql_arg in graphql_field.arguments ], field_varietal=field_varietal, field_varietal_data=get_field_varietal_data(graphql_field, field_varietal), )
async def gen_create_pent_dynamic( context: PentContext, pent_cls_name: str, data_cls_name: str, payload_cls_name: str, data: PentMutationData ) -> PentMutationPayload: data_cls = context.cls_from_name(data_cls_name) check.isinst(data, data_cls) pent_cls = context.cls_from_name(pent_cls_name) payload_cls = context.cls_from_name(payload_cls_name) out_pent = await create_pent(context, pent_cls, data) return cast(PentMutationPayload, payload_cls(out_pent))
def get_field_varietal_data( graphql_field: FieldDefinition, field_varietal: FieldVarietal) -> FieldVarietalsUnion: check.isinst(graphql_field, FieldDefinition) if field_varietal == FieldVarietal.EDGE_TO_STORED_ID: dir_ast = get_directive(graphql_field, 'edgeToStoredId') return EdgeToStoredIdData( edge_name=req_string_argument(dir_ast, 'edgeName'), edge_id=req_int_argument(dir_ast, 'edgeId'), field=req_string_argument(dir_ast, 'field'), ) elif field_varietal == FieldVarietal.DELETE_PENT: dir_ast = get_directive(graphql_field, 'deletePent') return DeletePentData(type=req_string_argument(dir_ast, 'type')) return None
def create_grapple_object_type( object_type_ast: ObjectTypeDefinition) -> GrappleTypeDef: check.isinst(object_type_ast, ObjectTypeDefinition) grapple_type_name = object_type_ast.name.value grapple_fields = [ create_grapple_field(field) for field in object_type_ast.fields ] pent_directive = get_directive(object_type_ast, 'pent') type_id = req_int_argument(pent_directive, 'typeId') if pent_directive else None pent_payload_directive = get_directive(object_type_ast, 'pentMutationPayload') return create_object_type_def(name=grapple_type_name, fields=grapple_fields, is_pent=pent_directive is not None, type_id=type_id, is_pent_payload=pent_payload_directive is not None)
def get_directive(ast: Node, name: str) -> Directive: for dir_node in ast.directives: if dir_node.name.value == name: check.isinst(dir_node, Directive) return dir_node return None