public async Task Compile_Arguments_Document()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolverWithDocument");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                new FieldMember("A", "b", resolverMember));

            // act
            var           compiler = new ResolverCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>())
            .Returns(new Resolvers());
            context.SetupGet(t => t.Document)
            .Returns(new DocumentNode(
                         null,
                         Array.Empty <IDefinitionNode>()));
            bool result = (bool)await resolver.Resolver(context.Object);

            Assert.True(result);
        }
        public async Task Compile_Arguments_Operation()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolverWithOperationDefinition");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                new FieldMember("A", "b", resolverMember));

            // act
            var           compiler = new ResolverCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>())
            .Returns(new Resolvers());
            context.SetupGet(t => t.Operation)
            .Returns(new OperationDefinitionNode(
                         null,
                         null,
                         OperationType.Query,
                         Array.Empty <VariableDefinitionNode>(),
                         Array.Empty <DirectiveNode>(),
                         new SelectionSetNode(
                             null,
                             Array.Empty <ISelectionNode>())));
            bool result = (bool)await resolver.Resolver(context.Object);

            Assert.True(result);
        }
        public async Task Compile_Arguments_IOutputField()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolverWithOutputField");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                new FieldMember("A", "b", resolverMember));

            // act
            var           compiler = new ResolverCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            ISchema schema = SchemaBuilder.New()
                             .AddDocumentFromString("type Query { a: String }")
                             .AddResolver("Query", "a", "foo")
                             .Create();;
            ObjectType queryType = schema.GetType <ObjectType>("Query");
            var        context   = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>())
            .Returns(new Resolvers());
            context.SetupGet(t => t.Field)
            .Returns(queryType.Fields.First());
            bool result = (bool)await resolver.Resolver(context.Object);

            Assert.True(result);
        }
        public async Task Compile_Arguments_EventMessage()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolverWithEventMessage");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                new FieldMember("A", "b", resolverMember));

            // act
            var           compiler = new ResolverCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>())
            .Returns(new Resolvers());
            context.Setup(t => t.CustomProperty <IEventMessage>(
                              typeof(IEventMessage).FullName))
            .Returns(new Mock <IEventMessage>().Object);
            bool result = (bool)await resolver.Resolver(context.Object);

            Assert.True(result);
        }
        public async Task Compile_Arguments_CancellationToken()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolverWithCancellationToken");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                new FieldMember("A", "b", resolverMember));

            // act
            var           compiler = new ResolverCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>())
            .Returns(new Resolvers());
            context.SetupGet(t => t.RequestAborted)
            .Returns(CancellationToken.None);
            string result = (string)await resolver.Resolver(context.Object);

            Assert.Equal("cancel", result);
        }
        public async Task Compile_StringMethod_WithParams_Resolver()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("StringResolverWithArg");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                typeof(Entity),
                new FieldMember("A", "b", resolverMember));

            // act
            var           compiler = new ResolverCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Resolver <Resolvers>())
            .Returns(new Resolvers());
            context.Setup(t => t.Argument <string>("a")).Returns("abc");
            string result = (string)await resolver.Resolver(context.Object);

            Assert.Equal("abc", result);
        }
Exemple #7
0
        public async Task Compile_Arguments_ScopedContextData()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolveWithScopedContextData");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                new FieldMember("A", "b", resolverMember));
            var contextData = ImmutableDictionary <string, object> .Empty
                              .SetItem("foo", "bar");

            // act
            var           compiler = new ResolverCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>()).Returns(new Resolvers());
            context.Setup(t => t.ScopedContextData).Returns(contextData);
            string result = (string)await resolver.Resolver(context.Object);

            Assert.Equal("bar", result);
        }
        public FieldResolver Compile(ResolverDescriptor descriptor)
        {
            MethodInfo resolverMethod = descriptor.ResolverType is null
                ? Parent.MakeGenericMethod(descriptor.SourceType)
                : Resolver.MakeGenericMethod(descriptor.ResolverType);

            Expression resolverInstance = Expression.Call(
                Context, resolverMethod);

            if (descriptor.Field.Member is { })
        public FieldResolver Compile(ResolverDescriptor descriptor)
        {
            MethodInfo resolverMethod = descriptor.ResolverType is null
                ? _parent.MakeGenericMethod(descriptor.SourceType)
                : _resolver.MakeGenericMethod(descriptor.ResolverType);

            Expression resolverInstance = Expression.Call(
                _context, resolverMethod);

            FieldResolverDelegate resolver = CreateResolver(
                resolverInstance,
                descriptor.Field.Member,
                descriptor.SourceType);

            return(new FieldResolver(
                       descriptor.Field.TypeName,
                       descriptor.Field.FieldName,
                       resolver));
        }
        public async Task Compile_ObjMethod_NoParams_SourceResolver()
        {
            // arrange
            Type       type               = typeof(Resolvers);
            MemberInfo resolverMember     = type.GetMethod("ObjectResolver");
            var        resolverDescriptor = new ResolverDescriptor(
                type,
                new FieldMember("A", "b", resolverMember));

            // act
            var           compiler = new ResolveCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>()).Returns(new Resolvers());
            string result = (string)await resolver.Resolver(context.Object);

            Assert.Equal("ObjectResolverResult", result);
        }
        public void Compile_Arguments_ScopedContextData_NotExists()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolveWithScopedContextData");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                new FieldMember("A", "b", resolverMember));
            var contextData = ImmutableDictionary <string, object> .Empty;

            // act
            var           compiler = new ResolveCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>()).Returns(new Resolvers());
            context.Setup(t => t.ScopedContextData).Returns(contextData);
            Action action = () => resolver.Resolver(context.Object);

            Assert.Throws <ArgumentException>(action);
        }
        public async Task Compile_Arguments_ContextData_DefaultValue()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolveWithContextDataDefault");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                new FieldMember("A", "b", resolverMember));
            var contextData = new Dictionary <string, object>();

            // act
            var           compiler = new ResolveCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>()).Returns(new Resolvers());
            context.Setup(t => t.ContextData).Returns(contextData);
            object result = await resolver.Resolver(context.Object);

            Assert.Null(result);
        }
        public async Task Compile_StringProperty_Resolver()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetProperty("StringProp");
            var resolverDescriptor = new ResolverDescriptor(
                type,
                typeof(Entity),
                new FieldMember("A", "b", resolverMember));

            // act
            var           compiler = new ResolveCompiler();
            FieldResolver resolver = compiler.Compile(resolverDescriptor);

            // assert
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Resolver <Resolvers>())
            .Returns(new Resolvers());
            string result = (string)await resolver.Resolver(context.Object);

            Assert.Equal("StringProp", result);
        }