private static void FindAllReferencedObjectTypes(GraphQlSchema schema, GraphQlType type, ISet <string> objectTypes)
        {
            foreach (var member in (IEnumerable <IGraphQlMember>)type.InputFields ?? type.Fields)
            {
                var         unwrappedType = member.Type.UnwrapIfNonNull();
                GraphQlType memberType;
                switch (unwrappedType.Kind)
                {
                case GraphQlTypeKindObject:
                    objectTypes.Add(unwrappedType.Name);
                    memberType = schema.Types.Single(t => t.Name == unwrappedType.Name);
                    FindAllReferencedObjectTypes(schema, memberType, objectTypes);
                    break;

                case GraphQlTypeKindList:
                    var itemType = unwrappedType.OfType.UnwrapIfNonNull();
                    if (IsComplexType(itemType.Kind))
                    {
                        memberType = schema.Types.Single(t => t.Name == itemType.Name);
                        FindAllReferencedObjectTypes(schema, memberType, objectTypes);
                    }

                    break;
                }
            }
        }
 private static void GenerateSharedTypes(GraphQlSchema schema, StringBuilder builder)
 {
     builder.AppendLine("#region shared types");
     GenerateEnums(schema, builder);
     builder.AppendLine("#endregion");
     builder.AppendLine();
 }
Пример #3
0
        public static string GenerateFullClientCSharpFile(GraphQlSchema schema, string @namespace)
        {
            var builder = new StringBuilder();

            builder.AppendLine(AutoGeneratedPrefix);
            builder.AppendLine();
            builder.AppendLine(RequiredNamespaces);
            builder.AppendLine($"namespace {@namespace}");
            builder.AppendLine("{");

            var memberBuilder = new StringBuilder();

            GenerateQueryBuilder(schema, memberBuilder);

            memberBuilder.AppendLine();
            memberBuilder.AppendLine();

            GenerateDataClasses(schema, memberBuilder);

            var indentedLines =
                memberBuilder
                .ToString()
                .Split(new[] { Environment.NewLine }, StringSplitOptions.None)
                .Select(l => $"    {l}");

            foreach (var line in indentedLines)
            {
                builder.AppendLine(line);
            }

            builder.AppendLine("}");

            return(builder.ToString());
        }
 private static void GenerateEnums(GraphQlSchema schema, StringBuilder builder)
 {
     foreach (var type in schema.Types.Where(t => t.Kind == GraphQlTypeKindEnum && !t.Name.StartsWith("__")))
     {
         GenerateEnum(type, builder);
         builder.AppendLine();
     }
 }
