private CodeBlockBuilder CreateEnumParsingSwitch( string paramName, EnumDescriptor descriptor) { StringBuilder sourceText = new StringBuilder() .AppendLine($"return {paramName} switch") .AppendLine("{") .AppendLineForEach( descriptor.Values, x => $" \"{x.GraphQLName}\" => {descriptor.Name}.{x.Name},") .AppendLine($" _ => throw new {TypeNames.GraphQLClientException}()") .AppendLine("};"); return(CodeBlockBuilder.From(sourceText)); }
private CodeBlockBuilder CreateEnumFormatingSwitch( string paramName, EnumDescriptor descriptor) { var sourceText = new StringBuilder(); sourceText.AppendLine($"return {paramName} switch"); sourceText.AppendLine("{"); foreach (var enumValue in descriptor.Values) { sourceText.AppendLine( $" {descriptor.Name}.{enumValue.Name} => \"{enumValue.GraphQLName}\","); } sourceText.AppendLine($" _ => throw new {TypeNames.GraphQLClientException}()"); sourceText.AppendLine("};"); return(CodeBlockBuilder.From(sourceText)); }
private ICode CreateSpecificEntityIdBody(EntityIdDescriptor entity) { var sourceText = new StringBuilder(); sourceText.AppendLine($"return new {TypeNames.EntityId}("); sourceText.AppendLine(" type,"); if (entity.Fields.Count == 1) { var field = entity.Fields[0]; sourceText.AppendLine( $" obj.GetProperty(\"{field.Name}\").{GetSerializerMethod(field)}()!);"); return(CodeBlockBuilder.From(sourceText)); } sourceText.Append(" ("); var next = false; foreach (var field in entity.Fields) { if (next) { sourceText.AppendLine(","); sourceText.Append(" "); } next = true; sourceText.Append( $"obj.GetProperty(\"{field.Name}\").{GetSerializerMethod(field)}()!"); } sourceText.AppendLine("));"); return(CodeBlockBuilder.From(sourceText)); }
private ICode CreateEntityIdBody(EntityIdFactoryDescriptor descriptor) { var sourceText = new StringBuilder(); sourceText.AppendLine( $"{TypeNames.String} typeName = obj.GetProperty(\"__typename\").GetString()!;"); sourceText.AppendLine(); sourceText.AppendLine("return typeName switch"); sourceText.AppendLine("{"); foreach (var entity in descriptor.Entities) { sourceText.AppendLine( $" \"{entity.Name}\" => Create{entity.Name}EntityId(obj, typeName),"); } sourceText.AppendLine($" _ => throw new {TypeNames.NotSupportedException}()"); sourceText.AppendLine("};"); return(CodeBlockBuilder.From(sourceText)); }
private ICode GenerateInternalMethodBody(DependencyInjectionDescriptor descriptor) { bool hasSubscriptions = descriptor.Operations.OfType <SubscriptionOperationDescriptor>().Any(); bool hasQueries = descriptor.Operations.OfType <QueryOperationDescriptor>().Any(); bool hasMutations = descriptor.Operations.OfType <MutationOperationDescriptor>().Any(); var stringBuilder = new StringBuilder(); var codeWriter = new CodeWriter(stringBuilder); stringBuilder.AppendLine(_staticCode); codeWriter.WriteComment("register connections"); if (hasSubscriptions) { stringBuilder.AppendLine(RegisterWebSocketConnection(descriptor.Name)); } if (hasQueries || hasMutations) { stringBuilder.AppendLine(RegisterHttpConnection(descriptor.Name)); } codeWriter.WriteComment("register mappers"); codeWriter.WriteLine(); foreach (var typeDescriptor in descriptor.TypeDescriptors) { if (typeDescriptor.Kind == TypeKind.EntityType && !typeDescriptor.IsInterface()) { NamedTypeDescriptor namedTypeDescriptor = (NamedTypeDescriptor)typeDescriptor.NamedType(); NameString className = namedTypeDescriptor.ExtractMapperName(); var interfaceName = TypeNames.IEntityMapper.WithGeneric( namedTypeDescriptor.ExtractTypeName(), typeDescriptor.Name); AddSingleton(codeWriter, interfaceName, className); } } codeWriter.WriteLine(); codeWriter.WriteComment("register serializers"); codeWriter.WriteLine(); foreach (var enumType in descriptor.EnumTypeDescriptor) { AddSingleton( codeWriter, TypeNames.ISerializer, EnumParserNameFromEnumName(enumType.Name)); } foreach (var serializer in _serializers) { AddSingleton( codeWriter, TypeNames.ISerializer, serializer); } foreach (var inputTypeDescriptor in descriptor.TypeDescriptors .Where(x => x.Kind is TypeKind.InputType)) { AddSingleton( codeWriter, TypeNames.ISerializer, InputValueFormatterFromType( (NamedTypeDescriptor)inputTypeDescriptor.NamedType())); } RegisterSerializerResolver().Build(codeWriter); codeWriter.WriteLine(); codeWriter.WriteComment("register operations"); foreach (var operation in descriptor.Operations) { string connectionKind = operation is SubscriptionOperationDescriptor ? TypeNames.WebSocketConnection : TypeNames.HttpConnection; NameString operationName = operation.OperationName; NameString fullName = operation.Name; NameString operationInterface = operation.ResultTypeReference.Name; // The resulttype of the operation is a NamedTypeDescriptor, that is an Interface var resultType = operation.ResultTypeReference as NamedTypeDescriptor ?? throw new ArgumentException("ResultTypeReference"); // The factories are generated based on the concrete result type, which is the // only implementee of the result type interface. var factoryName = ResultFactoryNameFromTypeName(resultType.ImplementedBy[0].Name); var builderName = ResultBuilderNameFromTypeName(operationName); stringBuilder.AppendLine( RegisterOperation( connectionKind, descriptor.Name, fullName, operationInterface, factoryName, builderName)); } stringBuilder.AppendLine( $"{TypeNames.AddSingleton.WithGeneric(descriptor.Name)}(services);"); stringBuilder.AppendLine(); stringBuilder.AppendLine("return services;"); return(CodeBlockBuilder.From(stringBuilder)); }