示例#1
0
        private void CollectFields(
            CompilerContext context,
            SelectionSetNode selectionSet,
            SelectionIncludeCondition?includeCondition)
        {
            for (var i = 0; i < selectionSet.Selections.Count; i++)
            {
                ISelectionNode            selection          = selectionSet.Selections[i];
                SelectionIncludeCondition?selectionCondition = includeCondition;

                if (selectionCondition is null)
                {
                    var reference = new SelectionReference(context.SelectionPath, selection);
                    context.IncludeConditionLookup.TryGetValue(reference, out selectionCondition);
                }

                ResolveFields(
                    context,
                    selection,
                    ExtractVisibility(selection, selectionCondition));
            }
        }
示例#2
0
        private void ResolveFieldSelection(
            CompilerContext context,
            FieldNode selection,
            SelectionIncludeCondition?includeCondition)
        {
            NameString fieldName    = selection.Name.Value;
            NameString responseName = selection.Alias is null
                ? selection.Name.Value
                : selection.Alias.Value;

            if (context.Type.Fields.TryGetField(fieldName, out IObjectField? field))
            {
                if ((selection.SelectionSet is null ||
                     selection.SelectionSet.Selections.Count == 0) &&
                    field.Type.NamedType().IsCompositeType())
                {
                    throw OperationCompiler_NoCompositeSelections(selection);
                }

                if (context.Fields.TryGetValue(responseName, out Selection? preparedSelection))
                {
                    preparedSelection.AddSelection(selection, includeCondition);
                }
                else
                {
                    // if this is the first time we find a selection to this field we have to
                    // create a new prepared selection.
                    preparedSelection = new Selection(
                        context.Type,
                        field,
                        selection.SelectionSet is not null
                            ? selection.WithSelectionSet(
                            selection.SelectionSet.WithSelections(
                                selection.SelectionSet.Selections))
                            : selection,
                        responseName: responseName,
                        resolverPipeline: CreateFieldMiddleware(field, selection),
                        arguments: CoerceArgumentValues(field, selection, responseName),
                        includeCondition: includeCondition,
                        internalSelection: context.IsInternalSelection);

                    context.Fields.Add(responseName, preparedSelection);
                }

                if (includeCondition is not null && selection.SelectionSet is not null)
                {
                    var selectionPath = context.SelectionPath.Append(responseName);

                    for (var i = 0; i < selection.SelectionSet.Selections.Count; i++)
                    {
                        ISelectionNode child     = selection.SelectionSet.Selections[i];
                        var            reference = new SelectionReference(selectionPath, child);

                        if (!context.IncludeConditionLookup.ContainsKey(reference))
                        {
                            context.IncludeConditionLookup.Add(reference, includeCondition);
                        }
                    }
                }
            }
            else
            {
                throw FieldDoesNotExistOnType(selection, context.Type.Name);
            }
        }
示例#3
0
        private void ResolveFieldSelection(
            CompilerContext context,
            FieldNode selection,
            SelectionIncludeCondition?includeCondition)
        {
            NameString fieldName    = selection.Name.Value;
            NameString responseName = selection.Alias is null
                ? selection.Name.Value
                : selection.Alias.Value;

            if (context.Type.Fields.TryGetField(fieldName, out IObjectField? field))
            {
                if ((selection.SelectionSet is null ||
                     selection.SelectionSet.Selections.Count == 0) &&
                    field.Type.NamedType().IsCompositeType())
                {
                    throw OperationCompiler_NoCompositeSelections(selection);
                }

                if (context.Fields.TryGetValue(responseName, out Selection? preparedSelection))
                {
                    preparedSelection.AddSelection(selection, includeCondition);
                }
                else
                {
                    Func <object, IAsyncEnumerable <object?> >?createStream = null;
                    bool isStreamable = selection.IsStreamable();

                    if (field.MaybeStream || field.Type.IsListType() && isStreamable)
                    {
                        IType elementType = field.Type.ElementType();

                        if (elementType.IsCompositeType())
                        {
                            Type runtimeType = elementType.ToRuntimeType();
                            CreateStreamDelegate streamDelegate = CreateStream(runtimeType);
                            createStream = o => streamDelegate(o);
                        }
                    }

                    // if this is the first time we find a selection to this field we have to
                    // create a new prepared selection.
                    preparedSelection = new Selection(
                        GetNextId(),
                        context.Type,
                        field,
                        selection.SelectionSet is not null
                            ? selection.WithSelectionSet(
                            selection.SelectionSet.WithSelections(
                                selection.SelectionSet.Selections))
                            : selection,
                        responseName: responseName,
                        resolverPipeline: CreateFieldMiddleware(field, selection),
                        pureResolver: TryCreatePureField(field, selection),
                        strategy: field.IsParallelExecutable
                            ? null // use default strategy
                            : SelectionExecutionStrategy.Serial,
                        arguments: CoerceArgumentValues(field, selection, responseName),
                        includeCondition: includeCondition,
                        internalSelection: context.IsInternalSelection,
                        createStream: createStream,
                        isStreamable: isStreamable);

                    context.Fields.Add(responseName, preparedSelection);
                }

                if (includeCondition is not null && selection.SelectionSet is not null)
                {
                    SelectionPath selectionPath = context.SelectionPath.Append(responseName);

                    for (var i = 0; i < selection.SelectionSet.Selections.Count; i++)
                    {
                        ISelectionNode child     = selection.SelectionSet.Selections[i];
                        var            reference = new SelectionReference(selectionPath, child);

                        if (!context.IncludeConditionLookup.ContainsKey(reference))
                        {
                            context.IncludeConditionLookup.Add(reference, includeCondition);
                        }
                    }
                }
            }
            else
            {
                throw FieldDoesNotExistOnType(selection, context.Type.Name);
            }
        }