Пример #5
0
        public static void GenerateQueryBuilder(GraphQlSchema schema, StringBuilder builder)
        {
            using (var reader = new StreamReader(typeof(GraphQlGenerator).GetTypeInfo().Assembly.GetManifestResourceStream("GraphQlClientGenerator.BaseClasses.cs")))
                builder.AppendLine(reader.ReadToEnd());

            GenerateSharedTypes(schema, builder);

            if (GraphQlGeneratorConfiguration.CSharpVersion == CSharpVersion.NewestWithNullableReferences)
            {
                builder.AppendLine("#nullable enable");
            }

            builder.AppendLine("#region builder classes");

            var complexTypes          = schema.Types.Where(t => IsComplexType(t.Kind) && !t.Name.StartsWith("__")).ToArray();
            var complexTypeDictionary = complexTypes.ToDictionary(t => t.Name);

            for (var i = 0; i < complexTypes.Length; i++)
            {
                var type = complexTypes[i];

                string queryPrefix;
                if (type.Name == schema.QueryType?.Name)
                {
                    queryPrefix = "query";
                }
                else if (type.Name == schema.MutationType?.Name)
                {
                    queryPrefix = "mutation";
                }
                else if (type.Name == schema.SubscriptionType?.Name)
                {
                    queryPrefix = "subscription";
                }
                else
                {
                    queryPrefix = null;
                }

                GenerateTypeQueryBuilder(type, complexTypeDictionary, queryPrefix, builder);

                if (i < complexTypes.Length - 1)
                {
                    builder.AppendLine();
                }
            }

            builder.AppendLine("#endregion");

            if (GraphQlGeneratorConfiguration.CSharpVersion == CSharpVersion.NewestWithNullableReferences)
            {
                builder.AppendLine("#nullable disable");
            }
        }
        public static void GenerateDataClasses(GraphQlSchema schema, StringBuilder builder)
        {
            var objectTypes   = schema.Types.Where(t => t.Kind == GraphQlTypeKindObject && !t.Name.StartsWith("__")).ToArray();
            var hasObjectType = objectTypes.Any();

            if (hasObjectType)
            {
                builder.AppendLine("#region data classes");

                for (var i = 0; i < objectTypes.Length; i++)
                {
                    var type = objectTypes[i];
                    GenerateDataClass(type.Name, null, builder, () => GenerateDataClassBody(type, builder));

                    builder.AppendLine();

                    if (i < objectTypes.Length - 1)
                    {
                        builder.AppendLine();
                    }
                }

                builder.AppendLine("#endregion");
            }

            var inputTypes = schema.Types.Where(t => t.Kind == GraphQlTypeKindInputObject && !t.Name.StartsWith("__")).ToArray();

            if (inputTypes.Any())
            {
                if (hasObjectType)
                {
                    builder.AppendLine();
                }

                builder.AppendLine("#region input classes");

                for (var i = 0; i < inputTypes.Length; i++)
                {
                    var type = inputTypes[i];
                    GenerateDataClass(type.Name, "GraphQlMutationInput", builder, () => GenerateInputDataClassBody(type, builder));

                    builder.AppendLine();

                    if (i < inputTypes.Length - 1)
                    {
                        builder.AppendLine();
                    }
                }

                builder.AppendLine("#endregion");
            }
        }
Пример #7
0
        protected GenerationContext(GraphQlSchema schema, GeneratedObjectType objectTypes, byte indentationSize)
        {
            var optionsInteger = (int)objectTypes;

            if (optionsInteger != 1 && optionsInteger != 2 && optionsInteger != 3)
            {
                throw new ArgumentException("invalid value", nameof(objectTypes));
            }

            Schema      = schema ?? throw new ArgumentNullException(nameof(schema));
            ObjectTypes = objectTypes;
            Indentation = indentationSize;
        }
        public static void GenerateQueryBuilder(GraphQlSchema schema, StringBuilder builder)
        {
#if UNITY_EDITOR
            var path = Path.GetDirectoryName(GetThisFilePath()) + "\\BaseClasses";
            using (var reader = new StreamReader(path))
#else
            using (var reader = new StreamReader(typeof(GraphQlGenerator).GetTypeInfo().Assembly.GetManifestResourceStream("GraphQlClientGenerator.BaseClasses")))
#endif
                builder.AppendLine(reader.ReadToEnd());

            GenerateSharedTypes(schema, builder);

            builder.AppendLine("#region builder classes");

            var complexTypes = schema.Types.Where(t => IsComplexType(t.Kind) && !t.Name.StartsWith("__")).ToArray();
            for (var i = 0; i < complexTypes.Length; i++)
            {
                var type = complexTypes[i];

                string queryPrefix;
                if (String.Equals(type.Name, schema.MutationType?.Name))
                {
                    queryPrefix = "mutation";
                }
                else if (String.Equals(type.Name, schema.SubscriptionType?.Name))
                {
                    queryPrefix = "subscription";
                }
                else
                {
                    queryPrefix = null;
                }

                GenerateTypeQueryBuilder(type, queryPrefix, builder);

                if (i < complexTypes.Length - 1)
                {
                    builder.AppendLine();
                }
            }

            builder.AppendLine("#endregion");
        }
        public static void GenerateDataClasses(GraphQlSchema schema, StringBuilder builder)
        {
            builder.AppendLine("#region data classes");

            var objectTypes = schema.Types.Where(t => t.Kind == GraphQlTypeKindObject && !t.Name.StartsWith("__")).ToArray();

            for (var i = 0; i < objectTypes.Length; i++)
            {
                var type = objectTypes[i];
                GenerateDataClass(type, builder);

                builder.AppendLine();

                if (i < objectTypes.Length - 1)
                {
                    builder.AppendLine();
                }
            }

            builder.Append("#endregion");
        }
