Example #1
0
        private static ISchema TypedSchema()
        {
            var query = new ObjectGraphType();

            query.Field <StringGraphType>(
                "author",
                arguments: new QueryArguments(new QueryArgument <AuthorInputType> {
                Name = "input"
            }),
                resolve: context => "testing"
                );

            query.Connection <PostGraphType>()
            .Name("posts")
            .AuthorizeWith("ConnectionPolicy")
            .Resolve(ctx => new Connection <Post>());

            query.Field <StringGraphType>(
                "project",
                arguments: new QueryArguments(new QueryArgument <AuthorInputType> {
                Name = "input"
            }),
                resolve: context => "testing"
                ).AuthorizeWith("AdminPolicy").AuthorizeWith("ConfidentialPolicy");

            return(new Schema {
                Query = query
            });
        }
Example #2
0
 public void ConfigureQueries(ObjectGraphType queryContext)
 {
     queryContext.Connection <ProductType>()
     .Name("products")
     .Argument <StringGraphType>("query", "The search query")
     .Resolve(ResolveQuickSearch);
 }
Example #3
0
        private void Build()
        {
            switch (_output)
            {
            case QueryOutput.Single:
                _objectGraphType.FieldAsync <ModelConventionType <TSource> >(_queryName,
                                                                             description: _description,
                                                                             arguments: _queryParameterBuilder.GetQueryArguments(), resolve: QueryResolver);
                break;

            case QueryOutput.List:
                if (_pageLimit > 0)
                {
                    var cb = _objectGraphType.Connection <ModelConventionType <TSource> >().Name(_queryName);
                    if (!string.IsNullOrEmpty(_description))
                    {
                        cb.Description(_description);
                    }
                    _queryParameterBuilder.PopulateWithArguments(cb);
                    cb.ResolveAsync(ConnectionResolver);
                }
                else
                {
                    _objectGraphType.FieldAsync <ListGraphType <ModelConventionType <TSource> > >(_queryName,
                                                                                                  description: _description,
                                                                                                  arguments: _queryParameterBuilder.GetQueryArguments(), resolve: QueryResolver);
                }

                break;
            }
        }
        public void should_throw_error_if_name_is_null_or_empty(string fieldName)
        {
            var type      = new ObjectGraphType();
            var exception = Should.Throw <ArgumentOutOfRangeException>(() => type.Connection <ObjectGraphType>().Name(fieldName));

            exception.Message.ShouldStartWith("A field name can not be null or empty.");
        }
Example #5
0
        public void should_throw_error_if_name_is_null_or_empty(string fieldName)
        {
            var type = new ObjectGraphType();
            ArgumentOutOfRangeException exception;

            // race condition with does_not_throw_with_filtering_nameconverter test
            try
            {
                exception = Should.Throw <ArgumentOutOfRangeException>(() => type.Connection <ObjectGraphType>().Name(fieldName));
            }
            catch (ShouldAssertException)
            {
                System.Threading.Thread.Sleep(100); // wait a bit and retry
                exception = Should.Throw <ArgumentOutOfRangeException>(() => type.Connection <ObjectGraphType>().Name(fieldName));
            }

            exception.Message.ShouldStartWith("A field name can not be null or empty.");
        }
Example #6
0
    public async Task can_define_simple_connection_with__custom_edge_type()
    {
        var type       = new ObjectGraphType();
        var connection = new Connection <Child, ParentChildrenEdge>
        {
            TotalCount = 1,
            PageInfo   = new PageInfo
            {
                HasNextPage     = true,
                HasPreviousPage = false,
                StartCursor     = "01",
                EndCursor       = "01",
            },
            Edges = new List <ParentChildrenEdge>
            {
                new ParentChildrenEdge
                {
                    Cursor = "01",
                    Node   = new Child
                    {
                        Field1 = "abcd",
                    },
                    FriendedAt = FriendedAt
                },
            },
        };

        type.Connection <ChildType, ParentChildrenEdgeType>()
        .Name("testConnection")
        .ResolveAsync(resArgs => Task.FromResult <object>(connection));

        var field = type.Fields.Single();

        field.Name.ShouldBe("testConnection");
        field.Type.ShouldBe(typeof(ConnectionType <ChildType, ParentChildrenEdgeType>));

        var boxedResult = await field.Resolver.ResolveAsync(new ResolveFieldContext()).ConfigureAwait(false);

        var result = boxedResult as Connection <Child, ParentChildrenEdge>;

        result.ShouldNotBeNull();
        if (result != null)
        {
            result.TotalCount.ShouldBe(1);
            result.PageInfo.HasNextPage.ShouldBe(true);
            result.PageInfo.HasPreviousPage.ShouldBe(false);
            result.PageInfo.StartCursor.ShouldBe("01");
            result.PageInfo.EndCursor.ShouldBe("01");
            result.Edges.Count.ShouldBe(1);
            result.Edges.First().Cursor.ShouldBe("01");
            result.Edges.First().Node.Field1.ShouldBe("abcd");
            result.Items.Count.ShouldBe(1);
            result.Items.First().Field1.ShouldBe("abcd");
            result.Edges.ShouldAllBe(c => c.FriendedAt == FriendedAt);
        }
    }
