Пример #1
0
        public FieldHelper(
            FieldCollector fieldCollector,
            DirectiveLookup directives,
            IVariableCollection variables,
            ICollection <IError> errors)
        {
            if (directives == null)
            {
                throw new ArgumentNullException(nameof(directives));
            }

            if (variables == null)
            {
                throw new ArgumentNullException(nameof(variables));
            }

            if (errors == null)
            {
                throw new ArgumentNullException(nameof(errors));
            }

            _fieldCollector = fieldCollector;;
            _errors         = errors;
            _directives     = directives;
        }
Пример #2
0
        public OperationExecuter(
            ISchema schema,
            DocumentNode queryDocument,
            OperationDefinitionNode operation)
        {
            _schema = schema
                      ?? throw new ArgumentNullException(nameof(schema));
            _queryDocument = queryDocument
                             ?? throw new ArgumentNullException(nameof(queryDocument));
            _operation = operation
                         ?? throw new ArgumentNullException(nameof(operation));

            _executionTimeout = schema.Options.ExecutionTimeout;

            _variableValueBuilder = new VariableValueBuilder(schema, _operation);

            if (!_executionStrategy.TryGetValue(_operation.Operation,
                                                out IExecutionStrategy strategy))
            {
                throw new NotSupportedException();
            }
            _strategy = strategy;

            var directiveCollector = new DirectiveCollector(_schema);

            directiveCollector.VisitDocument(_queryDocument);
            _directiveLookup = directiveCollector.CreateLookup();
        }
Пример #3
0
        private static IFieldHelper CreateFieldHelper(
            IVariableCollection variables,
            FragmentCollection fragments,
            DirectiveLookup directives,
            ICollection <IError> errors)
        {
            var fieldCollector = new FieldCollector(
                variables, fragments);

            return(new FieldHelper(
                       fieldCollector, directives,
                       variables, errors));
        }
Пример #4
0
        private IExecutionContext CreateExecutionContext(IQueryContext context)
        {
            DirectiveLookup directives = GetOrCreateDirectiveLookup(context);

            return(new ExecutionContext(
                       context.Schema,
                       context.ServiceScope,
                       context.Operation,
                       context.Variables,
                       directives,
                       context.ContextData,
                       context.RequestAborted));
        }
Пример #5
0
        public ExecutionContext(
            ISchema schema,
            DirectiveLookup directiveLookup,
            DocumentNode queryDocument,
            OperationDefinitionNode operation,
            OperationRequest request,
            VariableCollection variables,
            CancellationToken cancellationToken)
        {
            if (request == null)
            {
                throw new ArgumentNullException(nameof(request));
            }

            Schema = schema
                     ?? throw new ArgumentNullException(nameof(schema));
            _directiveLookup = directiveLookup
                               ?? throw new ArgumentNullException(nameof(directiveLookup));
            QueryDocument = queryDocument
                            ?? throw new ArgumentNullException(nameof(queryDocument));
            Operation = operation
                        ?? throw new ArgumentNullException(nameof(operation));
            Variables = variables
                        ?? throw new ArgumentNullException(nameof(variables));

            Services       = _serviceFactory.Services = request.Services;
            _session       = request.Session;
            _resolverCache = request.Session?.CustomContexts
                             .GetCustomContext <IResolverCache>();

            Fragments       = new FragmentCollection(schema, queryDocument);
            _fieldCollector = new FieldCollector(variables, Fragments);
            OperationType   = schema.GetOperationType(operation.Operation);
            RootValue       = ResolveRootValue(request.Services, schema,
                                               OperationType, request.InitialValue);

            if (RootValue == null)
            {
                RootValue         = CreateRootValue(Services, schema, OperationType);
                _disposeRootValue = true;
            }

            CancellationToken = cancellationToken;
            _clone            = c => new ExecutionContext(
                schema, directiveLookup, queryDocument,
                operation, CreateOperationRequest(
                    schema, request.VariableValues,
                    request.InitialValue),
                variables, c);
        }
        public void CloneExecutionContext()
        {
            // arrange
            var schema = Schema.Create(
                "type Query { foo: String }",
                c => c.Use(next => context => Task.CompletedTask));

            var query = Parser.Default.Parse("{ foo }");

            var errorHandler = new Mock <IErrorHandler>();

            var services = new Mock <IServiceProvider>();

            services.Setup(t => t.GetService(typeof(IErrorHandler)))
            .Returns(errorHandler.Object);

            IRequestServiceScope serviceScope = services.Object
                                                .CreateRequestServiceScope();

            var operation = new Mock <IOperation>();

            operation.Setup(t => t.Query).Returns(query);

            var variables = new Mock <IVariableCollection>();

            var directives = new DirectiveLookup(new Dictionary <ObjectType, IDictionary <FieldNode, IReadOnlyCollection <IDirective> > >());

            var contextData = new Dictionary <string, object>
            {
                { "abc", "123" }
            };

            // act
            var executionContext = new ExecutionContext(
                schema, serviceScope, operation.Object,
                variables.Object, directives, contextData,
                CancellationToken.None);
            IExecutionContext cloned = executionContext.Clone();

            // assert
            Assert.Equal(contextData, executionContext.ContextData);
            Assert.Equal(contextData, cloned.ContextData);
            Assert.False(object.ReferenceEquals(
                             executionContext.Result, cloned.Result));
            Assert.False(object.ReferenceEquals(
                             executionContext.ContextData, cloned.ContextData));
        }