Пример #10
0
        public MultipleFileGenerationContext(
            GraphQlSchema schema,
            string outputDirectory,
            string @namespace,
            string projectFileName          = null,
            GeneratedObjectType objectTypes = GeneratedObjectType.DataClasses | GeneratedObjectType.QueryBuilders)
            : base(schema, objectTypes, 4)
        {
            if (!Directory.Exists(outputDirectory))
            {
                throw new ArgumentException($"Directory '{outputDirectory}' does not exist. ", nameof(outputDirectory));
            }

            if (String.IsNullOrWhiteSpace(@namespace))
            {
                throw new ArgumentException("namespace required", nameof(@namespace));
            }

            _outputDirectory = outputDirectory;
            _namespace       = @namespace;
            _projectFileName = projectFileName;
        }
        public static void GenerateQueryBuilder(GraphQlSchema schema, StringBuilder builder)
        {
            using (var reader = new StreamReader(typeof(GraphQlGenerator).Assembly.GetManifestResourceStream("GraphQlClientGenerator.BaseClasses")))
                builder.AppendLine(reader.ReadToEnd());

            builder.AppendLine("#region builder classes");

            GenerarateEnums(schema, builder);

            var complexTypes = schema.Types.Where(t => t.Kind == GraphQlTypeKindObject && !t.Name.StartsWith("__")).ToArray();

            for (var i = 0; i < complexTypes.Length; i++)
            {
                var type = complexTypes[i];
                GenerateTypeQueryBuilder(type, builder);

                if (i < complexTypes.Length - 1)
                {
                    builder.AppendLine();
                }
            }

            builder.Append("#endregion");
        }
        public static void GenerateDataClasses(GraphQlSchema schema, StringBuilder builder)
        {
            var inputTypes            = schema.Types.Where(t => t.Kind == GraphQlTypeKindInputObject && !t.Name.StartsWith("__")).ToArray();
            var hasInputType          = inputTypes.Any();
            var referencedObjectTypes = new HashSet <string>();

            if (hasInputType)
            {
                builder.AppendLine();
                builder.AppendLine("#region input classes");

                for (var i = 0; i < inputTypes.Length; i++)
                {
                    var type = inputTypes[i];
                    FindAllReferencedObjectTypes(schema, type, referencedObjectTypes);
                    GenerateDataClass(type.Name, "IGraphQlInputObject", builder, () => GenerateInputDataClassBody(type, type.InputFields.Cast <IGraphQlMember>().ToArray(), builder));

                    builder.AppendLine();

                    if (i < inputTypes.Length - 1)
                    {
                        builder.AppendLine();
                    }
                }

                builder.AppendLine("#endregion");
            }

            var complexTypes = schema.Types.Where(t => IsComplexType(t.Kind) && !t.Name.StartsWith("__")).ToArray();

            if (!complexTypes.Any())
            {
                return;
            }

            if (hasInputType)
            {
                builder.AppendLine();
            }

            builder.AppendLine("#region data classes");

            for (var i = 0; i < complexTypes.Length; i++)
            {
                var type = complexTypes[i];
                var hasInputReference = referencedObjectTypes.Contains(type.Name);
                var isInterface       = type.Kind == GraphQlTypeKindInterface;

                void GenerateBody(bool isInterfaceMember)
                {
                    var fieldsToGenerate = type.Fields?.Where(f => !f.IsDeprecated || GraphQlGeneratorConfiguration.IncludeDeprecatedFields).ToArray();

                    if (hasInputReference)
                    {
                        GenerateInputDataClassBody(type, fieldsToGenerate, builder);
                    }
                    else if (fieldsToGenerate != null)
                    {
                        foreach (var field in fieldsToGenerate)
                        {
                            GenerateDataProperty(type, field, isInterfaceMember, field.IsDeprecated, field.DeprecationReason, builder);
                        }
                    }
                }

                var interfacesToImplement = new List <string>();
                if (isInterface)
                {
                    interfacesToImplement.Add(GenerateInterface($"I{type.Name}", builder, () => GenerateBody(true)));
                    builder.AppendLine();
                    builder.AppendLine();
                }
                else if (type.Interfaces?.Count > 0)
                {
                    interfacesToImplement.AddRange(type.Interfaces.Select(x => $"I{x.Name}{GraphQlGeneratorConfiguration.ClassPostfix}"));
                }

                if (hasInputReference)
                {
                    interfacesToImplement.Add("IGraphQlInputObject");
                }

                GenerateDataClass(type.Name, String.Join(", ", interfacesToImplement), builder, () => GenerateBody(false));

                builder.AppendLine();

                if (i < complexTypes.Length - 1)
                {
                    builder.AppendLine();
                }
            }

            builder.AppendLine("#endregion");
        }
