public void CreateSourcePropertyResolver()
        {
            // arrange
            Mock <IResolverContext> context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.Setup(t => t.Parent <FooType>()).Returns(new FooType());

            FieldReference          fieldReference = new FieldReference("type", "field");
            FieldResolverDescriptor descriptor     = FieldResolverDescriptor
                                                     .CreateSourceProperty(fieldReference, typeof(FooType),
                                                                           typeof(FooType).GetProperty("BarProperty"));

            // act
            FieldResolverBuilder fieldResolverBuilder = new FieldResolverBuilder();

            FieldResolver[] resolvers = fieldResolverBuilder.Build(
                new[] { descriptor }).ToArray();

            // assert
            Assert.Collection(resolvers,
                              r =>
            {
                Assert.Equal("type", r.TypeName);
                Assert.Equal("field", r.FieldName);
                Assert.NotNull(r.Resolver);

                object result = r.Resolver(context.Object, CancellationToken.None);
                Assert.Equal("Hello World Property", result);
            });
        }
예제 #2
0
        public void SyncSourceMethodGenerator_GenerateWithTwoArgument()
        {
            // arrange
            FieldResolverArgumentDescriptor argumentDescriptor1 =
                FieldResolverArgumentDescriptor.Create("a",
                                                       FieldResolverArgumentKind.Argument,
                                                       typeof(string));

            FieldResolverArgumentDescriptor argumentDescriptor2 =
                FieldResolverArgumentDescriptor.Create("b",
                                                       FieldResolverArgumentKind.Argument,
                                                       typeof(int));

            MethodInfo method = typeof(GeneratorTestDummy).GetMethods()
                                .Single(t => t.Name == "GetFoo" && t.GetParameters().Length == 2);
            FieldResolverDescriptor descriptor = FieldResolverDescriptor
                                                 .CreateSourceMethod(new FieldReference("Foo", "bar"),
                                                                     method.ReflectedType, method, false,
                                                                     new[] { argumentDescriptor1, argumentDescriptor2 });

            // act
            StringBuilder             source    = new StringBuilder();
            SyncSourceMethodGenerator generator = new SyncSourceMethodGenerator();
            string result = generator.Generate("abc", descriptor);

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
예제 #3
0
        public void AsyncResolverMethodGenerator_GenerateWithSourceArgumentAndTwoArguments()
        {
            // arrange
            FieldResolverArgumentDescriptor argumentDescriptor1 =
                FieldResolverArgumentDescriptor.Create("a",
                                                       FieldResolverArgumentKind.Source,
                                                       typeof(GeneratorTestDummy));

            FieldResolverArgumentDescriptor argumentDescriptor2 =
                FieldResolverArgumentDescriptor.Create("b",
                                                       FieldResolverArgumentKind.Argument,
                                                       typeof(string));

            FieldResolverArgumentDescriptor argumentDescriptor3 =
                FieldResolverArgumentDescriptor.Create("c",
                                                       FieldResolverArgumentKind.Argument,
                                                       typeof(int));

            Type       sourceType = typeof(GeneratorTestDummy);
            MethodInfo method     = typeof(GeneratorTestDummyResolver).GetMethods()
                                    .Single(t => t.Name == "GetFooAsync" && t.GetParameters().Length == 3);
            FieldResolverDescriptor descriptor = FieldResolverDescriptor
                                                 .CreateCollectionMethod(new FieldReference("Foo", "bar"),
                                                                         method.ReflectedType, sourceType, method, true,
                                                                         new[] { argumentDescriptor1, argumentDescriptor2, argumentDescriptor3 });

            // act
            StringBuilder source = new StringBuilder();
            AsyncResolverMethodGenerator generator = new AsyncResolverMethodGenerator();
            string result = generator.Generate("abc", descriptor);

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
예제 #4
0
        public void SourcePropertyGenerator_Generate()
        {
            // arrange
            PropertyInfo            property   = typeof(GeneratorTestDummy).GetProperties().Single();
            FieldResolverDescriptor descriptor = FieldResolverDescriptor
                                                 .CreateSourceProperty(new FieldReference("Foo", "bar"),
                                                                       property.ReflectedType, property);

            // act
            StringBuilder           source    = new StringBuilder();
            SourcePropertyGenerator generator = new SourcePropertyGenerator();
            string result = generator.Generate("abc", descriptor);

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
예제 #5
0
        public void SyncSourceMethodGenerator_GenerateWithoutArguments()
        {
            // arrange
            MethodInfo method = typeof(GeneratorTestDummy).GetMethods()
                                .Single(t => t.Name == "GetFoo" && t.GetParameters().Length == 0);
            FieldResolverDescriptor descriptor = FieldResolverDescriptor
                                                 .CreateSourceMethod(new FieldReference("Foo", "bar"),
                                                                     method.ReflectedType, method, false,
                                                                     Enumerable.Empty <FieldResolverArgumentDescriptor>());

            // act
            StringBuilder             source    = new StringBuilder();
            SyncSourceMethodGenerator generator = new SyncSourceMethodGenerator();
            string result = generator.Generate("abc", descriptor);

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
        private FieldResolverDescriptor CreateSourceResolverDescriptor(
            FieldResolverMember fieldResolverMember,
            Type resolverType, Type sourceType)
        {
            if (fieldResolverMember.Member is PropertyInfo p)
            {
                return(FieldResolverDescriptor.CreateSourceProperty(
                           fieldResolverMember, sourceType, p));
            }
            else if (fieldResolverMember.Member is MethodInfo m)
            {
                bool isAsync = typeof(Task).IsAssignableFrom(m.ReturnType);
                IReadOnlyCollection <FieldResolverArgumentDescriptor> argumentDescriptors =
                    CreateResolverArgumentDescriptors(m, resolverType, sourceType);
                return(FieldResolverDescriptor.CreateSourceMethod(
                           fieldResolverMember, sourceType, m, isAsync,
                           argumentDescriptors));
            }

            throw new NotSupportedException();
        }
        public void CreateAsyncCollectionMethodResolver()
        {
            // arrange
            Mock <IResolverContext> context = new Mock <IResolverContext>(MockBehavior.Strict);

            context.Setup(t => t.Parent <FooType>()).Returns(new FooType());
            context.Setup(t => t.Service <FooTypeResolver>()).Returns(new FooTypeResolver());

            FieldResolverArgumentDescriptor argumentDescriptor =
                new FieldResolverArgumentDescriptor(
                    "foo", "b", FieldResolverArgumentKind.Source,
                    typeof(FooType));

            FieldReference          fieldReference = new FieldReference("type", "field");
            FieldResolverDescriptor descriptor     = FieldResolverDescriptor
                                                     .CreateCollectionMethod(fieldReference,
                                                                             typeof(FooTypeResolver), typeof(FooType),
                                                                             typeof(FooTypeResolver).GetMethod("BarResolverAsync"), true,
                                                                             new[] { argumentDescriptor });

            // act
            FieldResolverBuilder fieldResolverBuilder = new FieldResolverBuilder();

            FieldResolver[] resolvers = fieldResolverBuilder.Build(
                new[] { descriptor }).ToArray();

            // assert
            Assert.Collection(resolvers,
                              r =>
            {
                Assert.Equal("type", r.TypeName);
                Assert.Equal("field", r.FieldName);
                Assert.NotNull(r.Resolver);

                object result = ((Task <object>)r.Resolver(context.Object, CancellationToken.None)).Result;
                Assert.Equal("Hello World_123", result);
            });
        }
        public void AsyncResolverMethodGenerator_GenerateWithQueryService()
        {
            // arrange
            var argumentDescriptor =
                new FieldResolverArgumentDescriptor("a", "b",
                                                    FieldResolverArgumentKind.Service,
                                                    typeof(GeneratorTestDummy));

            var        sourceType = typeof(GeneratorTestDummy);
            MethodInfo method     = typeof(GeneratorTestDummyResolver).GetMethods()
                                    .Single(t => t.Name == "GetFooAsync" && t.GetParameters().Length == 1);
            var descriptor = FieldResolverDescriptor
                             .CreateCollectionMethod(new FieldReference("Foo", "bar"),
                                                     method.ReflectedType, sourceType, method, true,
                                                     new[] { argumentDescriptor });

            // act
            var    source    = new StringBuilder();
            var    generator = new AsyncResolverMethodGenerator();
            string result    = generator.Generate("abc", descriptor);

            // assert
            Assert.Equal(Snapshot.Current(), Snapshot.New(result));
        }
 public static int ArgumentCount(this FieldResolverDescriptor descriptor)
 {
     return(descriptor.ArgumentDescriptors
            .Where(t => t.Kind == FieldResolverArgumentKind.Argument)
            .Count());
 }
 public static IEnumerable <FieldResolverArgumentDescriptor> Arguments(
     this FieldResolverDescriptor descriptor)
 {
     return(descriptor.ArgumentDescriptors
            .Where(t => t.Kind == FieldResolverArgumentKind.Argument));
 }