<commit_before>using GraphQL.Caching;
using GraphQL.DI;
using GraphQL.Execution;
using GraphQL.Instrumentation;
using GraphQL.Types;
using GraphQL.Utilities;
using GraphQL.Validation;
using GraphQL.Validation.Complexity;
using GraphQLParser;
using GraphQLParser.AST;
using ExecutionContext = GraphQL.Execution.ExecutionContext;

namespace GraphQL
{
    /// <summary>
    /// <inheritdoc cref="IDocumentExecuter"/>
    /// <br/><br/>
    /// Default implementation for <see cref="IDocumentExecuter"/>.
    /// </summary>
    public class DocumentExecuter : IDocumentExecuter
    {
        private readonly IDocumentBuilder _documentBuilder;
        private readonly IDocumentValidator _documentValidator;
        private readonly IComplexityAnalyzer _complexityAnalyzer;
        private readonly IDocumentCache _documentCache;
        private readonly IConfigureExecutionOptions[]? _configurations;
        private readonly IExecutionStrategySelector _executionStrategySelector;

        /// <summary>
        /// Initializes a new instance with default <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/> and <see cref="IComplexityAnalyzer"/> instances,
        /// and without document caching.
        /// </summary>
        public DocumentExecuter()
            : this(new GraphQLDocumentBuilder(), new DocumentValidator(), new ComplexityAnalyzer(), DefaultDocumentCache.Instance)
        {
        }

        /// <summary>
        /// Initializes a new instance with specified <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/> and <see cref="IComplexityAnalyzer"/> instances,
        /// and without document caching.
        /// </summary>
        public DocumentExecuter(IDocumentBuilder documentBuilder, IDocumentValidator documentValidator, IComplexityAnalyzer complexityAnalyzer)
            : this(documentBuilder, documentValidator, complexityAnalyzer, DefaultDocumentCache.Instance)
        {
        }

        /// <summary>
        /// Initializes a new instance with specified <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/>, <see cref="IComplexityAnalyzer"/>,
        /// and <see cref="IDocumentCache"/> instances.
        /// </summary>
        public DocumentExecuter(IDocumentBuilder documentBuilder, IDocumentValidator documentValidator, IComplexityAnalyzer complexityAnalyzer, IDocumentCache documentCache)
            : this(documentBuilder, documentValidator, complexityAnalyzer, documentCache, null!)
        {
        }

        /// <summary>
        /// Initializes a new instance with specified <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/>, <see cref="IComplexityAnalyzer"/>,
        /// <see cref="IDocumentCache"/> and a set of <see cref="IConfigureExecutionOptions"/> instances.
        /// </summary>
        public DocumentExecuter(IDocumentBuilder documentBuilder, IDocumentValidator documentValidator, IComplexityAnalyzer complexityAnalyzer, IDocumentCache documentCache, IEnumerable<IConfigureExecutionOptions> configurations)
            : this(documentBuilder, documentValidator, complexityAnalyzer, documentCache, configurations, new DefaultExecutionStrategySelector())
        {
        }

        /// <summary>
        /// Initializes a new instance with the specified <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/>, <see cref="IComplexityAnalyzer"/>,
        /// <see cref="IDocumentCache"/> and a set of <see cref="IConfigureExecutionOptions"/> instances.
        /// </summary>
        public DocumentExecuter(IDocumentBuilder documentBuilder, IDocumentValidator documentValidator, IComplexityAnalyzer complexityAnalyzer, IDocumentCache documentCache, IEnumerable<IConfigureExecutionOptions> configurations, IExecutionStrategySelector executionStrategySelector)
        {
            _documentBuilder = documentBuilder ?? throw new ArgumentNullException(nameof(documentBuilder));
            _documentValidator = documentValidator ?? throw new ArgumentNullException(nameof(documentValidator));
            _complexityAnalyzer = complexityAnalyzer ?? throw new ArgumentNullException(nameof(complexityAnalyzer));
            _documentCache = documentCache ?? throw new ArgumentNullException(nameof(documentCache));
            _configurations = configurations?.ToArray();
            _executionStrategySelector = executionStrategySelector ?? throw new ArgumentNullException(nameof(executionStrategySelector));
        }