Пример #13
0
        public static void GenerateDataClasses(GraphQlSchema schema, StringBuilder builder)
        {
            var inputTypes            = schema.Types.Where(t => t.Kind == GraphQlTypeKindInputObject && !t.Name.StartsWith("__")).ToArray();
            var hasInputType          = inputTypes.Any();
            var referencedObjectTypes = new HashSet <string>();

            if (GraphQlGeneratorConfiguration.CSharpVersion == CSharpVersion.NewestWithNullableReferences)
            {
                builder.AppendLine("#nullable enable");
            }

            if (hasInputType)
            {
                builder.AppendLine();
                builder.AppendLine("#region input classes");

                for (var i = 0; i < inputTypes.Length; i++)
                {
                    var type = inputTypes[i];
                    FindAllReferencedObjectTypes(schema, type, referencedObjectTypes);
                    GenerateDataClass(type.Name, type.Description, "IGraphQlInputObject", builder, () => GenerateInputDataClassBody(type, type.InputFields.Cast <IGraphQlMember>().ToArray(), builder));

                    builder.AppendLine();

                    if (i < inputTypes.Length - 1)
                    {
                        builder.AppendLine();
                    }
                }

                builder.AppendLine("#endregion");
            }

            var complexTypes = schema.Types.Where(t => IsComplexType(t.Kind) && !t.Name.StartsWith("__")).ToArray();

            if (complexTypes.Any())
            {
                if (hasInputType)
                {
                    builder.AppendLine();
                }

                var complexTypeDictionary = complexTypes.ToDictionary(t => t.Name);

                builder.AppendLine("#region data classes");

                for (var i = 0; i < complexTypes.Length; i++)
                {
                    var type = complexTypes[i];
                    var hasInputReference = referencedObjectTypes.Contains(type.Name);
                    var fieldsToGenerate  = GetFieldsToGenerate(type, complexTypeDictionary);
                    var isInterface       = type.Kind == GraphQlTypeKindInterface;

                    void GenerateBody(bool isInterfaceMember)
                    {
                        if (hasInputReference)
                        {
                            GenerateInputDataClassBody(type, (ICollection <IGraphQlMember>)fieldsToGenerate, builder);
                        }
                        else if (fieldsToGenerate != null)
                        {
                            foreach (var field in fieldsToGenerate)
                            {
                                GenerateDataProperty(type, field, isInterfaceMember, field.IsDeprecated, field.DeprecationReason, true, _ => builder.Append(" { get; set; }"), builder);
                            }
                        }
                    }

                    var interfacesToImplement = new List <string>();
                    if (isInterface)
                    {
                        interfacesToImplement.Add(GenerateInterface($"I{type.Name}", type.Description, builder, () => GenerateBody(true)));
                        builder.AppendLine();
                        builder.AppendLine();
                    }
                    else if (type.Interfaces?.Count > 0)
                    {
                        var fieldNames = new HashSet <string>(fieldsToGenerate.Select(f => f.Name));

                        foreach (var @interface in type.Interfaces)
                        {
                            interfacesToImplement.Add($"I{@interface.Name}{GraphQlGeneratorConfiguration.ClassPostfix}");

                            foreach (var interfaceField in complexTypeDictionary[@interface.Name].Fields.Where(FilterDeprecatedFields))
                            {
                                if (fieldNames.Add(interfaceField.Name))
                                {
                                    fieldsToGenerate.Add(interfaceField);
                                }
                            }
                        }
                    }

                    if (hasInputReference)
                    {
                        interfacesToImplement.Add("IGraphQlInputObject");
                    }

                    GenerateDataClass(type.Name, type.Description, String.Join(", ", interfacesToImplement), builder, () => GenerateBody(false));

                    builder.AppendLine();

                    if (i < complexTypes.Length - 1)
                    {
                        builder.AppendLine();
                    }
                }

                builder.AppendLine("#endregion");
            }

            if (GraphQlGeneratorConfiguration.CSharpVersion == CSharpVersion.NewestWithNullableReferences)
            {
                builder.AppendLine("#nullable disable");
            }
        }
