Example #1
0
        public async Task InvokeAsync(IMiddlewareContext context)
        {
            DelegateDirective delegateDirective = context.Field
                                                  .Directives[DirectiveNames.Delegate]
                                                  .FirstOrDefault()?.ToObject <DelegateDirective>();

            if (delegateDirective != null)
            {
                IImmutableStack <SelectionPathComponent> path =
                    delegateDirective.Path is null
                    ? ImmutableStack <SelectionPathComponent> .Empty
                    : SelectionPathParser.Parse(delegateDirective.Path);

                IReadOnlyQueryRequest request =
                    CreateQuery(context, delegateDirective.Schema, path);

                IReadOnlyQueryResult result = await ExecuteQueryAsync(
                    context, request, delegateDirective.Schema)
                                              .ConfigureAwait(false);

                UpdateContextData(context, result, delegateDirective);

                context.Result = new SerializedData(
                    ExtractData(result.Data, path.Count()));
                ReportErrors(context, result.Errors);
            }

            await _next.Invoke(context).ConfigureAwait(false);
        }
        public void Parse_PathWithVarArgs_ThreeComponentsOneWithVarArgs()
        {
            // arrange
            var serializedPath = new Source("foo(a: $foo:bar).bar.baz");

            // act
            Stack <SelectionPathComponent> path =
                SelectionPathParser.Parse(serializedPath);

            // assert
            Assert.Collection(path.Reverse(),
                              c =>
            {
                Assert.Equal("foo", c.Name.Value);
                Assert.Collection(c.Arguments,
                                  a =>
                {
                    Assert.Equal("a", a.Name.Value);
                    Assert.IsType <ScopedVariableNode>(a.Value);

                    var v = (ScopedVariableNode)a.Value;
                    Assert.Equal("foo_bar", v.ToVariableName());
                });
            },
                              c =>
            {
                Assert.Equal("bar", c.Name.Value);
                Assert.Empty(c.Arguments);
            },
                              c =>
            {
                Assert.Equal("baz", c.Name.Value);
                Assert.Empty(c.Arguments);
            });
        }
        public void Parse_PathWithoutArgs_ThreeComponentsFound()
        {
            // arrange
            var serializedPath = new Source("foo.bar.baz");

            // act
            Stack <SelectionPathComponent> path =
                SelectionPathParser.Parse(serializedPath);

            // assert
            Assert.Collection(path.Reverse(),
                              t =>
            {
                Assert.Equal("foo", t.Name.Value);
                Assert.Empty(t.Arguments);
            },
                              t =>
            {
                Assert.Equal("bar", t.Name.Value);
                Assert.Empty(t.Arguments);
            },
                              t =>
            {
                Assert.Equal("baz", t.Name.Value);
                Assert.Empty(t.Arguments);
            });
        }
Example #4
0
        public void Parse_Two_Chain_With_Literal()
        {
            // arrange
            var pathString = "foo(bar: 1).baz(quox: 2)";

            // act
            IImmutableStack <SelectionPathComponent> path =
                SelectionPathParser.Parse(pathString);

            // assert
            Assert.Collection(path.Reverse(),
                              segment =>
            {
                Assert.Equal("foo", segment.Name.Value);
                Assert.Collection(segment.Arguments,
                                  argument =>
                {
                    Assert.Equal("bar", argument.Name.Value);
                    Assert.Equal("1", argument.Value.Value);
                });
            },
                              segment =>
            {
                Assert.Equal("baz", segment.Name.Value);
                Assert.Collection(segment.Arguments,
                                  argument =>
                {
                    Assert.Equal("quox", argument.Name.Value);
                    Assert.Equal("2", argument.Value.Value);
                });
            });
        }
Example #5
0
        public void Parse_Single_Chain_With_ScopedVariable()
        {
            // arrange
            var pathString = "foo(bar: $fields:foo)";

            // act
            IImmutableStack <SelectionPathComponent> path =
                SelectionPathParser.Parse(pathString);

            // assert
            Assert.Collection(path,
                              segment =>
            {
                Assert.Equal("foo", segment.Name.Value);
                Assert.Collection(segment.Arguments,
                                  argument =>
                {
                    Assert.Equal("bar", argument.Name.Value);

                    ScopedVariableNode variable =
                        Assert.IsType <ScopedVariableNode>(argument.Value);

                    Assert.Equal("fields", variable.Scope.Value);
                    Assert.Equal("foo", variable.Name.Value);
                });
            });
        }
Example #6
0
        private Stack <SelectionPathComponent> GetSelectionPath(
            IDirectiveContext directiveContext)
        {
            var directive = directiveContext.Directive
                            .ToObject <DelegateDirective>();

            if (string.IsNullOrEmpty(directive.Path))
            {
                return(new Stack <SelectionPathComponent>());
            }

            return(SelectionPathParser.Parse(new Source(directive.Path)));
        }
Example #7
0
        public void Parse_Single_Chain_No_Arguments()
        {
            // arrange
            var pathString = "foo";

            // act
            IImmutableStack <SelectionPathComponent> path =
                SelectionPathParser.Parse(pathString);

            // assert
            Assert.Collection(path,
                              segment =>
            {
                Assert.Equal("foo", segment.Name.Value);
                Assert.Empty(segment.Arguments);
            });
        }
        public void Parse_PathWithArgs_ThreeComponentsTwoWithArgs()
        {
            // arrange
            var serializedPath = new Source("foo(a: 1).bar.baz(b: \"s\")");

            // act
            Stack <SelectionPathComponent> path =
                SelectionPathParser.Parse(serializedPath);

            // assert
            Assert.Collection(path.Reverse(),
                              c =>
            {
                Assert.Equal("foo", c.Name.Value);
                Assert.Collection(c.Arguments,
                                  a =>
                {
                    Assert.Equal("a", a.Name.Value);
                    Assert.IsType <IntValueNode>(a.Value);
                });
            },
                              c =>
            {
                Assert.Equal("bar", c.Name.Value);
                Assert.Empty(c.Arguments);
            },
                              c =>
            {
                Assert.Equal("baz", c.Name.Value);
                Assert.Collection(c.Arguments,
                                  a =>
                {
                    Assert.Equal("b", a.Name.Value);
                    Assert.IsType <StringValueNode>(a.Value);
                });
            });
        }