Example #1
0
        /// <summary>
        /// Updates this instance with a piece of data recieved from the completion of a user resolver function.
        /// This method performs no validation or resolution of the data, it simply assigns it as a value that was recieved
        /// from a resolver function and attaches it to this instance.
        /// </summary>
        /// <param name="data">The data.</param>
        public virtual void AssignResult(object data)
        {
            _childListItems = null;
            _childFields    = null;

            this.ResultData = data;
            this.SetStatus(FieldItemResolutionStatus.ResultAssigned);

            // initialize the children of this instance from the assigned result
            if (this.IsListField)
            {
                if (this.ResultData == null)
                {
                    return;
                }

                if (!GraphValidation.IsValidListType(this.ResultData.GetType()))
                {
                    return;
                }

                if (!(this.ResultData is IEnumerable arraySource))
                {
                    return;
                }

                _childListItems = new List <GraphDataItem>();

                // this instances's type expression is a list (or else we wouldnt be rendering out children)
                // strip out that outer most list component
                // to represent what each element of said list should be so that the rule
                // processor can validate the item as its own entity
                var childTypeExpression = this.TypeExpression.UnWrapExpression(MetaGraphTypes.IsList);

                var path  = this.Path.Clone();
                int index = 0;

                foreach (var item in arraySource)
                {
                    var indexedPath = path.Clone();
                    indexedPath.AddArrayIndex(index++);
                    var childItem = new GraphDataItem(this.FieldContext, item, indexedPath)
                    {
                        TypeExpression = childTypeExpression.Clone(),
                    };

                    this.AddListItem(childItem);

                    // child items are immediately resolved
                    // using the enumerated source data from the resolver that supplied data
                    // to this parent item.
                    // that is to say that no child resolver is needed to be processed to
                    // retrieve data for each child individually.
                    childItem.AssignResult(item);
                    childItem.SetStatus(this.Status);
                }
            }
        }