Exemple #1
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();
        }
Exemple #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);

            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();
        }