public async Task <IReadOnlyQueryResult> ExecuteAsync(
            IOperationContext operationContext)
        {
            if (operationContext is null)
            {
                throw new ArgumentNullException(nameof(operationContext));
            }

            var responseIndex = 0;
            var scopedContext = ImmutableDictionary <string, object?> .Empty;
            IPreparedSelectionList rootSelections = operationContext.Operation.GetRootSelections();
            ResultMap resultMap = operationContext.Result.RentResultMap(rootSelections.Count);

            for (var i = 0; i < rootSelections.Count; i++)
            {
                IPreparedSelection selection = rootSelections[i];
                if (selection.IsIncluded(operationContext.Variables))
                {
                    operationContext.Execution.TaskBacklog.Register(
                        new ResolverTaskDefinition(
                            operationContext,
                            selection,
                            responseIndex++,
                            resultMap,
                            operationContext.RootValue,
                            Path.New(selection.ResponseName),
                            scopedContext));

                    await ExecuteTasksAsync(operationContext);
                }
            }

            operationContext.Result.SetData(resultMap);
            return(operationContext.Result.BuildResult());
        }
        public static ResultMap EnqueueResolverTasks(
            this IPreparedSelectionList selections,
            IOperationContext operationContext,
            Func <NameString, Path> createPath,
            IImmutableDictionary <string, object?> scopedContext,
            object?parent)
        {
            var       responseIndex = 0;
            ResultMap resultMap     = operationContext.Result.RentResultMap(selections.Count);

            for (var i = 0; i < selections.Count; i++)
            {
                IPreparedSelection selection = selections[i];
                if (selection.IsIncluded(operationContext.Variables))
                {
                    operationContext.Execution.TaskBacklog.Register(
                        new ResolverTaskDefinition(
                            operationContext,
                            selection,
                            responseIndex++,
                            resultMap,
                            parent,
                            createPath(selection.ResponseName),
                            scopedContext));
                }
            }

            return(resultMap);
        }
Example #3
0
        public void Prepare_Inline_Fragment()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                hero(episode: EMPIRE) {
                    name
                    ... on Droid {
                        primaryFunction
                    }
                    ... on Human {
                        homePlanet
                    }
                }
             }");

            OperationDefinitionNode operation =
                document.Definitions.OfType <OperationDefinitionNode>().Single();

            var fragments = new FragmentCollection(schema, document);

            // act
            IReadOnlyDictionary <SelectionSetNode, PreparedSelectionSet> selectionSets =
                OperationCompiler.Compile(schema, fragments, operation);

            // assert
            IPreparedSelection hero = selectionSets[operation.SelectionSet].GetSelections(schema.QueryType).Single();

            Assert.Equal("hero", hero.ResponseName);

            Assert.Collection(
                selectionSets[hero.SelectionSet].GetSelections(schema.GetType <ObjectType>("Droid")),
                selection => Assert.Equal("name", selection.ResponseName),
                selection => Assert.Equal("primaryFunction", selection.ResponseName));

            Assert.Collection(
                selectionSets[hero.SelectionSet].GetSelections(schema.GetType <ObjectType>("Human")),
                selection => Assert.Equal("name", selection.ResponseName),
                selection => Assert.Equal("homePlanet", selection.ResponseName));

            var op = new PreparedOperation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);

            op.Print().MatchSnapshot();
        }
 public SelectionVisitorContext(
     IResolverContext context,
     ITypeConversion conversion,
     IPreparedSelection fieldSelection)
 {
     Conversion     = conversion;
     FieldSelection = fieldSelection;
     _context       = context;
     _arguments     = CoerceArguments(
         fieldSelection.Arguments,
         context.Variables,
         context.Path);
 }
 public SelectionVisitorContext(
     IResolverContext context,
     ITypeConverter conversion,
     IPreparedSelection fieldSelection,
     SelectionMiddlewareContext selectionMiddlewareContext)
 {
     Conversion       = conversion;
     FieldSelection   = fieldSelection;
     SelectionContext = selectionMiddlewareContext;
     _arguments       = CoerceArguments(
         fieldSelection.Arguments,
         context.Variables);
 }