Example #7
0
        public void WithScope0Alt()
        {
            var graph   = new ObjectGraphType();
            var builder = graph.Connection <StringGraphType>();
            var field   = builder.FieldType;

            builder.ResolveScoped(context => "hello");
            field.Resolver.Resolve(_scopedContext).ShouldBe("hello");
            VerifyScoped();
        }
        public void should_use_pagesize()
        {
            var graph = new ObjectGraphType();

            graph.Connection <ChildType>()
            .Name("connection")
            .PageSize(10)
            .Resolve(context => context.First);
            graph.Fields.Find("connection").Resolver.Resolve(new ResolveFieldContext()).ShouldBe(10);
        }
Example #9
0
    public async Task should_use_pagesize_async()
    {
        var graph = new ObjectGraphType();

        graph.Connection <ChildType>()
        .Name("connection")
        .PageSize(10)
        .ResolveAsync(context => Task.FromResult <object>(context.First));
        (await graph.Fields.Find("connection").Resolver.ResolveAsync(new ResolveFieldContext()).ConfigureAwait(false)).ShouldBe(10);
    }
        public void should_use_pagesize_async()
        {
            var graph = new ObjectGraphType();

            graph.Connection <ChildType>()
            .Name("connection")
            .PageSize(10)
            .ResolveAsync(context => Task.FromResult <object>(context.First));
            graph.Fields.Find("connection").Resolver.Resolve(new ResolveFieldContext()).ShouldBeOfType <Task <object> >().Result.ShouldBe(10);
        }
Example #11
0
        public void WithScope0AsyncAlt()
        {
            var graph   = new ObjectGraphType();
            var builder = graph.Connection <StringGraphType>();
            var field   = builder.FieldType;

            builder.ResolveScopedAsync(context => Task.FromResult <object>("hello"));
            field.Resolver.Resolve(_scopedContext).ShouldBeTask("hello");
            VerifyScoped();
        }
        public async Task can_define_simple_connection_with__async_resolver()
        {
            var type       = new ObjectGraphType();
            var connection = new Connection <Child>
            {
                TotalCount = 1,
                PageInfo   = new PageInfo
                {
                    HasNextPage     = true,
                    HasPreviousPage = false,
                    StartCursor     = "01",
                    EndCursor       = "01",
                },
                Edges = new List <Edge <Child> >
                {
                    new Edge <Child>
                    {
                        Cursor = "01",
                        Node   = new Child
                        {
                            Field1 = "abcd",
                        },
                    },
                },
            };

            type.Connection <ObjectGraphType>()
            .Name("testConnection")
            .ResolveAsync(resArgs => Task.FromResult <object>(connection));

            var field = type.Fields.Single();

            field.Name.ShouldBe("testConnection");
            field.Type.ShouldBe(typeof(ConnectionType <ObjectGraphType, EdgeType <ObjectGraphType> >));

            var boxedResult = await(Task <object>) field.Resolver.Resolve(new ResolveFieldContext());
            var result      = boxedResult as Connection <Child>;

            result.ShouldNotBeNull();
            if (result != null)
            {
                result.TotalCount.ShouldBe(1);
                result.PageInfo.HasNextPage.ShouldBe(true);
                result.PageInfo.HasPreviousPage.ShouldBe(false);
                result.PageInfo.StartCursor.ShouldBe("01");
                result.PageInfo.EndCursor.ShouldBe("01");
                result.Edges.Count.ShouldBe(1);
                result.Edges.First().Cursor.ShouldBe("01");
                result.Edges.First().Node.Field1.ShouldBe("abcd");
                result.Items.Count.ShouldBe(1);
                result.Items.First().Field1.ShouldBe("abcd");
            }
        }
    public void WithoutScope0()
    {
        var graph   = new ObjectGraphType();
        var builder = graph.Connection <StringGraphType>();
        var field   = builder.FieldType;

        builder
        .Resolve()
        .Resolve(context => "hello");
        field.Resolver.ResolveAsync(_unscopedConnectionContext).Result.ShouldBe("hello");
        VerifyUnscoped();
    }
