public virtual string ResolveBuilderName(GraphQlTypeBase type)
        {
            type = type.GetRealType();

            TypeNamingModel.NameEntry entry;

            switch (type)
            {
            case GraphQlInterfaceType _:
                entry = _typeNamingModel.BuilderInterface;
                break;

            case GraphQlObjectType _:
                entry = _typeNamingModel.BuilderObject;
                break;

            case GraphQlUnionType _:
                entry = _typeNamingModel.BuilderUnion;
                break;

            default:
                throw new GeneratorInvalidOperationException($"Type {type.Kind:G} can not be a builder.");
            }

            return(ProcessName(type, entry));
        }
        public virtual string ResolveDtoName(GraphQlTypeBase type, bool withNullable)
        {
            TypeNamingModel.NameEntry entry;

            switch (type)
            {
            case GraphQlEnumType _:
                entry = _typeNamingModel.DtoEnum;
                break;

            case GraphQlInputObjectType _:
                entry = _typeNamingModel.DtoInputObject;
                break;

            case GraphQlInterfaceType _:
                entry = _typeNamingModel.DtoInterface;
                break;

            case GraphQlObjectType _:
                entry = _typeNamingModel.DtoObject;
                break;

            case GraphQlUnionType _:
                entry = _typeNamingModel.DtoUnion;
                break;

            default:
                throw new GeneratorNotSupportedException($"Type {type.Kind:G} can not be dto.");
            }

            return(ProcessName(type, entry, withNullable));
        }
Esempio n. 3
0
        public ScalarFieldTypeDescription GetCustomScalarFieldType(GraphQlGeneratorConfiguration configuration,
                                                                   GraphQlType baseType,
                                                                   GraphQlTypeBase valueType,
                                                                   string valueName)
        {
            valueType = valueType is GraphQlFieldType fieldType?fieldType.UnwrapIfNonNull() : valueType;

            // DateTime and Byte
            switch (valueType.Name)
            {
            case "Uuid": return(new ScalarFieldTypeDescription {
                    NetTypeName = "Guid?", FormatMask = "N"
                });

            case "Uuid!": return(new ScalarFieldTypeDescription {
                    NetTypeName = "Guid", FormatMask = "N"
                });

            case "Long": return(new ScalarFieldTypeDescription {
                    NetTypeName = "long?", FormatMask = null
                });

            case "DateTime": return(new ScalarFieldTypeDescription {
                    NetTypeName = "DateTime?", FormatMask = null
                });

            case "DateTime!": return(new ScalarFieldTypeDescription {
                    NetTypeName = "DateTime", FormatMask = null
                });
            }

            // fallback - not needed if all fields and arguments are resolved or the expected type is of "object" type
            return(DefaultScalarFieldTypeMappingProvider.Instance.GetCustomScalarFieldType(configuration, baseType, valueType, valueName));
        }
        public virtual string ResolveGraphQlTypeName(GraphQlTypeBase type)
        {
            switch (type)
            {
            case GraphQlListType listType:
                return($"[{ResolveGraphQlTypeName(listType.OfType)}]");

            case GraphQlNonNullType nonNullType:
                return($"{ResolveGraphQlTypeName(nonNullType.OfType)}!");

            default:
                return(type.Name);
            }
        }
