Ejemplo n.º 1
0
        private static string GetDataPropertyType(GraphQlType baseType, IGraphQlMember member)
        {
            string propertyType;
            var    fieldType = member.Type.UnwrapIfNonNull();

            switch (fieldType.Kind)
            {
            case GraphQlTypeKind.Object:
            case GraphQlTypeKind.Interface:
            case GraphQlTypeKind.Union:
            case GraphQlTypeKind.InputObject:
                var fieldTypeName = fieldType.Name;
                fieldTypeName = UseCustomClassNameIfDefined(fieldTypeName);
                propertyType  = $"{fieldTypeName}{GraphQlGeneratorConfiguration.ClassPostfix}";
                return(AddQuestionMarkIfNullableReferencesEnabled(propertyType));

            case GraphQlTypeKind.Enum:
                return(GraphQlGeneratorConfiguration.CustomScalarFieldTypeMapping(baseType, member.Type, member.Name));

            case GraphQlTypeKind.List:
                var itemTypeName = fieldType.OfType.UnwrapIfNonNull().Name;
                itemTypeName = UseCustomClassNameIfDefined(itemTypeName);
                var itemType         = IsUnknownObjectScalar(baseType, member.Name, fieldType.OfType) ? "object" : $"{itemTypeName}{GraphQlGeneratorConfiguration.ClassPostfix}";
                var suggestedNetType = ScalarToNetType(baseType, member.Name, fieldType.OfType).TrimEnd('?');
                if (!String.Equals(suggestedNetType, "object") && !String.Equals(suggestedNetType, "object?") && !suggestedNetType.TrimEnd().EndsWith("System.Object") && !suggestedNetType.TrimEnd().EndsWith("System.Object?"))
                {
                    itemType = suggestedNetType;
                }

                propertyType = $"ICollection<{itemType}>";

                return(AddQuestionMarkIfNullableReferencesEnabled(propertyType));

            case GraphQlTypeKind.Scalar:
                switch (fieldType.Name)
                {
                case GraphQlTypeBase.GraphQlTypeScalarInteger:
                    return(GetIntegerNetType(baseType, member.Type, member.Name));

                case GraphQlTypeBase.GraphQlTypeScalarString:
                    return(GetCustomScalarType(baseType, member.Type, member.Name));

                case GraphQlTypeBase.GraphQlTypeScalarFloat:
                    return(GetFloatNetType(baseType, member.Type, member.Name));

                case GraphQlTypeBase.GraphQlTypeScalarBoolean:
                    return(GetBooleanNetType(baseType, member.Type, member.Name));

                case GraphQlTypeBase.GraphQlTypeScalarId:
                    return(GetIdNetType(baseType, member.Type, member.Name));

                default:
                    return(GetCustomScalarType(baseType, member.Type, member.Name));
                }

            default:
                return(AddQuestionMarkIfNullableReferencesEnabled("string"));
            }
        }
