Пример #1
0
        public void EnsureCapacity(int size)
        {
            // arrange
            var resultMap = new ResultMap();

            // act
            resultMap.EnsureCapacity(size);

            // assert
            Assert.Equal(size, resultMap.Count);
        }
Пример #2
0
        public Task <IQueryResult> ExecuteAsync(
            IOperationContext operationContext)
        {
            if (operationContext is null)
            {
                throw new ArgumentNullException(nameof(operationContext));
            }

            ISelectionSet selectionSet            = operationContext.Operation.GetRootSelectionSet();
            IReadOnlyList <ISelection> selections = selectionSet.Selections;
            ResultMap resultMap = operationContext.Result.RentResultMap(selections.Count);

            return(ExecuteSelectionsAsync(operationContext, selections, resultMap));
        }
Пример #3
0
            private async ValueTask <ISourceStream> SubscribeAsync()
            {
                OperationContext operationContext = _operationContextPool.Get();

                try
                {
                    object?rootValue = RootValueResolver.TryResolve(
                        _requestContext,
                        _requestContext.Services,
                        _subscriptionType,
                        ref _cachedRootValue);

                    operationContext.Initialize(
                        _requestContext,
                        _requestContext.Services,
                        NoopBatchDispatcher.Default,
                        _requestContext.Operation !,
                        rootValue,
                        _requestContext.Variables !);

                    ResultMap  resultMap     = operationContext.Result.RentResultMap(1);
                    ISelection rootSelection = _rootSelections.Selections[0];

                    var middlewareContext = new MiddlewareContext();
                    middlewareContext.Initialize(
                        operationContext,
                        _rootSelections.Selections[0],
                        resultMap,
                        1,
                        rootValue,
                        Path.New(_rootSelections.Selections[0].ResponseName),
                        ImmutableDictionary <string, object?> .Empty);

                    ISourceStream sourceStream =
                        await rootSelection.Field.SubscribeResolver !.Invoke(middlewareContext)
                        .ConfigureAwait(false);

                    if (operationContext.Result.Errors.Count > 0)
                    {
                        throw new GraphQLException(operationContext.Result.Errors);
                    }

                    return(sourceStream);
                }
                finally
                {
                    operationContext.Result.DropResult();
                    _operationContextPool.Return(operationContext);
                }
            }
        public static ResultMap EnqueueResolverTasks(
            this ISelectionSet selectionSet,
            IOperationContext operationContext,
            Path path,
            IImmutableDictionary <string, object?> scopedContext,
            object?parent)
        {
            var responseIndex = 0;
            IReadOnlyList <ISelection> selections = selectionSet.Selections;
            ResultMap resultMap = operationContext.Result.RentResultMap(selections.Count);

            for (var i = 0; i < selections.Count; i++)
            {
                ISelection selection = selections[i];
                if (selection.IsIncluded(operationContext.Variables))
                {
                    operationContext.Execution.TaskBacklog.Register(
                        new ResolverTaskDefinition(
                            operationContext,
                            selection,
                            responseIndex++,
                            resultMap,
                            parent,
                            path.Append(selection.ResponseName),
                            scopedContext));
                }
            }

            if (selectionSet.Fragments.Count > 0)
            {
                IReadOnlyList <IFragment> fragments = selectionSet.Fragments;
                for (var i = 0; i < fragments.Count; i++)
                {
                    IFragment fragment = fragments[i];
                    if (!fragment.IsConditional)
                    {
                        operationContext.Execution.DeferredTaskBacklog.Register(
                            new DeferredFragment(
                                fragment,
                                fragment.GetLabel(operationContext.Variables),
                                path,
                                parent,
                                scopedContext));
                    }
                }
            }

            return(resultMap);
        }
Пример #5
0
        public void BuildResult_SimpleResultSet_SnapshotMatches()
        {
            // arrange
            var       helper = new ResultHelper(CreatePool());
            ResultMap map    = helper.RentResultMap(1);

            map.SetValue(0, "abc", "def", false);
            helper.SetData(map);

            // act
            IQueryResult result = helper.BuildResult();

            // assert
            result.ToJson().MatchSnapshot();
        }
Пример #6
0
 public ResolverTaskDefinition(
     IOperationContext operationContext,
     ISelection selection,
     int responseIndex,
     ResultMap resultMap,
     object?parent,
     Path path,
     IImmutableDictionary <string, object?> scopedContextData)
 {
     OperationContext  = operationContext;
     Selection         = selection;
     ResponseIndex     = responseIndex;
     ResultMap         = resultMap;
     Parent            = parent;
     Path              = path;
     ScopedContextData = scopedContextData;
 }
Пример #7
0
        public void GetValue_ValueIsFound(int capacity)
        {
            // arrange
            var resultMap = new ResultMap();

            resultMap.EnsureCapacity(capacity);
            resultMap.SetValue(0, "abc", "def");
            resultMap.SetValue(1, "def", "def");
            resultMap.SetValue(2, "ghi", "def");

            // act
            ResultValue value = resultMap.GetValue("def", out int index);

            // assert
            Assert.Equal("def", value.Name);
            Assert.Equal(1, index);
        }
