public override object VisitSelectionSet(GraphQLParser.SelectionSetContext context)
        {
            var selections = new Selections();

            foreach (var selectionContext in context.selection())
            {
                var selection = Visit(selectionContext) as Selection;
                selections.Add(selection);
            }

            return(selections);
        }
Exemplo n.º 2
0
        public Dictionary <string, Fields> CollectFields(ExecutionContext context, GraphType type, Selections selections, Dictionary <string, Fields> fields)
        {
            if (fields == null)
            {
                fields = new Dictionary <string, Fields>();
            }

            selections.Apply(selection =>
            {
                if (selection.Field != null)
                {
                    if (!ShouldIncludeNode(context, selection.Field.Directives))
                    {
                        return;
                    }

                    var name = selection.Field.Alias ?? selection.Field.Name;
                    if (!fields.ContainsKey(name))
                    {
                        fields[name] = new Fields();
                    }
                    fields[name].Add(selection.Field);
                }
                else if (selection.Fragment != null)
                {
                    if (selection.Fragment is FragmentSpread)
                    {
                        var spread = selection.Fragment as FragmentSpread;

                        if (!ShouldIncludeNode(context, spread.Directives))
                        {
                            return;
                        }

                        var fragment = context.Fragments.FindDefinition(spread.Name);
                        if (!ShouldIncludeNode(context, fragment.Directives) ||
                            !DoesFragmentConditionMatch(context, fragment, type))
                        {
                            return;
                        }

                        CollectFields(context, type, fragment.Selections, fields);
                    }
                    else if (selection.Fragment is InlineFragment)
                    {
                        var inline = selection.Fragment as InlineFragment;

                        if (!ShouldIncludeNode(context, inline.Directives) ||
                            !DoesFragmentConditionMatch(context, inline, type))
                        {
                            return;
                        }

                        CollectFields(context, type, inline.Selections, fields);
                    }
                }
            });

            return(fields);
        }
        public Dictionary <string, Fields> CollectFields(ExecutionContext context, GraphType type, Selections selections, Dictionary <string, Fields> fields)
        {
            if (fields == null)
            {
                fields = new Dictionary <string, Fields>();
            }

            selections.Apply(selection =>
            {
                if (selection.Field != null)
                {
                    var name = selection.Field.Alias ?? selection.Field.Name;
                    if (!fields.ContainsKey(name))
                    {
                        fields[name] = new Fields();
                    }
                    fields[name].Add(selection.Field);
                }
            });

            return(fields);
        }