Ejemplo n.º 2
0
        private static void GenerateDataProperty(
            GraphQlType baseType,
            IGraphQlMember member,
            bool isInterfaceMember,
            bool isDeprecated,
            string deprecationReason,
            bool decorateWithJsonProperty,
            WriteDataClassPropertyBodyDelegate writeBody,
            StringBuilder builder)
        {
            var propertyName = NamingHelper.ToPascalCase(member.Name);

            var propertyType = GetDataPropertyType(baseType, member);

            GenerateCodeComments(builder, member.Description, 4);

            if (isDeprecated)
            {
                deprecationReason = String.IsNullOrWhiteSpace(deprecationReason) ? null : $"(@\"{deprecationReason.Replace("\"", "\"\"")}\")";
                builder.AppendLine($"    [Obsolete{deprecationReason}]");
            }

            if (decorateWithJsonProperty)
            {
                decorateWithJsonProperty =
                    GraphQlGeneratorConfiguration.JsonPropertyGeneration == JsonPropertyGenerationOption.Always ||
                    !String.Equals(
                        member.Name,
                        propertyName,
                        GraphQlGeneratorConfiguration.JsonPropertyGeneration == JsonPropertyGenerationOption.CaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);

                if (GraphQlGeneratorConfiguration.JsonPropertyGeneration == JsonPropertyGenerationOption.Never)
                {
                    decorateWithJsonProperty = false;
                }
            }

            if (!isInterfaceMember && decorateWithJsonProperty)
            {
                builder.AppendLine($"    [JsonProperty(\"{member.Name}\")]");
            }

            if (baseType.Kind == GraphQlTypeKind.InputObject)
            {
                builder.AppendLine($"    [JsonConverter(typeof(QueryBuilderParameterConverter<{propertyType}>))]");
                propertyType = AddQuestionMarkIfNullableReferencesEnabled($"QueryBuilderParameter<{propertyType}>");
            }

            builder.Append($"    {(isInterfaceMember ? null : "public ")}{propertyType} {propertyName}");

            writeBody(propertyType, GetBackingFieldName(member.Name));

            builder.AppendLine();
        }
        private static void GenerateDataProperty(GraphQlType baseType, IGraphQlMember member, bool isInterfaceMember, bool isDeprecated, string deprecationReason, StringBuilder builder)
        {
            var propertyName = NamingHelper.ToPascalCase(member.Name);

            string propertyType;
            var    fieldType = member.Type.UnwrapIfNonNull();

            switch (fieldType.Kind)
            {
            case GraphQlTypeKindObject:
            case GraphQlTypeKindInterface:
            case GraphQlTypeKindUnion:
            case GraphQlTypeKindInputObject:
                propertyType = $"{fieldType.Name}{GraphQlGeneratorConfiguration.ClassPostfix}";
                break;

            case GraphQlTypeKindEnum:
                propertyType = $"{fieldType.Name}?";
                break;

            case GraphQlTypeKindList:
                var itemType         = IsUnknownObjectScalar(baseType, member.Name, fieldType.OfType) ? "object" : $"{fieldType.OfType.UnwrapIfNonNull().Name}{GraphQlGeneratorConfiguration.ClassPostfix}";
                var suggestedNetType = ScalarToNetType(baseType, member.Name, fieldType.OfType.UnwrapIfNonNull()).TrimEnd('?');
                if (!String.Equals(suggestedNetType, "object") && !suggestedNetType.TrimEnd().EndsWith("System.Object"))
                {
                    itemType = suggestedNetType;
                }

                propertyType = $"ICollection<{itemType}>";
                break;

            case GraphQlTypeKindScalar:
                switch (fieldType.Name)
                {
                case GraphQlTypeBase.GraphQlTypeScalarInteger:
                    propertyType = GetIntegerNetType();
                    break;

                case GraphQlTypeBase.GraphQlTypeScalarString:
                    propertyType = GetCustomScalarType(baseType, fieldType, member.Name);
                    break;

                case GraphQlTypeBase.GraphQlTypeScalarFloat:
                    propertyType = GetFloatNetType();
                    break;

                case GraphQlTypeBase.GraphQlTypeScalarBoolean:
                    propertyType = "bool?";
                    break;

                case GraphQlTypeBase.GraphQlTypeScalarId:
                    propertyType = GetIdNetType(baseType, fieldType, member.Name);
                    break;

                default:
                    propertyType = GetCustomScalarType(baseType, fieldType, member.Name);
                    break;
                }

                break;

            default:
                propertyType = "string";
                break;
            }

            GenerateCodeComments(builder, member.Description);

            if (isDeprecated)
            {
                deprecationReason = String.IsNullOrWhiteSpace(deprecationReason) ? null : $"(@\"{deprecationReason.Replace("\"", "\"\"")}\")";
                builder.AppendLine($"    [Obsolete{deprecationReason}]");
            }

            builder.AppendLine($"    {(isInterfaceMember ? null : "public ")}{propertyType} {propertyName} {{ get; set; }}");
        }
        private static void GenerateDataProperty(GraphQlType baseType, IGraphQlMember member, bool isDeprecated, string deprecationReason, StringBuilder builder)
        {
            var propertyName = NamingHelper.ToPascalCase(member.Name);

            string propertyType;
            var    fieldType = UnwrapNonNull(member.Type);

            switch (fieldType.Kind)
            {
            case GraphQlTypeKindObject:
                propertyType = $"{fieldType.Name}{GraphQlGeneratorConfiguration.ClassPostfix}";
                break;

            case GraphQlTypeKindEnum:
                propertyType = $"{fieldType.Name}?";
                break;

            case GraphQlTypeKindList:
                var itemType         = IsObjectScalar(fieldType.OfType) ? "object" : $"{UnwrapNonNull(fieldType.OfType).Name}{GraphQlGeneratorConfiguration.ClassPostfix}";
                var suggestedNetType = ScalarToNetType(baseType, member.Name, UnwrapNonNull(fieldType.OfType)).TrimEnd('?');
                if (!String.Equals(suggestedNetType, "object"))
                {
                    itemType = suggestedNetType;
                }

                propertyType = $"ICollection<{itemType}>";
                break;

            case GraphQlTypeKindScalar:
                switch (fieldType.Name)
                {
                case "Int":
                    propertyType = "int?";
                    break;

                case "Byte":
                    propertyType = "byte?";
                    break;

                case "String":
                    propertyType = GraphQlGeneratorConfiguration.CustomScalarFieldTypeMapping(baseType, fieldType, member.Name);
                    break;

                case "Float":
                    propertyType = "decimal?";
                    break;

                case "Boolean":
                    propertyType = "bool?";
                    break;

                case "ID":
                    propertyType = "Guid?";
                    break;

                case "DateTime":
                    propertyType = "DateTime?";
                    break;

                default:
                    propertyType = GraphQlGeneratorConfiguration.CustomScalarFieldTypeMapping(baseType, fieldType, member.Name);
                    break;
                }

                break;

            default:
                propertyType = "string";
                break;
            }

            GenerateCodeComments(builder, member.Description);

            if (isDeprecated)
            {
                deprecationReason = String.IsNullOrWhiteSpace(deprecationReason) ? null : $"(@\"{deprecationReason.Replace("\"", "\"\"")}\")";
                builder.AppendLine($"    [Obsolete{deprecationReason}]");
            }

            builder.AppendLine($"    public {propertyType} {propertyName} {{ get; set; }}");
        }
