public void CreateSourcePropertyResolver() { // arrange var context = new Mock <IResolverContext>(MockBehavior.Strict); context.Setup(t => t.Parent <FooType>()) .Returns(new FooType()); context.Setup(t => t.RequestAborted) .Returns(CancellationToken.None); var fieldMember = new FieldMember( "type", "field", typeof(FooType).GetProperty("BarProperty")); var descriptor = new SourceResolverDescriptor( typeof(FooType), fieldMember); // act var resolverBuilder = new ResolverBuilder(); resolverBuilder.AddDescriptor(descriptor); ResolverBuilderResult result = resolverBuilder.Build(); // assert Assert.Collection(result.Resolvers, r => { Assert.Equal("type", r.TypeName); Assert.Equal("field", r.FieldName); Assert.NotNull(r.Resolver); object resolvedResult = r.Resolver( context.Object).Result; Assert.Equal("Hello World Property", resolvedResult); }); }
public static void Compile( IDictionary <FieldReference, RegisteredResolver> resolvers) { if (resolvers == null) { throw new ArgumentNullException(nameof(resolvers)); } var resolverBuilder = new ResolverBuilder(); foreach (RegisteredResolver resolver in resolvers.Values) { if (resolver.Field is FieldMember member) { if (resolver.IsSourceResolver) { resolverBuilder.AddDescriptor( new SourceResolverDescriptor( resolver.SourceType, member)); } else { resolverBuilder.AddDescriptor( new ResolverDescriptor( resolver.ResolverType, resolver.SourceType, member)); } } } ResolverBuilderResult result = resolverBuilder.Build(); foreach (FieldResolver resolver in result.Resolvers) { var reference = resolver.ToFieldReference(); if (resolvers.TryGetValue(reference, out RegisteredResolver registered)) { resolvers[reference] = registered.WithField(resolver); } } }
public void CreateAsyncCollectionMethodResolver() { // arrange var context = new Mock <IResolverContext>(MockBehavior.Strict); context.Setup(t => t.Parent <FooType>()) .Returns(new FooType()); context.Setup(t => t.Resolver <FooTypeResolver>()) .Returns(new FooTypeResolver()); context.Setup(t => t.RequestAborted) .Returns(CancellationToken.None); var argumentDescriptor = new ArgumentDescriptor( "foo", "b", ArgumentKind.Source, typeof(FooType), null); var fieldMember = new FieldMember( "type", "field", typeof(FooTypeResolver).GetMethod("BarResolverAsync")); var descriptor = new ResolverDescriptor( typeof(FooTypeResolver), typeof(FooType), fieldMember, new[] { argumentDescriptor }); // act var resolverBuilder = new ResolverBuilder(); resolverBuilder.AddDescriptor(descriptor); ResolverBuilderResult result = resolverBuilder.Build(); // assert Assert.Collection(result.Resolvers, r => { Assert.Equal("type", r.TypeName); Assert.Equal("field", r.FieldName); Assert.NotNull(r.Resolver); object resolvedValue = r.Resolver( context.Object) .Result; Assert.Equal("Hello World_123", resolvedValue); }); }