Пример #8
0
        public void ContainsKey(int capacity)
        {
            // arrange
            var resultMap = new ResultMap();

            resultMap.EnsureCapacity(capacity);
            resultMap.SetValue(0, "abc", "def");
            resultMap.SetValue(capacity / 2, "def", "def");
            resultMap.SetValue(capacity - 1, "ghi", "def");

            IReadOnlyDictionary <string, object> dict = resultMap;

            // act
            var found = dict.ContainsKey("def");

            // assert
            Assert.True(found);
        }
Пример #9
0
        public void SetValue()
        {
            // arrange
            var resultMap = new ResultMap();

            resultMap.EnsureCapacity(1);

            // act
            resultMap.SetValue(0, "abc", "def");

            // assert
            Assert.Collection(
                (IEnumerable <ResultValue>)resultMap,
                t =>
            {
                Assert.Equal("abc", t.Name);
                Assert.Equal("def", t.Value);
            });
        }
Пример #10
0
        public void TryGetValue_ValueIsFound(int capacity)
        {
            // arrange
            var resultMap = new ResultMap();

            resultMap.EnsureCapacity(capacity);
            resultMap.SetValue(0, "abc", "def");
            resultMap.SetValue(capacity / 2, "def", "def");
            resultMap.SetValue(capacity - 1, "ghi", "def");

            IReadOnlyDictionary <string, object> dict = resultMap;

            // act
            var found = dict.TryGetValue("def", out var value);

            // assert
            Assert.True(found);
            Assert.Equal("def", value);
        }
Пример #11
0
        public void EnumerateValues()
        {
            // arrange
            var resultMap = new ResultMap();

            resultMap.EnsureCapacity(5);

            // act
            resultMap.SetValue(0, "abc1", "def");
            resultMap.SetValue(2, "abc2", "def");
            resultMap.SetValue(4, "abc3", "def");

            // assert
            Assert.Collection(
                ((IReadOnlyDictionary <string, object>)resultMap).Values,
                t => Assert.Equal("def", t),
                t => Assert.Equal("def", t),
                t => Assert.Equal("def", t));
        }
Пример #12
0
        /// <inheritdoc/>
        public async Task <IQueryResult> ExecuteAsync(IOperationContext operationContext)
        {
            ResultMap resultMap = Fragment.SelectionSet.EnqueueResolverTasks(
                operationContext,
                Path,
                ScopedContextData,
                Value);

            await ResolverExecutionHelper
            .ExecuteTasksAsync(operationContext)
            .ConfigureAwait(false);

            return(operationContext
                   .TrySetNext(true)
                   .SetLabel(Label)
                   .SetPath(Path)
                   .SetData(resultMap)
                   .BuildResult());
        }
 public void Initialize(
     IOperationContext operationContext,
     ISelection selection,
     ResultMap resultMap,
     int responseIndex,
     object?parent,
     Path path,
     IImmutableDictionary <string, object?> scopedContextData)
 {
     _operationContext = operationContext;
     _selection        = selection;
     ResultMap         = resultMap;
     ResponseIndex     = responseIndex;
     _parent           = parent;
     Path = path;
     ScopedContextData = scopedContextData;
     LocalContextData  = ImmutableDictionary <string, object?> .Empty;
     Arguments         = _selection.Arguments;
 }
Пример #14
0
        /// <inheritdoc/>
        public async Task <IQueryResult?> ExecuteAsync(IOperationContext operationContext)
        {
            operationContext.QueryPlan = operationContext.QueryPlan.GetDeferredPlan(Fragment.Id);

            ResultMap resultMap = EnqueueResolverTasks(
                operationContext,
                Fragment.SelectionSet,
                Parent,
                Path,
                ScopedContextData);

            await operationContext.Scheduler.ExecuteAsync().ConfigureAwait(false);

            return(operationContext
                   .TrySetNext(true)
                   .SetLabel(Label)
                   .SetPath(Path)
                   .SetData(resultMap)
                   .BuildResult());
        }
Пример #15
0
        private static async Task <IQueryResult> ExecuteInternalAsync(
            IOperationContext operationContext,
            IImmutableDictionary <string, object?> scopedContext)
        {
            ISelectionSet rootSelections =
                operationContext.Operation.GetRootSelectionSet();

            ResultMap resultMap = EnqueueResolverTasks(
                operationContext,
                rootSelections,
                operationContext.RootValue,
                Path.Root,
                scopedContext);

            await operationContext.Scheduler.ExecuteAsync().ConfigureAwait(false);

            return(operationContext
                   .TrySetNext()
                   .SetData(resultMap)
                   .BuildResult());
        }
Пример #16
0
 public void Initialize(
     IOperationContext operationContext,
     ISelection selection,
     ResultMap resultMap,
     int responseIndex,
     object?parent,
     Path path,
     IImmutableDictionary <string, object?> scopedContextData)
 {
     _task             = default;
     _operationContext = operationContext;
     _selection        = selection;
     _context.Initialize(
         operationContext,
         selection,
         resultMap,
         responseIndex,
         parent,
         path,
         scopedContextData);
 }