Ejemplo n.º 5
0
        private static void GenerateDataProperty(
            GraphQlType baseType,
            IGraphQlMember member,
            bool isInterfaceMember,
            bool isDeprecated,
            string deprecationReason,
            bool decorateWithJsonProperty,
            WriteDataClassPropertyBodyDelegate writeBody,
            StringBuilder builder)
        {
            var propertyName = NamingHelper.ToPascalCase(member.Name);

            string propertyType;
            var    fieldType = member.Type.UnwrapIfNonNull();

            switch (fieldType.Kind)
            {
            case GraphQlTypeKindObject:
            case GraphQlTypeKindInterface:
            case GraphQlTypeKindUnion:
            case GraphQlTypeKindInputObject:
                var fieldTypeName = fieldType.Name;
                fieldTypeName = UseCustomClassNameIfDefined(fieldTypeName);
                propertyType  = $"{fieldTypeName}{GraphQlGeneratorConfiguration.ClassPostfix}";
                propertyType  = AddQuestionMarkIfNullableReferencesEnabled(propertyType);
                break;

            case GraphQlTypeKindEnum:
                propertyType = $"{fieldType.Name}?";
                break;

            case GraphQlTypeKindList:
                var itemTypeName = fieldType.OfType.UnwrapIfNonNull().Name;
                itemTypeName = UseCustomClassNameIfDefined(itemTypeName);
                var itemType         = IsUnknownObjectScalar(baseType, member.Name, fieldType.OfType) ? "object" : $"{itemTypeName}{GraphQlGeneratorConfiguration.ClassPostfix}";
                var suggestedNetType = ScalarToNetType(baseType, member.Name, fieldType.OfType.UnwrapIfNonNull()).TrimEnd('?');
                if (!String.Equals(suggestedNetType, "object") && !String.Equals(suggestedNetType, "object?") && !suggestedNetType.TrimEnd().EndsWith("System.Object") && !suggestedNetType.TrimEnd().EndsWith("System.Object?"))
                {
                    itemType = suggestedNetType;
                }

                propertyType = $"ICollection<{itemType}>";

                propertyType = AddQuestionMarkIfNullableReferencesEnabled(propertyType);

                break;

            case GraphQlTypeKindScalar:
                switch (fieldType.Name)
                {
                case GraphQlTypeBase.GraphQlTypeScalarInteger:
                    propertyType = GetIntegerNetType();
                    break;

                case GraphQlTypeBase.GraphQlTypeScalarString:
                    propertyType = GetCustomScalarType(baseType, fieldType, member.Name);
                    break;

                case GraphQlTypeBase.GraphQlTypeScalarFloat:
                    propertyType = GetFloatNetType();
                    break;

                case GraphQlTypeBase.GraphQlTypeScalarBoolean:
                    propertyType = "bool?";
                    break;

                case GraphQlTypeBase.GraphQlTypeScalarId:
                    propertyType = GetIdNetType(baseType, fieldType, member.Name);
                    break;

                default:
                    propertyType = GetCustomScalarType(baseType, fieldType, member.Name);
                    break;
                }

                break;

            default:
                propertyType = AddQuestionMarkIfNullableReferencesEnabled("string");
                break;
            }

            GenerateCodeComments(builder, member.Description, 4);

            if (isDeprecated)
            {
                deprecationReason = String.IsNullOrWhiteSpace(deprecationReason) ? null : $"(@\"{deprecationReason.Replace("\"", "\"\"")}\")";
                builder.AppendLine($"    [Obsolete{deprecationReason}]");
            }

            if (decorateWithJsonProperty)
            {
                decorateWithJsonProperty =
                    GraphQlGeneratorConfiguration.JsonPropertyGeneration == JsonPropertyGenerationOption.Always ||
                    !String.Equals(
                        member.Name,
                        propertyName,
                        GraphQlGeneratorConfiguration.JsonPropertyGeneration == JsonPropertyGenerationOption.CaseInsensitive ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal);

                if (GraphQlGeneratorConfiguration.JsonPropertyGeneration == JsonPropertyGenerationOption.Never)
                {
                    decorateWithJsonProperty = false;
                }
            }

            if (!isInterfaceMember && decorateWithJsonProperty)
            {
                builder.AppendLine($"    [JsonProperty(\"{member.Name}\")]");
            }

            builder.Append($"    {(isInterfaceMember ? null : "public ")}{propertyType} {propertyName}");

            writeBody(propertyType);

            builder.AppendLine();
        }