Example #1
0
        private void CompleteSelectionSet(CompilerContext context)
        {
            foreach (Selection selection in context.Fields.Values)
            {
                // we now mark the selection read-only and add it to the final selection-set.
                selection.MakeReadOnly();
                context.Selections.Add(selection);

                // if one selection of a selection-set is conditional,
                // then the whole set is conditional and has to be post processed during execution.
                if (!context.IsConditional && (selection.IsConditional || selection.IsInternal))
                {
                    context.IsConditional = true;
                }

                // if the field of the selection returns a composite type we will traverse
                // the child selection-sets as well.
                INamedType fieldType = selection.Field.Type.NamedType();
                if (fieldType.IsCompositeType())
                {
                    if (selection.SelectionSet is null)
                    {
                        // composite fields always have to have a selection-set
                        // otherwise we need to throw.
                        throw QueryCompiler_CompositeTypeSelectionSet(selection.SyntaxNode);
                    }

                    IReadOnlyList <ObjectType> possibleTypes = _schema.GetPossibleTypes(fieldType);

                    for (var i = possibleTypes.Count - 1; i >= 0; i--)
                    {
                        // we branch the context which will enqueue the new context
                        // to the work backlog.
                        context.TryBranch(possibleTypes[i], selection);
                    }
                }
            }

            context.Complete();
        }