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());
        }
示例#2
0
        public async Task <IReadOnlyQueryResult> ExecuteAsync(
            IOperationContext operationContext,
            IImmutableDictionary <string, object?> scopedContext)
        {
            if (operationContext is null)
            {
                throw new ArgumentNullException(nameof(operationContext));
            }

            if (scopedContext is null)
            {
                throw new ArgumentNullException(nameof(scopedContext));
            }

            IPreparedSelectionList rootSelections =
                operationContext.Operation.GetRootSelections();

            ResultMap resultMap = rootSelections.EnqueueResolverTasks(
                operationContext, Path.New, scopedContext,
                operationContext.RootValue);

            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);
        }
示例#4
0
        public void Object_Field_Visibility_Is_Correctly_Inherited_2()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) => name.Equals("v"));

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean, $q: Boolean) {
                    hero(episode: EMPIRE) @include(if: $v) {
                        name @include(if: $q)
                    }
                    ... on Query {
                        hero(episode: EMPIRE) {
                            id
                        }
                    }
                    ... @include(if: $v) {
                        hero(episode: EMPIRE) {
                            height
                        }
                    }
                }");

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

            var fragments = new FragmentCollection(schema, document);

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

            // assert
            var op = new PreparedOperation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);
            IPreparedSelectionList rootSelections =
                op.RootSelectionSet.GetSelections(op.RootSelectionSet.GetPossibleTypes().First());
            IPreparedSelectionList droidSelections =
                op.GetSelections(rootSelections[0].SelectionSet !, droid);

            Assert.Collection(
                droidSelections.Where(t => t.IsIncluded(variables.Object)),
                t => Assert.Equal("id", t.ResponseName),
                t => Assert.Equal("height", t.ResponseName));

            op.Print().MatchSnapshot();
        }
示例#5
0
        public void Field_Is_Visible_When_One_Selection_Is_Visible_3()
        {
            // arrange
            var variables = new Mock <IVariableValueCollection>();

            variables.Setup(t => t.GetVariable <bool>(It.IsAny <NameString>()))
            .Returns((NameString name) =>
            {
                return(name.Equals("q"));
            });

            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .Create();

            ObjectType droid = schema.GetType <ObjectType>("Droid");

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query foo($v: Boolean, $q: Boolean){
                    hero(episode: EMPIRE) {
                        name @include(if: $v)
                        ... abc @include(if: $q)
                    }
                }

                fragment abc on Droid {
                    name
                }");

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

            var fragments = new FragmentCollection(schema, document);

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

            // assert
            var op = new PreparedOperation(
                "abc",
                document,
                operation,
                schema.QueryType,
                selectionSets);
            IPreparedSelectionList rootSelections =
                op.RootSelectionSet.GetSelections(op.RootSelectionSet.GetPossibleTypes().First());
            IPreparedSelectionList droidSelections =
                op.GetSelections(rootSelections[0].SelectionSet !, droid);

            Assert.Equal("name", droidSelections[0].ResponseName);
            Assert.True(droidSelections[0].IsConditional);
            Assert.True(droidSelections[0].IsIncluded(variables.Object));
            Assert.True(droidSelections.IsConditional);
        }
示例#6
0
 private Subscription(
     ObjectPool <OperationContext> operationContextPool,
     QueryExecutor queryExecutor,
     IRequestContext requestContext,
     ObjectType subscriptionType,
     IPreparedSelectionList rootSelections,
     IDiagnosticEvents diagnosicEvents)
 {
     _operationContextPool = operationContextPool;
     _queryExecutor        = queryExecutor;
     _requestContext       = requestContext;
     _subscriptionType     = subscriptionType;
     _rootSelections       = rootSelections;
     _diagnosicEvents      = diagnosicEvents;
 }
        public async Task <IExecutionResult> ExecuteAsync(
            IRequestContext requestContext)
        {
            if (requestContext is null)
            {
                throw new ArgumentNullException(nameof(requestContext));
            }

            if (requestContext.Operation is null || requestContext.Variables is null)
            {
                throw SubscriptionExecutor_ContextInvalidState();
            }

            IPreparedSelectionList rootSelections = requestContext.Operation.GetRootSelections();

            if (rootSelections.Count != 1)
            {
                throw SubscriptionExecutor_SubscriptionsMustHaveOneField();
            }

            if (rootSelections[0].Field.SubscribeResolver is null)
            {
                throw SubscriptionExecutor_NoSubscribeResolver();
            }

            Subscription?subscription = null;

            try
            {
                subscription = await Subscription.SubscribeAsync(
                    _operationContextPool,
                    _queryExecutor,
                    requestContext,
                    requestContext.Operation.RootType,
                    rootSelections,
                    _diagnosticEvents)
                               .ConfigureAwait(false);

                return(new SubscriptionResult(
                           subscription.ExecuteAsync,
                           null,
                           session: subscription));
            }
            catch (GraphQLException ex)
            {
                if (subscription is { })
示例#8
0
            public static async ValueTask <Subscription> SubscribeAsync(
                ObjectPool <OperationContext> operationContextPool,
                QueryExecutor queryExecutor,
                IRequestContext requestContext,
                ObjectType subscriptionType,
                IPreparedSelectionList rootSelections,
                IDiagnosticEvents diagnosicEvents)
            {
                var subscription = new Subscription(
                    operationContextPool,
                    queryExecutor,
                    requestContext,
                    subscriptionType,
                    rootSelections,
                    diagnosicEvents);

                subscription._sourceStream = await subscription.SubscribeAsync();

                return(subscription);
            }
示例#9
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);
        }