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

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

            var node = BuildExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, parent.Path);

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

            parent.Items = new List <ExecutionNode> {
                node
            };
        }
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))
            {
                throw new InvalidOperationException($"Expected an IEnumerable list though did not find one. Found: {parent.Result?.GetType().Name}");
            }

            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);
                    }
                    else if (node is ValueExecutionNode valueNode)
                    {
                        node.Result = valueNode.GraphType.Serialize(d)
                                      ?? throw new InvalidOperationException($"Unable to serialize '{d}' to '{valueNode.GraphType.Name}' for list index {index}.");
                    }

                    arrayItems.Add(node);
                }
                else
                {
                    if (listType.ResolvedType is NonNullGraphType)
                    {
                        throw new InvalidOperationException($"Cannot return a null member within a non-null list for list index {index}.");
                    }

                    var nullExecutionNode = new NullExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, index);
                    arrayItems.Add(nullExecutionNode);
                }

                index++;
            }

            parent.Items = arrayItems;
        }
Exemplo n.º 3
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;
        }
        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;
            }

            var data = parent.Result as IEnumerable;

            if (data == null)
            {
                var error = new ExecutionError("User error: expected an IEnumerable list though did not find one.");
                error.AddLocation(parent.Field, context.Document);

                throw error;
            }

            var index      = 0;
            var arrayItems = new List <ExecutionNode>();

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

                ExecutionNode node = BuildExecutionNode(parent, itemType, parent.Field, parent.FieldDefinition, path);
                node.Result = d;

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

                arrayItems.Add(node);
            }

            parent.Items = arrayItems;
        }
Exemplo n.º 5
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;
        }