Example #14
0
        public void bidirectional_called_twice_creates_proper_arguments()
        {
            var graph = new ObjectGraphType();

            graph.Connection <ChildType>()
            .Name("connection")
            .Description("RandomDescription")
            .Bidirectional()
            .Bidirectional();

            graph.Fields.Find("connection").Arguments.Count(x => x.Name == "before").ShouldBe(1);
            graph.Fields.Find("connection").Arguments.Count(x => x.Name == "last").ShouldBe(1);
        }
Example #15
0
        public void WithoutScope1Async()
        {
            var graph   = new ObjectGraphType();
            var builder = graph.Connection <StringGraphType>();
            var field   = builder.FieldType;

            builder
            .Resolve()
            .WithService <string>()
            .ResolveAsync((context, value) => Task.FromResult <object>(value));
            field.Resolver.Resolve(_unscopedConnectionContext).ShouldBeTask("hello");
            VerifyUnscoped();
        }
        public void can_define_simple_connection_with_resolver()
        {
            var type = new ObjectGraphType();

            type.Connection <ObjectGraphType>()
            .Name("testConnection")
            .Resolve(resArgs =>
                     new Connection <Child>
            {
                TotalCount = 1,
                PageInfo   = new PageInfo
                {
                    HasNextPage     = true,
                    HasPreviousPage = false,
                    StartCursor     = "01",
                    EndCursor       = "01",
                },
                Edges = new List <Edge <Child> >
                {
                    new Edge <Child>
                    {
                        Cursor = "01",
                        Node   = new Child
                        {
                            Field1 = "abcd",
                        },
                    },
                },
            });

            var field = type.Fields.Single();

            field.Name.ShouldEqual("testConnection");
            field.Type.ShouldEqual(typeof(ConnectionType <ObjectGraphType>));
            var result = field.Resolve(null) as Connection <Child>;

            result.ShouldNotBeNull();
            if (result != null)
            {
                result.TotalCount.ShouldEqual(1);
                result.PageInfo.HasNextPage.ShouldEqual(true);
                result.PageInfo.HasPreviousPage.ShouldEqual(false);
                result.PageInfo.StartCursor.ShouldEqual("01");
                result.PageInfo.EndCursor.ShouldEqual("01");
                result.Edges.Count.ShouldEqual(1);
                result.Edges.First().Cursor.ShouldEqual("01");
                result.Edges.First().Node.Field1.ShouldEqual("abcd");
                result.Items.Count.ShouldEqual(1);
                result.Items.First().Field1.ShouldEqual("abcd");
            }
        }
Example #17
0
        public void WithScope1()
        {
            var graph   = new ObjectGraphType();
            var builder = graph.Connection <StringGraphType>();
            var field   = builder.FieldType;

            builder
            .Resolve()
            .WithScope()
            .WithService <string>()
            .Resolve((context, value) => value);
            field.Resolver.Resolve(_scopedContext).ShouldBe("hello");
            VerifyScoped();
        }
        public void ConfigureQueries(ObjectGraphType queryContext)
        {
            queryContext.Connection <ProductType>()
            .Name("products")
            .Argument <StringGraphType>("query", "The search query")
            .Resolve(ResolveQuickSearch);

            queryContext.Field <CartType>("cart",
                                          resolve: context =>
            {
                ICart cart = _cartService.LoadCart(_cartService.DefaultCartName);
                return(_cartViewModelFactory.CreateLargeCartViewModel(cart));
            });
        }
Example #19
0
        public void WithoutScope2()
        {
            var graph   = new ObjectGraphType();
            var builder = graph.Connection <StringGraphType>();
            var field   = builder.FieldType;

            builder
            .Resolve()
            .WithService <string>()
            .WithService <int>()
            .Resolve((context, value, v2) => value + v2);
            field.Resolver.Resolve(_unscopedConnectionContext).ShouldBe("hello2");
            VerifyUnscoped();
        }
Example #20
0
        public void WithScope5Alt()
        {
            var graph   = new ObjectGraphType();
            var builder = graph.Connection <StringGraphType>();
            var field   = builder.FieldType;

            builder
            .Resolve()
            .WithScope()
            .WithServices <string, int, short, byte, long>()
            .Resolve((context, value, v2, v3, v4, v5) => value + v2 + v3 + v4 + v5);
            field.Resolver.Resolve(_scopedContext).ShouldBe("hello2345");
            VerifyScoped();
        }
    public void WithScope3Alt()
    {
        var graph   = new ObjectGraphType();
        var builder = graph.Connection <StringGraphType>();
        var field   = builder.FieldType;

        builder
        .Resolve()
        .WithScope()
        .WithServices <string, int, short>()
        .Resolve((context, value, v2, v3) => value + v2 + v3);
        field.Resolver.ResolveAsync(_scopedContext).Result.ShouldBe("hello23");
        VerifyScoped();
    }
