Exemplo n.º 1
0
        public static void SetArrayItemNodes(ExecutionContext context, ArrayExecutionNode parent)
        {
            var listType = (ListGraphType)parent.GraphType;
            var itemType = listType.ResolvedType;

            if (itemType is NonNullGraphType nonNullGraphType)
            {
                itemType = nonNullGraphType.ResolvedType;
            }

            if (!(parent.Result is IEnumerable data))
            {
                var error = new ExecutionError("User error: expected an IEnumerable list though did not find one.");
                throw error;
            }

            var index      = 0;
            var arrayItems = (data is ICollection collection)
                ? new List <ExecutionNode>(collection.Count)
                : new List <ExecutionNode>();

            foreach (var d in data)
            {
                var path = AppendPath(parent.Path, (index++).ToString());

                if (d != null)
                {
                    var node = BuildExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, path);
                    node.Result = d;

                    if (node is ObjectExecutionNode objectNode)
                    {
                        SetSubFieldNodes(context, objectNode);
                    }

                    arrayItems.Add(node);
                }
                else
                {
                    var valueExecutionNode = new ValueExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, path)
                    {
                        Result = null
                    };
                    arrayItems.Add(valueExecutionNode);
                }
            }

            parent.Items = arrayItems;
        }
Exemplo n.º 2
0
        public static void SetArrayItemNodes(ExecutionContext context, ArrayExecutionNode parent)
        {
            var listType = (ListGraphType)parent.GraphType;
            var itemType = listType.ResolvedType;

            if (itemType is NonNullGraphType nonNullGraphType)
            {
                itemType = nonNullGraphType.ResolvedType;
            }

            if (!(parent.Result is IEnumerable data))
            {
                var error = new ExecutionError("User error: expected an IEnumerable list though did not find one.");
                throw error;
            }

            var index      = 0;
            var arrayItems = (data is ICollection collection)
                ? new List <ExecutionNode>(collection.Count)
                : new List <ExecutionNode>();

            foreach (var d in data)
            {
                if (d != null)
                {
                    var node = BuildExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, index++);
                    node.Result = d;

                    if (node is ObjectExecutionNode objectNode)
                    {
                        SetSubFieldNodes(context, objectNode);
                    }
                    else if (node is ArrayExecutionNode arrayNode)
                    {
                        SetArrayItemNodes(context, arrayNode);
                    }

                    arrayItems.Add(node);
                }
                else
                {
                    if (listType.ResolvedType is NonNullGraphType)
                    {
                        var error = new ExecutionError(
                            "Cannot return null for non-null type."
                            + $" Field: {parent.Name}, Type: {parent.FieldDefinition.ResolvedType}.");

                        error.AddLocation(parent.Field, context.Document);
                        error.Path = parent.Path.Append(index);
                        context.Errors.Add(error);
                        return;
                    }

                    var valueExecutionNode = new ValueExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, index++)
                    {
                        Result = null
                    };
                    arrayItems.Add(valueExecutionNode);
                }
            }

            parent.Items = arrayItems;
        }