Example #1
0
        public void ParallelAttributeWillBeHonored(ExecutionStrategy defaultStrategy)
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType <ParallelQuery>()
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                    foo
                }");

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

            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // act
            QueryPlanNode root = QueryPlanBuilder.Prepare(operation);

            // assert
            Snapshot(root, defaultStrategy);
        }
Example #2
0
        public void ExtendedRootTypesWillHonorGlobalSerialSetting(ExecutionStrategy defaultStrategy)
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddQueryType(c => c.Name(OperationTypeNames.Query))
                             .AddType <BarQueries>()
                             .ModifyOptions(o => o.DefaultResolverStrategy = defaultStrategy)
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                    bars
                }");

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

            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // act
            QueryPlanNode root = QueryPlanBuilder.Prepare(operation);

            // assert
            Snapshot(root, defaultStrategy);
        }
Example #3
0
        public void CreateReviewForEpisode_Plan(ExecutionStrategy defaultStrategy)
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .ModifyOptions(o => o.DefaultResolverStrategy = defaultStrategy)
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"mutation CreateReviewForEpisode(
                    $ep: Episode!, $review: ReviewInput!) {
                    createReview(episode: $ep, review: $review) {
                        stars
                        commentary
                    }
                }");

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

            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.MutationType !,
                    new(new DefaultTypeConverter()));

            // act
            QueryPlanNode root = QueryPlanBuilder.Prepare(operation);

            // assert
            Snapshot(root, defaultStrategy);
        }
            private static void Visit(ISelection selection, QueryPlanContext context)
            {
                if (selection.IsStreamable && selection.SelectionSet is not null)
                {
                    QueryPlanContext streamContext = context.Branch();

                    VisitChildren(selection, streamContext);

                    if (streamContext.Root is not null &&
                        !context.Streams.ContainsKey(selection.Id))
                    {
                        context.Streams.Add(selection.Id, new(selection.Id, streamContext.Root));
                    }

                    if (streamContext.Streams.Count > 0)
                    {
                        foreach (StreamPlanNode streamPlan in streamContext.Streams.Values)
                        {
                            if (!context.Streams.ContainsKey(selection.Id))
                            {
                                context.Streams.Add(selection.Id, streamPlan);
                            }
                        }
                    }
                }

                if (context.NodePath.Count == 0)
                {
                    context.Root = new ResolverNode(selection);
                    context.NodePath.Push(context.Root);
                }
                else
                {
                    QueryPlanNode parent = context.NodePath.Peek();

                    if (selection.Strategy == SelectionExecutionStrategy.Serial)
                    {
                        if (parent is ResolverNode {
                            Strategy : ExecutionStrategy.Serial
                        } p)
                        {
                            p.Selections.Add(selection);
                        }
                        else if (context.SelectionPath.Count > 0 &&
                                 context.NodePath.TryPeek(2, out QueryPlanNode? grandParent) &&
                                 grandParent is ResolverNode {
                            Strategy: ExecutionStrategy.Serial
                        } gp&&
                                 gp.Selections.Contains(context.SelectionPath.PeekOrDefault() !))
                        {
                            gp.Selections.Add(selection);
                        }
Example #5
0
        public void GetHero_Root_Deferred_Plan(ExecutionStrategy defaultStrategy)
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .ModifyOptions(o => o.DefaultResolverStrategy = defaultStrategy)
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"query GetHero($episode: Episode, $withFriends: Boolean!) {
                    ... @defer {
                        hero(episode: $episode) {
                            name
                            friends @include(if: $withFriends) {
                                nodes {
                                    id
                                }
                            }
                        }
                    }
                    a: hero(episode: $episode) {
                        name
                        friends @include(if: $withFriends) {
                            nodes {
                                id
                            }
                        }
                    }
                }");

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

            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // act
            QueryPlanNode root = QueryPlanBuilder.Prepare(operation);

            // assert
            Snapshot(root, defaultStrategy);
        }
Example #6
0
        public void GetHero_Stream_Plan_Nested_Streams(ExecutionStrategy defaultStrategy)
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddStarWarsTypes()
                             .ModifyOptions(o => o.DefaultResolverStrategy = defaultStrategy)
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"{
                    hero(episode: NEW_HOPE) {
                        id
                        ... @defer(label: ""friends"") {
                            friends {
                                nodes @stream(initialCount: 1) {
                                    id
                                    name
                                    friends {
                                        nodes @stream(initialCount: 1) {
                                            id
                                            name
                                        }
                                    }
                                }
                            }
                        }
                    }
                }");

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

            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.QueryType,
                    new(new DefaultTypeConverter()));

            // act
            QueryPlanNode root = QueryPlanBuilder.Prepare(operation);

            // assert
            Snapshot(root, defaultStrategy);
        }
