Beispiel #1
0
        private async void OnExecute()
        {
            try
            {
                Console.WriteLine($"Loading {SchemaFile}...");
                var schemaText = File.ReadAllText(SchemaFile);

                var mappings = new Dictionary <string, string>();
                if (!string.IsNullOrEmpty(ScalarMapping))
                {
                    mappings = ScalarMapping.Split(',').Select(s => s.Split('=')).ToDictionary(k => k[0], v => v[1]);
                }

                // parse into AST
                var typeInfo = SchemaCompiler.Compile(schemaText, mappings);

                Console.WriteLine($"Generating types in namespace {Namespace}, outputting to {ClientClassName}.cs");

                // pass the schema to the template
                var engine = new RazorLightEngineBuilder()
                             .UseEmbeddedResourcesProject(typeof(Program))
                             .UseMemoryCachingProvider()
                             .Build();

                var allTypes = typeInfo.Types.Concat(typeInfo.Inputs).ToDictionary(k => k.Key, v => v.Value);

                string result = await engine.CompileRenderAsync("types.cshtml", new
                {
                    Namespace  = Namespace,
                    SchemaFile = SchemaFile,
                    Types      = allTypes,
                    Enums      = typeInfo.Enums,
                    Mutation   = typeInfo.Mutation,
                    CmdArgs    = $"-n {Namespace} -c {ClientClassName} -m {ScalarMapping}"
                });

                Directory.CreateDirectory(OutputDir);
                File.WriteAllText($"{OutputDir}/GeneratedTypes.cs", result);

                result = await engine.CompileRenderAsync("client.cshtml", new
                {
                    Namespace       = Namespace,
                    SchemaFile      = SchemaFile,
                    Query           = typeInfo.Query,
                    Mutation        = typeInfo.Mutation,
                    ClientClassName = ClientClassName,
                });

                File.WriteAllText($"{OutputDir}/{ClientClassName}.cs", result);

                Console.WriteLine($"Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());
            }
        }
Beispiel #2
0
        private async void OnExecute()
        {
            try
            {
                Uri  uriResult;
                bool isGraphQlEndpoint = Uri.TryCreate(Source, UriKind.Absolute, out uriResult) &&
                                         (uriResult.Scheme == Uri.UriSchemeHttp || uriResult.Scheme == Uri.UriSchemeHttps);

                string schemaText          = null;
                bool   isIntroSpectionFile = false;

                if (isGraphQlEndpoint)
                {
                    Console.WriteLine($"Loading from {Source}...");
                    using (var httpClient = new HttpClient())
                    {
                        foreach (var header in SplitMultiValueArgument(HeaderValues))
                        {
                            httpClient.DefaultRequestHeaders.Add(header.Key, header.Value);
                        }

                        Dictionary <string, string> request = new Dictionary <string, string>();
                        request["query"]         = IntroSpectionQuery.Query;
                        request["operationName"] = "IntrospectionQuery";

                        var response = httpClient
                                       .PostAsync(Source,
                                                  new StringContent(JsonConvert.SerializeObject(request), Encoding.UTF8, "application/json")).GetAwaiter().GetResult();

                        schemaText = await response.Content.ReadAsStringAsync();

                        isIntroSpectionFile = true;
                    }
                }
                else
                {
                    Console.WriteLine($"Loading {Source}...");
                    schemaText          = File.ReadAllText(Source);
                    isIntroSpectionFile = Path.GetExtension(Source).Equals(".json", StringComparison.OrdinalIgnoreCase);
                }

                var mappings = new Dictionary <string, string>();
                if (!string.IsNullOrEmpty(ScalarMapping))
                {
                    SplitMultiValueArgument(ScalarMapping).ToList().ForEach(i => {
                        dotnetToGqlTypeMappings[i.Value] = i.Key;
                        mappings[i.Key] = i.Value;
                    });
                }

                // parse into AST
                var typeInfo = !isIntroSpectionFile?
                               SchemaCompiler.Compile(schemaText, mappings) :
                                   IntrospectionCompiler.Compile(schemaText, mappings);

                Console.WriteLine($"Generating types in namespace {Namespace}, outputting to {ClientClassName}.cs");

                // pass the schema to the template
                var engine = new RazorLightEngineBuilder()
                             .UseEmbeddedResourcesProject(typeof(Program))
                             .UseMemoryCachingProvider()
                             .Build();

                var allTypes = typeInfo.Types.Concat(typeInfo.Inputs).ToDictionary(k => k.Key, v => v.Value);

                string result = await engine.CompileRenderAsync("types.cshtml", new
                {
                    Namespace  = Namespace,
                    SchemaFile = Source,
                    Types      = allTypes,
                    Enums      = typeInfo.Enums,
                    Mutation   = typeInfo.Mutation,
                    CmdArgs    = $"-n {Namespace} -c {ClientClassName} -m {ScalarMapping}"
                });

                Directory.CreateDirectory(OutputDir);
                File.WriteAllText($"{OutputDir}/GeneratedTypes.cs", result);

                result = await engine.CompileRenderAsync("client.cshtml", new
                {
                    Namespace       = Namespace,
                    SchemaFile      = Source,
                    Query           = typeInfo.Query,
                    Mutation        = typeInfo.Mutation,
                    ClientClassName = ClientClassName,
                    Mappings        = dotnetToGqlTypeMappings
                });

                File.WriteAllText($"{OutputDir}/{ClientClassName}.cs", result);

                Console.WriteLine($"Done.");
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e.ToString());
            }
        }