        /// <inheritdoc/>
        public virtual async Task<ExecutionResult> ExecuteAsync(ExecutionOptions options)
        {
            if (options == null)
                throw new ArgumentNullException(nameof(options));
            if (options.Schema == null)
                throw new InvalidOperationException("Cannot execute request if no schema is specified");
            if (options.Query == null && options.Document == null)
                throw new InvalidOperationException("Cannot execute request if no query is specified");

            if (_configurations != null)
            {
                foreach (var configuration in _configurations)
                {
                    // allocation free when the configuration delegate is not asynchronous
                    await configuration.ConfigureAsync(options).ConfigureAwait(false);
                }
            }

            var metrics = (options.EnableMetrics ? new Metrics() : Metrics.None).Start(options.OperationName);

            ExecutionResult? result = null;
            ExecutionContext? context = null;
            bool executionOccurred = false;

            try
            {
                if (!options.Schema.Initialized)
                {
                    using (metrics.Subject("schema", "Initializing schema"))
                    {
                        options.Schema.Initialize();
                    }
                }

                var document = options.Document;<commit_msg>Allow execution configurations to set Schema and Query (#3119)<commit_after>using GraphQL.Caching;
using GraphQL.DI;
using GraphQL.Execution;
using GraphQL.Instrumentation;
using GraphQL.Types;
using GraphQL.Utilities;
using GraphQL.Validation;
using GraphQL.Validation.Complexity;
using GraphQLParser;
using GraphQLParser.AST;
using ExecutionContext = GraphQL.Execution.ExecutionContext;

namespace GraphQL
{
    /// <summary>
    /// <inheritdoc cref="IDocumentExecuter"/>
    /// <br/><br/>
    /// Default implementation for <see cref="IDocumentExecuter"/>.
    /// </summary>
    public class DocumentExecuter : IDocumentExecuter
    {
        private readonly IDocumentBuilder _documentBuilder;
        private readonly IDocumentValidator _documentValidator;
        private readonly IComplexityAnalyzer _complexityAnalyzer;
        private readonly IDocumentCache _documentCache;
        private readonly IConfigureExecutionOptions[]? _configurations;
        private readonly IExecutionStrategySelector _executionStrategySelector;

        /// <summary>
        /// Initializes a new instance with default <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/> and <see cref="IComplexityAnalyzer"/> instances,
        /// and without document caching.
        /// </summary>
        public DocumentExecuter()
            : this(new GraphQLDocumentBuilder(), new DocumentValidator(), new ComplexityAnalyzer(), DefaultDocumentCache.Instance)
        {
        }

        /// <summary>
        /// Initializes a new instance with specified <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/> and <see cref="IComplexityAnalyzer"/> instances,
        /// and without document caching.
        /// </summary>
        public DocumentExecuter(IDocumentBuilder documentBuilder, IDocumentValidator documentValidator, IComplexityAnalyzer complexityAnalyzer)
            : this(documentBuilder, documentValidator, complexityAnalyzer, DefaultDocumentCache.Instance)
        {
        }

        /// <summary>
        /// Initializes a new instance with specified <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/>, <see cref="IComplexityAnalyzer"/>,
        /// and <see cref="IDocumentCache"/> instances.
        /// </summary>
        public DocumentExecuter(IDocumentBuilder documentBuilder, IDocumentValidator documentValidator, IComplexityAnalyzer complexityAnalyzer, IDocumentCache documentCache)
            : this(documentBuilder, documentValidator, complexityAnalyzer, documentCache, null!)
        {
        }

        /// <summary>
        /// Initializes a new instance with specified <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/>, <see cref="IComplexityAnalyzer"/>,
        /// <see cref="IDocumentCache"/> and a set of <see cref="IConfigureExecutionOptions"/> instances.
        /// </summary>
        public DocumentExecuter(IDocumentBuilder documentBuilder, IDocumentValidator documentValidator, IComplexityAnalyzer complexityAnalyzer, IDocumentCache documentCache, IEnumerable<IConfigureExecutionOptions> configurations)
            : this(documentBuilder, documentValidator, complexityAnalyzer, documentCache, configurations, new DefaultExecutionStrategySelector())
        {
        }

        /// <summary>
        /// Initializes a new instance with the specified <see cref="IDocumentBuilder"/>,
        /// <see cref="IDocumentValidator"/>, <see cref="IComplexityAnalyzer"/>,
        /// <see cref="IDocumentCache"/> and a set of <see cref="IConfigureExecutionOptions"/> instances.
        /// </summary>
        public DocumentExecuter(IDocumentBuilder documentBuilder, IDocumentValidator documentValidator, IComplexityAnalyzer complexityAnalyzer, IDocumentCache documentCache, IEnumerable<IConfigureExecutionOptions> configurations, IExecutionStrategySelector executionStrategySelector)
        {
            _documentBuilder = documentBuilder ?? throw new ArgumentNullException(nameof(documentBuilder));
            _documentValidator = documentValidator ?? throw new ArgumentNullException(nameof(documentValidator));
            _complexityAnalyzer = complexityAnalyzer ?? throw new ArgumentNullException(nameof(complexityAnalyzer));
            _documentCache = documentCache ?? throw new ArgumentNullException(nameof(documentCache));
            _configurations = configurations?.ToArray();
            _executionStrategySelector = executionStrategySelector ?? throw new ArgumentNullException(nameof(executionStrategySelector));
        }

        /// <inheritdoc/>
        public virtual async Task<ExecutionResult> ExecuteAsync(ExecutionOptions options)
        {
            if (options == null)
                throw new ArgumentNullException(nameof(options));

            if (_configurations != null)
            {
                foreach (var configuration in _configurations)
                {
                    // allocation free when the configuration delegate is not asynchronous
                    await configuration.ConfigureAsync(options).ConfigureAwait(false);
                }
            }

            if (options.Schema == null)
                throw new InvalidOperationException("Cannot execute request if no schema is specified");
            if (options.Query == null && options.Document == null)
                throw new InvalidOperationException("Cannot execute request if no query is specified");

            var metrics = (options.EnableMetrics ? new Metrics() : Metrics.None).Start(options.OperationName);

            ExecutionResult? result = null;
            ExecutionContext? context = null;
            bool executionOccurred = false;

            try
            {
                if (!options.Schema.Initialized)
                {
                    using (metrics.Subject("schema", "Initializing schema"))
                    {
                        options.Schema.Initialize();
                    }
                }

                var document = options.Document;
{'fraction_non_alphanumeric': 0.09198936347016785, 'fraction_numerical': 0.00033239155725444574, 'mean_word_length': 2.8182106598984773, 'pattern_counts': {'":': 0, '<': 83, '<?xml version=': 0, '>': 83, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '357051', 'n_tokens_mistral': 2694, 'n_tokens_neox': 2633, 'n_words': 699}