public async Task Compile_Arguments_Schema()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolverWithSchema");
            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();;
            var context = new Mock <IResolverContext>();

            context.Setup(t => t.Parent <Resolvers>())
            .Returns(new Resolvers());
            context.SetupGet(t => t.Schema)
            .Returns(schema);
            bool result = (bool)await resolver.Resolver(context.Object);

            Assert.True(result);
        }
        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 async Task Compile_Arguments_Service()
        {
            // arrange
            Type       type           = typeof(Resolvers);
            MemberInfo resolverMember =
                type.GetMethod("ResolverWithService");
            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.Service <MyService>())
            .Returns(new MyService());
            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_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_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);
        }
Beispiel #8
0
        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>(
                              WellKnownContextData.EventMessage))
            .Returns(new Mock <IEventMessage>().Object);
            bool result = (bool)await resolver.Resolver(context.Object);

            Assert.True(result);
        }
Beispiel #9
0
        public static void Compile(
            IDictionary <FieldReference, RegisteredResolver> resolvers)
        {
            if (resolvers == null)
            {
                throw new ArgumentNullException(nameof(resolvers));
            }

            var compiler = new ExpressionCompiler();

            foreach (var item in resolvers.ToArray())
            {
                RegisteredResolver registered = item.Value;
                if (registered.Field is FieldMember member)
                {
                    ResolverDescriptor descriptor =
                        registered.IsSourceResolver
                            ? new ResolverDescriptor(
                            registered.SourceType,
                            member)
                            : new ResolverDescriptor(
                            registered.ResolverType,
                            registered.SourceType,
                            member);
                    FieldResolver resolver = compiler.Compile(descriptor);
                    resolvers[item.Key] = registered.WithField(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 ResolverCompiler();
            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 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);
            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 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.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 ResolverCompiler();
            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);
        }