Пример #7
0
        private IExecutionContext CreateExecutionContext(IQueryContext context)
        {
            DirectiveLookup directiveLookup = _directiveCache.GetOrCreate(
                context.Request.Query,
                () => CreateLookup(context.Schema, context.Document));

            OperationRequest request = CreateOperationRequest(context);

            VariableCollection variables = CoerceVariables(
                context.Schema, context.Operation,
                request.VariableValues);

            var executionContext = new ExecutionContext(
                context.Schema, directiveLookup, context.Document,
                context.Operation, request, variables,
                context.RequestAborted);

            return(executionContext);
        }
Пример #8
0
        public ExecutionContext(
            ISchema schema,
            DirectiveLookup directiveLookup,
            DocumentNode queryDocument,
            OperationDefinitionNode operation,
            OperationRequest request,
            VariableCollection variables,
            CancellationToken requestAborted)
        {
            Schema = schema
                     ?? throw new ArgumentNullException(nameof(schema));
            _directiveLookup = directiveLookup
                               ?? throw new ArgumentNullException(nameof(directiveLookup));
            QueryDocument = queryDocument
                            ?? throw new ArgumentNullException(nameof(queryDocument));
            Operation = operation
                        ?? throw new ArgumentNullException(nameof(operation));
            Variables = variables
                        ?? throw new ArgumentNullException(nameof(variables));
            _request = request
                       ?? throw new ArgumentNullException(nameof(request));

            Services       = _serviceFactory.Services = request.Services;
            _resolverCache = request.Session?.CustomContexts
                             .GetCustomContext <IResolverCache>();
            ErrorHandler = Services.GetRequiredService <IErrorHandler>();

            Fragments       = new FragmentCollection(schema, queryDocument);
            _fieldCollector = new FieldCollector(variables, Fragments);
            OperationType   = schema.GetOperationType(operation.Operation);
            RootValue       = ResolveRootValue(request.Services, schema,
                                               OperationType, request.InitialValue);

            if (RootValue == null)
            {
                RootValue         = CreateRootValue(Services, schema, OperationType);
                _disposeRootValue = true;
            }

            RequestAborted = requestAborted;
        }
Пример #9
0
        public ExecutionContext(
            ISchema schema,
            IRequestServiceScope serviceScope,
            IOperation operation,
            IVariableCollection variables,
            DirectiveLookup directives,
            IDictionary <string, object> contextData,
            CancellationToken requestAborted)
        {
            Schema = schema
                     ?? throw new ArgumentNullException(nameof(schema));
            ServiceScope = serviceScope
                           ?? throw new ArgumentNullException(nameof(serviceScope));
            Operation = operation
                        ?? throw new ArgumentNullException(nameof(operation));
            Variables = variables
                        ?? throw new ArgumentNullException(nameof(variables));
            Directives = directives
                         ?? throw new ArgumentNullException(nameof(directives));
            ContextData = contextData
                          ?? throw new ArgumentNullException(nameof(contextData));
            RequestAborted = requestAborted;

            ErrorHandler = serviceScope.ServiceProvider
                           .GetRequiredService <IErrorHandler>();

            Result = new QueryResult();

            FieldHelper = CreateFieldHelper(
                variables,
                new FragmentCollection(schema, operation.Query),
                directives,
                Result.Errors);

            Activator = new Activator(serviceScope.ServiceProvider);
        }