Пример #17
0
 public void Initialize(
     IOperationContext operationContext,
     ISelection selection,
     ResultMap resultMap,
     int responseIndex,
     object?parent,
     Path path,
     IImmutableDictionary <string, object?> scopedContextData)
 {
     _operationContext = operationContext;
     _services         = operationContext.Services;
     _selection        = selection;
     ResultMap         = resultMap;
     ResponseIndex     = responseIndex;
     _parent           = parent;
     _parser           = _operationContext.Services.GetRequiredService <InputParser>();
     Path = path;
     ScopedContextData = scopedContextData;
     LocalContextData  = ImmutableDictionary <string, object?> .Empty;
     Arguments         = _selection.Arguments;
     RequestAborted    = _operationContext.RequestAborted;
 }
Пример #18
0
        private static async Task <IQueryResult> ExecuteSelectionsAsync(
            IOperationContext operationContext,
            IReadOnlyList <ISelection> selections,
            ResultMap resultMap)
        {
            var responseIndex = 0;
            ImmutableDictionary <string, object?> scopedContext =
                ImmutableDictionary <string, object?> .Empty;

            for (var i = 0; i < selections.Count; i++)
            {
                ISelection selection = selections[i];
                if (selection.IsIncluded(operationContext.Variables))
                {
                    operationContext.Execution.TaskBacklog.Register(
                        new ResolverTaskDefinition(
                            operationContext,
                            selection,
                            responseIndex++,
                            resultMap,
                            operationContext.RootValue,
                            Path.New(selection.ResponseName),
                            scopedContext));

                    await ExecuteTasksAsync(operationContext).ConfigureAwait(false);

                    if (i + 1 < selections.Count)
                    {
                        operationContext.Execution.Reset();
                    }
                }
            }

            return(operationContext
                   .TrySetNext()
                   .SetData(resultMap)
                   .BuildResult());
        }
Пример #19
0
            // subscribe will use the subscribe resolver to create a source stream that yields
            // the event messages from the underlying pub/sub-system.
            private async ValueTask <ISourceStream> SubscribeAsync()
            {
                OperationContext operationContext = _operationContextPool.Get();

                try
                {
                    // first we will create the root value which essentially
                    // is the subscription object. In some cases this object is null.
                    object?rootValue = RootValueResolver.Resolve(
                        _requestContext,
                        _requestContext.Services,
                        _subscriptionType,
                        ref _cachedRootValue);

                    // next we need to initialize our operation context so that we have access to
                    // variables services and other things.
                    // The subscribe resolver will use a noop dispatcher and all DataLoader are
                    // dispatched immediately.
                    operationContext.Initialize(
                        _requestContext,
                        _requestContext.Services,
                        NoopBatchDispatcher.Default,
                        _requestContext.Operation !,
                        _requestContext.Variables !,
                        rootValue,
                        _resolveQueryRootValue);

                    // next we need a result map so that we can store the subscribe temporarily
                    // while executing the subscribe pipeline.
                    ResultMap  resultMap     = operationContext.Result.RentResultMap(1);
                    ISelection rootSelection = _rootSelections.Selections[0];

                    // we create a temporary middleware context so that we can use the standard
                    // resolver pipeline.
                    var middlewareContext = new MiddlewareContext();
                    middlewareContext.Initialize(
                        operationContext,
                        rootSelection,
                        resultMap,
                        1,
                        rootValue,
                        Path.New(rootSelection.ResponseName),
                        ImmutableDictionary <string, object?> .Empty);

                    // it is important that we correctly coerce the arguments before
                    // invoking subscribe.
                    if (!rootSelection.Arguments.TryCoerceArguments(
                            middlewareContext.Variables,
                            middlewareContext.ReportError,
                            out IReadOnlyDictionary <NameString, ArgumentValue>?coercedArgs))
                    {
                        // the middleware context reports errors to the operation context,
                        // this means if we failed, we need to grab the coercion errors from there
                        // and just throw a GraphQLException.
                        throw new GraphQLException(operationContext.Result.Errors);
                    }

                    // if everything is fine with the arguments we still need to assign them.
                    middlewareContext.Arguments = coercedArgs;

                    // last but not least we can invoke the subscribe resolver which will subscribe
                    // to the underlying pub/sub-system yielding the source stream.
                    ISourceStream sourceStream =
                        await rootSelection.Field.SubscribeResolver !.Invoke(middlewareContext)
                        .ConfigureAwait(false);

                    if (operationContext.Result.Errors.Count > 0)
                    {
                        // again if we have any errors we will just throw them and not opening
                        // any subscription context.
                        throw new GraphQLException(operationContext.Result.Errors);
                    }

                    return(sourceStream);
                }
                finally
                {
                    operationContext.Result.DropResult();
                    _operationContextPool.Return(operationContext);
                }
            }
Пример #20
0
 public void SetData(ResultMap data)
 {
     _data = data;
 }