public static CSharpGeneratorResult Generate(
            ClientModel clientModel,
            CSharpGeneratorSettings settings)
        {
            if (clientModel is null)
            {
                throw new ArgumentNullException(nameof(clientModel));
            }

            if (string.IsNullOrEmpty(settings.ClientName))
            {
                throw new ArgumentException(
                          string.Format(
                              Resources.CSharpGenerator_Generate_ArgumentCannotBeNull,
                              nameof(settings.ClientName)),
                          nameof(settings));
            }

            if (string.IsNullOrEmpty(settings.Namespace))
            {
                throw new ArgumentException(
                          string.Format(
                              Resources.CSharpGenerator_Generate_ArgumentCannotBeNull,
                              nameof(settings.Namespace)),
                          nameof(settings));
            }

            var context = new MapperContext(
                settings.Namespace,
                settings.ClientName,
                settings.HashProvider,
                settings.RequestStrategy,
                settings.TransportProfiles);

            // First we run all mappers that do not have any dependencies on others.
            EntityIdFactoryDescriptorMapper.Map(clientModel, context);

            // Second we start with the type descriptor mapper which creates
            // the type structure for the generators.
            // The following mappers can depend on this foundational data.
            TypeDescriptorMapper.Map(clientModel, context);

            // now we execute all mappers that depend on the previous type mappers.
            OperationDescriptorMapper.Map(clientModel, context);
            StoreAccessorMapper.Map(clientModel, context);
            DataTypeDescriptorMapper.Map(clientModel, context);
            EntityTypeDescriptorMapper.Map(clientModel, context);
            ResultBuilderDescriptorMapper.Map(clientModel, context);

            // We generate the client mapper next as we have all components of the client generated
            ClientDescriptorMapper.Map(clientModel, context);

            // Lastly we generate the DI code, as we now have collected everything
            DependencyInjectionMapper.Map(clientModel, context);

            // Last we execute all our generators with the descriptors.
            var code      = new StringBuilder();
            var documents = new List <SourceDocument>();

            foreach (var descriptor in context.GetAllDescriptors())
            {
                foreach (var generator in _generators)
                {
                    if (generator.CanHandle(descriptor))
                    {
                        documents.Add(WriteDocument(generator, descriptor, code));
                    }
                }
            }

            if (settings.RequestStrategy == RequestStrategy.PersistedQuery)
            {
                foreach (var operation in context.Operations)
                {
                    documents.Add(new SourceDocument(
                                      operation.Name,
                                      Encoding.UTF8.GetString(operation.Body),
                                      SourceDocumentKind.GraphQL,
                                      operation.HashValue));
                }
            }

            return(new(documents));
        }
Exemple #2
0
        public static CSharpGeneratorResult Generate(
            ClientModel clientModel,
            CSharpGeneratorSettings settings)
        {
            if (clientModel is null)
            {
                throw new ArgumentNullException(nameof(clientModel));
            }

            if (string.IsNullOrEmpty(settings.ClientName))
            {
                throw new ArgumentException(
                          string.Format(
                              Resources.CSharpGenerator_Generate_ArgumentCannotBeNull,
                              nameof(settings.ClientName)),
                          nameof(settings));
            }

            if (string.IsNullOrEmpty(settings.Namespace))
            {
                throw new ArgumentException(
                          string.Format(
                              Resources.CSharpGenerator_Generate_ArgumentCannotBeNull,
                              nameof(settings.Namespace)),
                          nameof(settings));
            }

            var context = new MapperContext(
                settings.Namespace,
                settings.ClientName,
                settings.HashProvider,
                settings.RequestStrategy,
                settings.TransportProfiles);

            // First we run all mappers that do not have any dependencies on others.
            EntityIdFactoryDescriptorMapper.Map(clientModel, context);

            // Second we start with the type descriptor mapper which creates
            // the type structure for the generators.
            // The following mappers can depend on this foundational data.
            TypeDescriptorMapper.Map(clientModel, context);

            // now we execute all mappers that depend on the previous type mappers.
            OperationDescriptorMapper.Map(clientModel, context);
            StoreAccessorMapper.Map(clientModel, context);
            DataTypeDescriptorMapper.Map(clientModel, context);
            EntityTypeDescriptorMapper.Map(clientModel, context);
            ResultBuilderDescriptorMapper.Map(clientModel, context);

            // We generate the client mapper next as we have all components of the client generated
            ClientDescriptorMapper.Map(clientModel, context);

            // Lastly we generate the DI code, as we now have collected everything
            DependencyInjectionMapper.Map(clientModel, context);

            // Last we execute all our generators with the descriptors.
            IReadOnlyList <GeneratorResult> results = GenerateCSharpDocuments(context, settings);

            var documents = new List <SourceDocument>();

            if (settings.SingleCodeFile)
            {
                GenerateSingleCSharpDocument(
                    results.Where(t => t.Result.IsCSharpDocument),
                    SourceDocumentKind.CSharp,
                    settings.ClientName,
                    documents);

                if (results.Any(t => t.Result.IsRazorComponent))
                {
                    GenerateSingleCSharpDocument(
                        results.Where(t => t.Result.IsRazorComponent),
                        SourceDocumentKind.Razor,
                        settings.ClientName,
                        documents);
                }
            }
            else
            {
                GenerateMultipleCSharpDocuments(
                    results.Where(t => t.Result.IsCSharpDocument),
                    SourceDocumentKind.CSharp,
                    documents);

                if (results.Any(t => t.Result.IsRazorComponent))
                {
                    GenerateMultipleCSharpDocuments(
                        results.Where(t => t.Result.IsRazorComponent),
                        SourceDocumentKind.Razor,
                        documents);
                }
            }

            // If persisted queries is enabled we will add the queries as documents.
            if (settings.RequestStrategy == RequestStrategy.PersistedQuery)
            {
                foreach (var operation in context.Operations)
                {
                    documents.Add(new SourceDocument(
                                      operation.Name,
                                      Encoding.UTF8.GetString(operation.Body),
                                      SourceDocumentKind.GraphQL,
                                      operation.HashValue));
                }
            }

            return(new(
                       documents,
                       clientModel.Operations
                       .Select(t => t.OperationType)
                       .Distinct()
                       .ToArray()));
        }