コード例 #1
0
        public IEnumerable <CSharpDocument> Generate(
            ClientModel clientModel,
            string ns,
            string clientName)
        {
            if (clientModel is null)
            {
                throw new ArgumentNullException(nameof(clientModel));
            }

            var context = new MapperContext(ns, clientName);

            EnumDescriptorMapper.Map(clientModel, context);
            EntityIdFactoryDescriptorMapper.Map(clientModel, context);
            TypeDescriptorMapper.Map(clientModel, context);
            EntityTypeDescriptorMapper.Map(clientModel, context);
            OperationDescriptorMapper.Map(clientModel, context);
            ClientDescriptorMapper.Map(clientModel, context);
            ResultBuilderDescriptorMapper.Map(clientModel, context);
            DependencyInjectionMapper.Map(clientModel, context);
            DataTypeDescriptorMapper.Map(clientModel, context);

            var code = new StringBuilder();

            foreach (var descriptor in context.GetAllDescriptors())
            {
                foreach (var generator in _generators)
                {
                    if (generator.CanHandle(descriptor))
                    {
                        yield return(WriteDocument(generator, descriptor, code));
                    }
                }
            }
        }
コード例 #2
0
        public IEnumerable <CSharpDocument> Generate(
            ClientModel clientModel,
            string ns,
            string clientName)
        {
            if (clientModel is null)
            {
                throw new ArgumentNullException(nameof(clientModel));
            }

            var context = new MapperContext(ns, clientName);

            // 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);
            DependencyInjectionMapper.Map(clientModel, context);
            DataTypeDescriptorMapper.Map(clientModel, context);
            EntityTypeDescriptorMapper.Map(clientModel, context);
            ResultBuilderDescriptorMapper.Map(clientModel, context);

            // Lastly we generate the client mapper
            ClientDescriptorMapper.Map(clientModel, context);

            // Last we execute all our generators with the descriptiptors.
            var code = new StringBuilder();

            foreach (var descriptor in context.GetAllDescriptors())
            {
                foreach (var generator in _generators)
                {
                    if (generator.CanHandle(descriptor))
                    {
                        yield return(WriteDocument(generator, descriptor, code));
                    }
                }
            }
        }
コード例 #3
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.
            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));
        }
コード例 #4
0
ファイル: CSharpGenerator.cs プロジェクト: tkvw/hotchocolate
        public static CSharpGeneratorResult Generate(
            ClientModel clientModel,
            string clientName = "GraphQLClient",
            string @namespace = "StrawberryShake.GraphQL")
        {
            if (clientModel is null)
            {
                throw new ArgumentNullException(nameof(clientModel));
            }

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

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

            var context = new MapperContext(@namespace, clientName);

            // 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);
            DependencyInjectionMapper.Map(clientModel, context);
            DataTypeDescriptorMapper.Map(clientModel, context);
            EntityTypeDescriptorMapper.Map(clientModel, context);
            ResultBuilderDescriptorMapper.Map(clientModel, context);

            // Lastly we generate the client mapper
            ClientDescriptorMapper.Map(clientModel, context);

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

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

            return(new(documents));
        }
コード例 #5
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()));
        }