Example #6
0
            private async ValueTask <ISourceStream> SubscribeAsync()
            {
                OperationContext operationContext = _operationContextPool.Get();

                try
                {
                    object?rootValue = RootValueResolver.TryResolve(
                        _requestContext,
                        _requestContext.Services,
                        _subscriptionType,
                        ref _cachedRootValue);

                    operationContext.Initialize(
                        _requestContext,
                        _requestContext.Services,
                        NoopBatchDispatcher.Default,
                        _requestContext.Operation !,
                        rootValue,
                        _requestContext.Variables !);

                    ResultMap          resultMap     = operationContext.Result.RentResultMap(1);
                    IPreparedSelection rootSelection = _rootSelections[0];

                    var middlewareContext = new MiddlewareContext();
                    middlewareContext.Initialize(
                        operationContext,
                        _rootSelections[0],
                        resultMap,
                        1,
                        rootValue,
                        Path.New(_rootSelections[0].ResponseName),
                        ImmutableDictionary <string, object?> .Empty);

                    ISourceStream sourceStream =
                        await rootSelection.Field.SubscribeResolver !.Invoke(middlewareContext)
                        .ConfigureAwait(false);

                    if (operationContext.Result.Errors.Count > 0)
                    {
                        throw new GraphQLException(operationContext.Result.Errors);
                    }

                    return(sourceStream);
                }
                finally
                {
                    operationContext.Result.DropResult();
                    _operationContextPool.Return(operationContext);
                }
            }
 public ResolverTaskDefinition(
     IOperationContext operationContext,
     IPreparedSelection selection,
     int responseIndex,
     ResultMap resultMap,
     object?parent,
     Path path,
     IImmutableDictionary <string, object?> scopedContextData)
 {
     OperationContext  = operationContext;
     Selection         = selection;
     ResponseIndex     = responseIndex;
     ResultMap         = resultMap;
     Parent            = parent;
     Path              = path;
     ScopedContextData = scopedContextData;
 }
 public void Initialize(
     IOperationContext operationContext,
     IPreparedSelection selection,
     ResultMap resultMap,
     int responseIndex,
     object?parent,
     Path path,
     IImmutableDictionary <string, object?> scopedContextData)
 {
     _operationContext = operationContext;
     _selection        = selection;
     ResultMap         = resultMap;
     ResponseIndex     = responseIndex;
     _parent           = parent;
     Path = path;
     ScopedContextData = scopedContextData;
     LocalContextData  = ImmutableDictionary <string, object?> .Empty;
     Arguments         = _selection.Arguments;
 }
Example #9
0
 public void Initialize(
     IOperationContext operationContext,
     IPreparedSelection selection,
     ResultMap resultMap,
     int responseIndex,
     object?parent,
     Path path,
     IImmutableDictionary <string, object?> scopedContextData)
 {
     _task             = default;
     _operationContext = operationContext;
     _selection        = selection;
     _context.Initialize(
         operationContext,
         selection,
         resultMap,
         responseIndex,
         parent,
         path,
         scopedContextData);
 }
Example #10
0
        public IReadOnlyList <IFieldSelection> GetSelections(
            ObjectType typeContext,
            SelectionSetNode?selectionSet = null,
            bool allowInternals           = false)
        {
            if (typeContext is null)
            {
                throw new ArgumentNullException(nameof(typeContext));
            }

            selectionSet ??= _selection.SelectionSet;

            if (selectionSet is null)
            {
                return(Array.Empty <IFieldSelection>());
            }

            IPreparedSelectionList fields =
                _operationContext.CollectFields(selectionSet, typeContext);

            if (fields.IsConditional)
            {
                var finalFields = new List <IFieldSelection>();

                for (var i = 0; i < fields.Count; i++)
                {
                    IPreparedSelection selection = fields[i];
                    if (selection.IsIncluded(_operationContext.Variables, allowInternals))
                    {
                        finalFields.Add(selection);
                    }
                }

                return(finalFields);
            }

            return(fields);
        }