示例#1
0
 public CodeGenerator(CodeGeneratorSettings settings)
     : this(settings, new IIntrosepctionProvider[] {
     new SchemaIntrospection.JsonIntrospection(),
     new SchemaIntrospection.HttpIntrospection(),
     new SchemaIntrospection.SchemaFileIntrospection()
 })
 {
 }
示例#2
0
 public CodeGenerator(ILogger logger, CodeGeneratorSettings settings)
     : this(logger, settings, new IIntrosepctionProvider[] {
     new SchemaIntrospection.JsonIntrospection(),
     new SchemaIntrospection.HttpIntrospection(),
         #if DLL_INTROSPECTION
     new SchemaIntrospection.DllIntrospection(),
         #endif
     new SchemaIntrospection.SchemaFileIntrospection()
 })
 {
 }
示例#3
0
        private IEnumerable <CodeGeneratorSettings> LoadFromResolvedPath(string path)
        {
            // rootPath
            var dir = Path.GetDirectoryName(path);

            var json = File.ReadAllText(path);

            var simpleList = Newtonsoft.Json.JsonConvert.DeserializeObject <List <SimpleSettings> >(json, new SingleOrArrayConverter <SimpleSettings>());

            foreach (var simple in simpleList)
            {
                var settings = new CodeGeneratorSettings();

                if (simple.Schema.SchemaType() != SchemaSource.SchemaTypes.Http)
                {
                    // we are not and url based location then it must be a path
                    simple.Schema.Location = GetPath(dir, simple.Schema.Location);
                }

                settings.Schema      = simple.Schema;
                settings.OutputPath  = GetPath(dir, simple.Output);
                settings.SourcePaths = GetPaths(dir, simple.Source);
                settings.ClassName   = simple.Classname;
                settings.Namespace   = simple.Namespace;

                // we create a list of paths/resources based on the collection of named templates and the default set loaded in from the source file
                var           type          = Path.GetExtension(settings.OutputPath).Trim('.').ToLower();
                var           info          = typeof(CodeGeneratorSettings).GetTypeInfo();
                var           templateSet   = info.Namespace + ".Templates." + type + ".";
                List <string> templateFiles = new List <string>();
                var           templates     = typeof(CodeGeneratorSettings).GetTypeInfo().Assembly.GetManifestResourceNames().Where(x => x.StartsWith(templateSet, StringComparison.OrdinalIgnoreCase));
                templateFiles.AddRange(templates);

                var templatePaths = simple.Template.SelectMany(x => GetPaths(dir, x)).ToList();
                templateFiles.AddRange(templatePaths);

                settings.Templates    = templateFiles;
                settings.SettingsPath = path;
                yield return(settings);
            }
        }
        public static GraphQLDocument Parse(IEnumerable <NamedSource> parts, CodeGeneratorSettings settings)
        {
            List <LocatedNamedSource> sources = new List <LocatedNamedSource>();

            try
            {
                StringBuilder sb        = new StringBuilder();
                int           linecount = 0;

                var codeGenDirective = $"directive @{settings.TypeNameDirective}( type: String ) on FIELD_DEFINITION";
                linecount += codeGenDirective.Count(x => x == '\n') + 1;
                sb.Append(codeGenDirective);
                sb.Append('\n');

                foreach (NamedSource part in parts)
                {
                    int    start = sb.Length;
                    string body  = part.Body.Replace("\r", "");

                    sb.Append(body);
                    sb.Append("\n");
                    sources.Add(new LocatedNamedSource()
                    {
                        LineStartAt = linecount,
                        StartAt     = start,
                        EndAt       = sb.Length,
                        Path        = part.Path,
                        Body        = body
                    });

                    linecount += body.Count(x => x == '\n') + 1;
                }
                string final          = sb.ToString();
                var    parser         = new Parser(new Lexer());
                var    parsedDocument = parser.Parse(new Source(final));

                return(new GraphQLDocument(parsedDocument, sources, settings));
            }
            catch (GraphQLSyntaxErrorException ex)
            {
                var msg             = ex.ToString();
                var trimedStart     = msg.Substring("GraphQLParser.Exceptions.GraphQLSyntaxErrorException: Syntax Error GraphQL (".Length);
                var lineColSpliiter = trimedStart.IndexOf(':');
                var lineColEnder    = trimedStart.IndexOf(')');
                int line            = int.Parse(trimedStart.Substring(0, lineColSpliiter));
                int col             = int.Parse(trimedStart.Substring(lineColSpliiter + 1, lineColEnder - lineColSpliiter - 1));

                var endOfLine = trimedStart.IndexOf('\n');
                var message   = trimedStart.Substring(lineColEnder + 1, endOfLine - lineColEnder - 1).Trim();

                var source = sources.Where(x => x.LineStartAt <= line).OrderByDescending(x => x.LineStartAt).FirstOrDefault();
                return(GraphQLDocument.Error(new GraphQLError
                {
                    Path = source?.Path,
                    Line = source == null ? (int?)null : line - source.LineStartAt,
                    Column = col,
                    Code = ErrorCodes.SyntaxError,
                    Message = message
                }));
            }
            catch (Exception ex)
            {
                return(GraphQLDocument.Error(ErrorCodes.UnhandledException, ex.ToString()));
            }
        }
示例#5
0
 public CodeGenerator(CodeGeneratorSettings settings, IEnumerable <SchemaIntrospection.IIntrosepctionProvider> introspectionProviders)
 {
     this.settings = settings;
     this.introspectionProviders = introspectionProviders;
 }
        public static GraphQLDocument Parse(IEnumerable <NamedSource> parts, CodeGeneratorSettings settings)
        {
            List <LocatedNamedSource> sources = new List <LocatedNamedSource>();

            try
            {
                StringBuilder sb        = new StringBuilder();
                int           linecount = 0;

                var codeGenDirective = $"directive @{settings.TypeNameDirective}( type: String ) on FIELD_DEFINITION";
                linecount += codeGenDirective.Count(x => x == '\n') + 1;
                sb.Append(codeGenDirective);
                sb.Append('\n');

                foreach (NamedSource part in parts)
                {
                    int    start = sb.Length;
                    string body  = part.Body.Replace("\r", "");

                    sb.Append(body);
                    sb.Append("\n");
                    sources.Add(new LocatedNamedSource()
                    {
                        LineStartAt = linecount,
                        StartAt     = start,
                        EndAt       = sb.Length,
                        Path        = part.Path,
                        Body        = body
                    });

                    linecount += body.Count(x => x == '\n') + 1;
                }
                string final = sb.ToString();

                var bytes    = Encoding.UTF8.GetBytes(final);
                var hcparser = new HotChocolate.Language.Utf8GraphQLParser(bytes);
                var doc      = hcparser.Parse();

                //var parser = new Parser(new Lexer());
                //var parsedDocument = parser.Parse(new Source(final));

                return(new GraphQLDocument(doc, sources, settings));
            }
            catch (HotChocolate.Language.SyntaxException sex)
            {
                var message = sex.Message;

                int col  = sex.Column;
                int line = sex.Line;

                var source = sources.Where(x => x.LineStartAt <= line).OrderByDescending(x => x.LineStartAt).FirstOrDefault();
                return(GraphQLDocument.Error(new GraphQLError
                {
                    Path = source?.Path,
                    Line = source == null ? (int?)null : line - source.LineStartAt,
                    Column = col,
                    Code = ErrorCodes.SyntaxError,
                    Message = message
                }));
            }
            catch (Exception fex)
            {
                var t = fex;
                return(GraphQLDocument.Error(ErrorCodes.UnhandledException, fex.ToString()));
            }
        }