Пример #14
0
 public SingleFileGenerationContext(GraphQlSchema schema, TextWriter writer, GeneratedObjectType objectTypes = GeneratedObjectType.DataClasses | GeneratedObjectType.QueryBuilders, byte indentationSize = 0)
     : base(schema, objectTypes, indentationSize)
 {
     Writer       = writer ?? throw new ArgumentNullException(nameof(writer));
     _indentation = new String(' ', indentationSize);
 }
Пример #15
0
        public static void GenerateDataClasses(GraphQlSchema schema, StringBuilder builder)
        {
            var inputTypes            = schema.Types.Where(t => t.Kind == GraphQlTypeKindInputObject && !t.Name.StartsWith("__")).ToArray();
            var hasInputType          = inputTypes.Any();
            var referencedObjectTypes = new HashSet <string>();

            if (hasInputType)
            {
                builder.AppendLine();
                builder.AppendLine("#region input classes");

                for (var i = 0; i < inputTypes.Length; i++)
                {
                    var type = inputTypes[i];
                    FindAllReferencedObjectTypes(schema, type, referencedObjectTypes);
                    GenerateDataClass(type.Name, "IGraphQlInputObject", builder, () => GenerateInputDataClassBody(type, type.InputFields.Cast <IGraphQlMember>().ToArray(), builder));

                    builder.AppendLine();

                    if (i < inputTypes.Length - 1)
                    {
                        builder.AppendLine();
                    }
                }

                builder.AppendLine("#endregion");
            }

            var objectTypes = schema.Types.Where(t => t.Kind == GraphQlTypeKindObject && !t.Name.StartsWith("__")).ToArray();

            if (!objectTypes.Any())
            {
                return;
            }

            if (hasInputType)
            {
                builder.AppendLine();
            }

            builder.AppendLine("#region data classes");

            for (var i = 0; i < objectTypes.Length; i++)
            {
                var type = objectTypes[i];
                var hasInputReference = referencedObjectTypes.Contains(type.Name);
                GenerateDataClass(
                    type.Name,
                    hasInputReference ? "IGraphQlInputObject" : null,
                    builder,
                    () =>
                {
                    var fieldsToGenerate = type.Fields.Where(f => !f.IsDeprecated || GraphQlGeneratorConfiguration.IncludeDeprecatedFields).ToArray();
                    if (hasInputReference)
                    {
                        GenerateInputDataClassBody(type, fieldsToGenerate, builder);
                    }
                    else
                    {
                        foreach (var field in fieldsToGenerate)
                        {
                            GenerateDataProperty(type, field, field.IsDeprecated, field.DeprecationReason, builder);
                        }
                    }
                });

                builder.AppendLine();

                if (i < objectTypes.Length - 1)
                {
                    builder.AppendLine();
                }
            }

            builder.AppendLine("#endregion");
        }