Esempio n. 1
0
        private bool TryGenerateClient(
            ClientGeneratorContext context,
            [NotNullWhen(true)] out CSharpGeneratorResult?result)
        {
            context.Log.BeginGenerateCode();

            try
            {
                result = CSharpGenerator.Generate(
                    context.GetDocuments(),
                    clientName: context.Settings.Name,
                    @namespace: context.GetNamespace(),
                    strictSchemaValidation: context.Settings.StrictSchemaValidation);
                return(true);
            }
            catch (GraphQLException ex)
            {
                context.ReportError(ex.Errors);
                result = null;
                return(false);
            }
            catch (Exception ex)
            {
                context.Log.Error(ex);
                context.ReportError(ex);
                result = null;
                return(false);
            }
            finally
            {
                context.Log.EndGenerateCode();
            }
        }
Esempio n. 2
0
        private bool TryGenerateClient(
            ClientGeneratorContext context,
            [NotNullWhen(true)] out CSharpGeneratorResult?result)
        {
            context.Log.BeginGenerateCode();

            try
            {
                var name       = context.Settings.Name;
                var @namespace = context.GetNamespace();
                var documents  = context.GetDocuments();

                result = new CSharpGenerator().Generate(documents, name, @namespace);
                return(true);
            }
            catch (GraphQLException ex)
            {
                context.ReportError(ex.Errors);
                result = null;
                return(false);
            }
            catch (Exception ex)
            {
                context.Log.Error(ex);
                context.ReportError(ex);
                result = null;
                return(false);
            }
            finally
            {
                context.Log.EndGenerateCode();
            }
        }
        private bool TryGenerateClient(
            ClientGeneratorContext context,
            [NotNullWhen(true)] out CSharpGeneratorResult?result)
        {
            context.Log.BeginGenerateCode();

            try
            {
                var settings = new CSharpGeneratorSettings
                {
                    ClientName             = context.Settings.Name,
                    Namespace              = context.GetNamespace(),
                    RequestStrategy        = context.Settings.RequestStrategy,
                    StrictSchemaValidation = context.Settings.StrictSchemaValidation,
                    NoStore         = context.Settings.NoStore,
                    InputRecords    = context.Settings.Records.Inputs,
                    RazorComponents = context.Settings.RazorComponents,
                    EntityRecords   = context.Settings.Records.Entities,
                    SingleCodeFile  = context.Settings.UseSingleFile,
                    HashProvider    = context.Settings.HashAlgorithm.ToLowerInvariant() switch
                    {
                        "sha1" => new Sha1DocumentHashProvider(HashFormat.Hex),
                        "sha256" => new Sha256DocumentHashProvider(HashFormat.Hex),
                        "md5" => new MD5DocumentHashProvider(HashFormat.Hex),
                        _ => new Sha1DocumentHashProvider(HashFormat.Hex)
                    }
                };

                if (context.Settings.TransportProfiles?
                    .Where(t => !string.IsNullOrEmpty(t.Name))
                    .ToList() is { Count : > 0 } profiles)
                {
                    settings.TransportProfiles.Clear();

                    foreach (var profile in profiles)
                    {
                        settings.TransportProfiles.Add(
                            new TransportProfile(
                                profile.Name,
                                profile.Default,
                                profile.Query,
                                profile.Mutation,
                                profile.Subscription));
                    }
                }

                var persistedQueryDirectory = context.GetPersistedQueryDirectory();

                context.Log.SetGeneratorSettings(settings);
                context.Log.SetPersistedQueryLocation(persistedQueryDirectory);

                if (settings.RequestStrategy == RequestStrategy.PersistedQuery &&
                    persistedQueryDirectory is null)
                {
                    settings.RequestStrategy = RequestStrategy.Default;
                }

                result = CSharpGenerator.Generate(context.GetDocuments(), settings);
                return(true);
            }
            catch (GraphQLException ex)
            {
                context.ReportError(ex.Errors);
                result = null;
                return(false);
            }
            catch (Exception ex)
            {
                context.Log.Error(ex);
                context.ReportError(ex);
                result = null;
                return(false);
            }
            finally
            {
                context.Log.EndGenerateCode();
            }
        }