public async Task RouteNotFound_ViaNoParameters_YieldsDefaultResult()
        {
            var actionResult = new RouteNotFoundGraphActionResult();

            var context = this.CreateResolutionContext();
            await actionResult.Complete(context);

            Assert.IsTrue(context.IsCancelled);
            Assert.AreEqual(1, context.Messages.Count);
            Assert.AreEqual(Constants.ErrorCodes.INVALID_ROUTE, context.Messages[0].Code);
        }
        public async Task RouteNotFound_ViaGraphAction_YieldsNegativeResult()
        {
            var action = TemplateHelper.CreateFieldTemplate <ActionableController>(nameof(ActionableController.DoStuff)) as IGraphMethod;

            var exception    = new Exception("fail");
            var actionResult = new RouteNotFoundGraphActionResult(action, exception);

            var context = this.CreateResolutionContext();
            await actionResult.Complete(context);

            Assert.IsTrue(context.IsCancelled);
            Assert.AreEqual(1, context.Messages.Count);
            Assert.AreEqual(Constants.ErrorCodes.INVALID_ROUTE, context.Messages[0].Code);
            Assert.IsTrue(context.Messages[0].Message.Contains(action.Name));
        }
        /// <summary>
        /// Processes the given <see cref="IGraphDirectiveRequest" /> against this instance
        /// performing the operation as defined by this entity and generating a response.
        /// </summary>
        /// <param name="context">The  context containing the necessary data to resolve
        /// the directive and produce a result.</param>
        /// <param name="cancelToken">The cancel token monitoring the execution of a graph request.</param>
        /// <returns>Task&lt;IGraphPipelineResponse&gt;.</returns>
        public async Task Resolve(DirectiveResolutionContext context, CancellationToken cancelToken = default)
        {
            var action = _directiveTemplate.FindMethod(context.Request.LifeCycle);

            // if no action is found skip processing of this directive
            if (action == null)
            {
                return;
            }

            IGraphActionResult result;

            try
            {
                // create a scoped controller instance for this invocation
                var directive = context
                                .ServiceProvider?
                                .GetService(_directiveTemplate.ObjectType) as GraphDirective;

                if (directive == null)
                {
                    result = new RouteNotFoundGraphActionResult(
                        $"The directive '{_directiveTemplate.InternalFullName}' " +
                        "was not found in the scoped service provider.");
                }
                else
                {
                    // invoke the right action method and set a result.
                    var task = directive.InvokeActionAsync(action, context);

                    var returnedItem = await task.ConfigureAwait(false);

                    result = this.EnsureGraphActionResult(returnedItem);
                }
            }
            catch (Exception ex)
            {
                // :(
                result = new InternalServerErrorGraphActionResult("Operation failed.", ex);
            }

            // resolve the final graph action output using the provided field context
            // in what ever manner is appropriate for the result itself
            await result.Complete(context);
        }
Beispiel #4
0
        public async Task Resolve(FieldResolutionContext context, CancellationToken cancelToken = default)
        {
            IGraphActionResult result;

            try
            {
                // create a scoped controller instance for this invocation
                var controller = context
                                 .ServiceProvider?
                                 .GetService(_actionMethod.Parent.ObjectType) as GraphController;

                if (controller == null)
                {
                    result = new RouteNotFoundGraphActionResult(
                        $"The controller assigned to process the field '{context.Request.InvocationContext.Field.Route.Path}' " +
                        "was not found.");
                }
                else
                {
                    // invoke the right action method and set a result.
                    var task         = controller.InvokeActionAsync(_actionMethod, context);
                    var returnedItem = await task.ConfigureAwait(false);

                    result = this.EnsureGraphActionResult(returnedItem);
                }
            }
            catch (Exception ex)
            {
                // :(
                result = new InternalServerErrorGraphActionResult("Operation failed.", ex);
            }

            // resolve the final graph action output using the provided field context
            // in what ever manner is appropriate for the result itself
            await result.Complete(context).ConfigureAwait(false);
        }