Esempio n. 5
0
        public static GraphQlTypeBase GetRealType(this GraphQlTypeBase type)
        {
            type.VerifyNotNull(nameof(type));

            switch (type)
            {
            case GraphQlListType listType:
                return(GetRealType(listType.OfType));

            case GraphQlNonNullType nonNullType:
                return(GetRealType(nonNullType.OfType));

            default:
                return(type);
            }
        }
        public bool IfKind(GraphQlTypeBase type, string[] kindValues)
        {
            type = type.GetRealType();

            foreach (var kindValue in kindValues)
            {
                if (!Enum.TryParse <GraphQlKind>(kindValue, true, out var kind))
                {
                    throw new GeneratorNotSupportedException($"Kind '{kindValue}' is not supported.");
                }

                if (type.Kind == kind)
                {
                    return(true);
                }
            }

            return(false);
        }
        protected override string MakeNullable(string generatedName, GraphQlTypeBase type)
        {
            switch (type.Kind)
            {
            case GraphQlKind.Enum:
            case GraphQlKind.Scalar:
                return($"{generatedName}?");

            case GraphQlKind.List:
            case GraphQlKind.InputObject:
            case GraphQlKind.Interface:
            case GraphQlKind.Object:
            case GraphQlKind.Union:
                return(generatedName);

            case GraphQlKind.NonNull:
                throw new GeneratorInvalidOperationException("Non null can not be processed.");

            default:
                throw new GeneratorNotSupportedException($"Kind '{type.Kind:G}' is not supported.");
            }
        }
        private string ProcessName(GraphQlTypeBase type, TypeNamingModel.NameEntry entry, bool isNullable = false)
        {
            var result = type.Name;

            if (entry != null)
            {
                if (!string.IsNullOrEmpty(entry.RemoveRegex))
                {
                    result = Regex.Replace(result, entry.RemoveRegex, string.Empty);
                }

                if (entry.BuildFormat.Contains("{0}"))
                {
                    result = string.Format(entry.BuildFormat, result);
                }

                if (isNullable)
                {
                    result = MakeNullable(result, type);
                }
            }

            return(result);
        }
            public ScalarFieldTypeDescription GetCustomScalarFieldType(GraphQlGeneratorConfiguration configuration, GraphQlType baseType, GraphQlTypeBase valueType, string valueName)
            {
                var isNotNull     = valueType.Kind == GraphQlTypeKind.NonNull;
                var unwrappedType = valueType is GraphQlFieldType fieldType?fieldType.UnwrapIfNonNull() : valueType;

                var nullablePostfix = isNotNull ? null : "?";

                if (unwrappedType.Name == "ID")
                {
                    return new ScalarFieldTypeDescription {
                               NetTypeName = "Guid" + nullablePostfix, FormatMask = "N"
                    }
                }
                ;

                if (valueName == "before" || valueName == "after" || unwrappedType.Name == "DateTimeOffset")
                {
                    return new ScalarFieldTypeDescription {
                               NetTypeName = "DateTimeOffset" + nullablePostfix, FormatMask = "yyyy-MM-dd\"T\"HH:mm"
                    }
                }
                ;

                return(DefaultScalarFieldTypeMappingProvider.Instance.GetCustomScalarFieldType(configuration, baseType, valueType, valueName));
            }
        }
            public ScalarFieldTypeDescription GetCustomScalarFieldType(GraphQlGeneratorConfiguration configuration, GraphQlType baseType, GraphQlTypeBase valueType, string valueName) =>
            valueType.Name == "Boolean"
                    ? new ScalarFieldTypeDescription
            {
                NetTypeName = "bool"
            }

                    : DefaultScalarFieldTypeMappingProvider.Instance.GetCustomScalarFieldType(configuration, baseType, valueType, valueName);
 public override string ResolveGraphQlTypeName(GraphQlTypeBase type)
 {
     return(base.ResolveGraphQlTypeName(type));
 }
        public override string ResolveDtoName(GraphQlTypeBase type, bool withNullable)
        {
            var nullableSign = withNullable ? "?" : string.Empty;

            switch (type)
            {
            case GraphQlListType listType:
                return($"List<{ResolveDtoName(listType.OfType, withNullable)}>");

            case GraphQlNonNullType nonNullType:
                return(ResolveDtoName(nonNullType.OfType, true));

            case GraphQlScalarType scalarType:
                switch (scalarType.Type)
                {
                case ScalarTypes.String:
                    return("string");

                case ScalarTypes.Boolean:
                    return($"{nullableSign}");

                case ScalarTypes.Float:
                    return($"float{nullableSign}");

                case ScalarTypes.Int:
                    return($"int{nullableSign}");

                case ScalarTypes.Id:
                    return("string");

                case ScalarTypes.Date:
                    return($"DateTime{nullableSign}");

                case ScalarTypes.DateTime:
                    return($"DateTime{nullableSign}");

                case ScalarTypes.DateTimeOffset:
                    return($"DateTimeOffset{nullableSign}");

                case ScalarTypes.Seconds:
                    return($"long{nullableSign}");

                case ScalarTypes.Milliseconds:
                    return($"long{nullableSign}");

                case ScalarTypes.Decimal:
                    return($"decimal{nullableSign}");

                case ScalarTypes.Uri:
                    return("Uri");

                case ScalarTypes.Guid:
                    return($"Guid{nullableSign}");

                case ScalarTypes.Short:
                    return($"short{nullableSign}");

                case ScalarTypes.UShort:
                    return($"ushort{nullableSign}");

                case ScalarTypes.UInt:
                    return($"uint{nullableSign}");

                case ScalarTypes.ULong:
                    return($"ulong{nullableSign}");

                case ScalarTypes.Byte:
                    return($"byte{nullableSign}");

                case ScalarTypes.SByte:
                    return($"sbyte{nullableSign}");

                default:
                    throw new GeneratorNotSupportedException($"Scalar type '{scalarType.Type}' is not supported.");
                }

            default:
                return(base.ResolveDtoName(type, withNullable));
            }
        }
Esempio n. 13
0
        public static ScalarFieldTypeDescription GetFallbackFieldType(GraphQlGeneratorConfiguration configuration, GraphQlTypeBase valueType)
        {
            valueType = (valueType as GraphQlFieldType)?.UnwrapIfNonNull() ?? valueType;
            if (valueType.Kind == GraphQlTypeKind.Enum)
            {
                return new ScalarFieldTypeDescription {
                           NetTypeName = configuration.ClassPrefix + NamingHelper.ToPascalCase(valueType.Name) + configuration.ClassSuffix + "?"
                }
            }
            ;

            var dataType = valueType.Name == GraphQlTypeBase.GraphQlTypeScalarString ? "string" : "object";

            return(new ScalarFieldTypeDescription {
                NetTypeName = GraphQlGenerator.AddQuestionMarkIfNullableReferencesEnabled(configuration, dataType)
            });
        }
    }
Esempio n. 14
0
        public ScalarFieldTypeDescription GetCustomScalarFieldType(GraphQlGeneratorConfiguration configuration, GraphQlType baseType, GraphQlTypeBase valueType, string valueName)
        {
            valueName = NamingHelper.ToPascalCase(valueName);

            if (valueName == "From" || valueName == "ValidFrom" || valueName == "To" || valueName == "ValidTo" ||
                valueName == "CreatedAt" || valueName == "UpdatedAt" || valueName == "ModifiedAt" || valueName == "DeletedAt" ||
                valueName.EndsWith("Timestamp"))
            {
                return new ScalarFieldTypeDescription {
                           NetTypeName = "DateTimeOffset?"
                }
            }
            ;

            return(GetFallbackFieldType(configuration, valueType));
        }
 protected abstract string MakeNullable(string generatedName, GraphQlTypeBase type);
 public virtual string ResolveOnTypeMethodName(GraphQlTypeBase type)
 {
     return(ProcessName(type.GetRealType(), _typeNamingModel.ConstructionOnType));
 }