Example #7
0
        public void TypeNameFieldsInMutations()
        {
            // arrange
            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString(@"
                    type Query {
                        foo: String
                    }

                    type Mutation {
                        bar: Bar
                    }

                    type Bar {
                        test: String
                    }
                ")
                             .Use(next => next)
                             .Create();

            DocumentNode document = Utf8GraphQLParser.Parse(
                @"mutation {
                    bar {
                        test
                        __typename
                    }
                }");

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

            IPreparedOperation operation =
                OperationCompiler.Compile(
                    "a",
                    document,
                    operationDefinition,
                    schema,
                    schema.MutationType !,
                    new(new DefaultTypeConverter()));

            // act
            QueryPlanNode root = QueryPlanBuilder.Prepare(operation);

            // assert
            Snapshot(root);
        }
Example #8
0
        private static void Snapshot(
            QueryPlanNode node,
            ExecutionStrategy strategy = ExecutionStrategy.Parallel)
        {
            var options = new JsonWriterOptions {
                Indented = true
            };

            using var stream = new MemoryStream();
            using var writer = new Utf8JsonWriter(stream, options);

            node.Serialize(writer);
            writer.Flush();

            Encoding.UTF8
            .GetString(stream.ToArray())
            .MatchSnapshot(new SnapshotNameExtension(strategy));
        }
Example #9
0
            public static OperationNode Build(QueryPlanContext context)
            {
                var root = new SequenceNode {
                    CancelOnError = true
                };

                context.Root = root;
                context.NodePath.Push(root);

                foreach (ISelection mutation in context.Operation.GetRootSelectionSet().Selections)
                {
                    context.SelectionPath.Push(mutation);

                    var mutationStep = new ResolverNode(
                        mutation,
                        context.SelectionPath.PeekOrDefault(),
                        GetStrategyFromSelection(mutation));

                    root.AddNode(mutationStep);

                    QueryStrategy.VisitChildren(mutation, context);
                }

                context.NodePath.Pop();

                QueryPlanNode optimized     = QueryStrategy.Optimize(context.Root);
                var           operationNode = new OperationNode(optimized);

                if (context.Deferred.Count > 0)
                {
                    foreach (var deferred in QueryStrategy.BuildDeferred(context))
                    {
                        operationNode.Deferred.Add(deferred);
                    }
                }

                if (context.Streams.Count > 0)
                {
                    operationNode.Streams.AddRange(context.Streams.Values);
                }

                return(operationNode);
            }
            public static OperationNode Build(QueryPlanContext context)
            {
                QueryPlanNode root          = Build(context, context.Operation.GetRootSelectionSet());
                var           operationNode = new OperationNode(root);

                if (context.Deferred.Count > 0)
                {
                    foreach (var deferred in BuildDeferred(context))
                    {
                        operationNode.Deferred.Add(deferred);
                    }
                }

                if (context.Streams.Count > 0)
                {
                    operationNode.Streams.AddRange(context.Streams.Values);
                }

                return(operationNode);
            }
Example #11
0
 public StreamPlanNode(int id, QueryPlanNode root)
 {
     Id   = id;
     Root = root;
 }
Example #12
0
 public OperationNode(QueryPlanNode operation)
     : base(ExecutionStrategy.Serial)
 {
     Operation = operation;
     AddNode(operation);
 }