Example #22
0
        public void WithScope3Async()
        {
            var graph   = new ObjectGraphType();
            var builder = graph.Connection <StringGraphType>();
            var field   = builder.FieldType;

            builder
            .Resolve()
            .WithService <string>()
            .WithService <int>()
            .WithService <short>()
            .WithScope()
            .ResolveAsync((context, value, v2, v3) => Task.FromResult <object>(value + v2 + v3));
            field.Resolver.Resolve(_scopedContext).ShouldBeTask("hello23");
            VerifyScoped();
        }
    public void WithoutScope4()
    {
        var graph   = new ObjectGraphType();
        var builder = graph.Connection <StringGraphType>();
        var field   = builder.FieldType;

        builder
        .Resolve()
        .WithService <string>()
        .WithService <int>()
        .WithService <short>()
        .WithService <byte>()
        .Resolve((context, value, v2, v3, v4) => value + v2 + v3 + v4);
        field.Resolver.ResolveAsync(_unscopedConnectionContext).Result.ShouldBe("hello234");
        VerifyUnscoped();
    }
Example #24
0
        public void WithoutScope5Async()
        {
            var graph   = new ObjectGraphType();
            var builder = graph.Connection <StringGraphType>();
            var field   = builder.FieldType;

            builder
            .Resolve()
            .WithService <string>()
            .WithService <int>()
            .WithService <short>()
            .WithService <byte>()
            .WithService <long>()
            .ResolveAsync((context, value, v2, v3, v4, v5) => Task.FromResult <object>(value + v2 + v3 + v4 + v5));
            field.Resolver.Resolve(_unscopedConnectionContext).ShouldBeTask("hello2345");
            VerifyUnscoped();
        }
Example #25
0
        public static void DefineConnectionAsync <TMyT, TGraphType, TSourceType>(
            this ObjectGraphType <TMyT> obj,
            string name,
            DataAccessUserContext userContext,
            ConnectionNodeResolver <TSourceType> nodeResolve
            ) where TGraphType : IGraphType
        {
            obj.Connection <TGraphType>()
            .Name(name)
            .Argument <IntGraphType>("first", "Only get the first n elements")
            .Argument <IntGraphType>("after", "Skip elements up to the 'after' element")
            .Argument <StringGraphType>("order", "Specifies how to order the data. This is type-specific, but you can generally just type the name of a field.")
            .ResolveAsync(async ctx =>
            {
                var first = ctx.GetArgument <int>("first");
                var after = ctx.GetArgument <int>("after");
                var order = ctx.GetArgument <string>("order");

                if (first >= PagingConstants.ItemsPerPage || first == 0)
                {
                    first = PagingConstants.ItemsPerPage;
                }

                var values = await nodeResolve(userContext, first, after, order);

                return(new Connection <TSourceType>
                {
                    TotalCount = values.Count(),
                    PageInfo = new PageInfo
                    {
                        EndCursor = Convert.ToString(after + values.Count()),
                        StartCursor = Convert.ToString(after),
                        HasNextPage = values.Count() == PagingConstants.ItemsPerPage,
                        HasPreviousPage = after > 0
                    },
                    Edges = values.Select(c => new Edge <TSourceType>
                    {
                        Cursor = "TODO",
                        Node = c
                    }).ToList()
                });
            });
        }
    public void ConnectionBuilder()
    {
        var graph = new ObjectGraphType();

        graph.Connection <StringGraphType>()
        .Name("Field")
        .AuthorizeWith("Policy1")
        .AuthorizeWith("Policy2")
        .AuthorizeWith("Policy2")
        .AuthorizeWithPolicy("Policy3")
        .AuthorizeWithPolicy("Policy3")
        .AuthorizeWithRoles("Role1,Role2")
        .AuthorizeWithRoles("Role3, Role2")
        .AuthorizeWithRoles("Role1", "Role4");

        var field = graph.Fields.Find("Field");

        field.IsAuthorizationRequired().ShouldBeTrue();
        field.GetPolicies().ShouldBe(new string[] { "Policy1", "Policy2", "Policy3" });
        field.GetRoles().ShouldBe(new string[] { "Role1", "Role2", "Role3", "Role4" });
    }