Exemple #1
0
 public IEnumerable <ICharacter> GetCharacter(
     [Parent] ICharacter character,
     [Service] CharacterRepository repository)
 {
     foreach (string friendId in character.Friends)
     {
         ICharacter friend = repository.GetCharacter(friendId);
         if (friend != null)
         {
             yield return(friend);
         }
     }
 }
        private static Schema CreateSchema()
        {
            var repository    = new CharacterRepository();
            var eventRegistry = new InMemoryEventRegistry();
            var registry      = new DataLoaderRegistry(new EmptyServiceProvider());

            var services = new Dictionary <Type, object>
            {
                [typeof(CharacterRepository)] = repository,
                [typeof(Query)]               = new Query(repository),
                [typeof(Mutation)]            = new Mutation(),
                [typeof(Subscription)]        = new Subscription(),
                [typeof(IEventSender)]        = eventRegistry,
                [typeof(IEventRegistry)]      = eventRegistry,
                [typeof(IDataLoaderRegistry)] = registry
            };

            var serviceResolver = new Func <Type, object>(
                t =>
            {
                if (services.TryGetValue(t, out var s))
                {
                    return(s);
                }
                return(null);
            });

            var serviceProvider = new Mock <IServiceProvider>(
                MockBehavior.Strict);

            serviceProvider.Setup(t => t.GetService(It.IsAny <Type>()))
            .Returns(serviceResolver);

            registry.Register(typeof(HumanDataLoader).FullName,
                              s => new HumanDataLoader(repository));

            return(Schema.Create(c =>
            {
                c.RegisterServiceProvider(serviceProvider.Object);

                c.RegisterQueryType <QueryType>();
                c.RegisterMutationType <MutationType>();
                c.RegisterSubscriptionType <SubscriptionType>();

                c.RegisterType <HumanType>();
                c.RegisterType <DroidType>();
                c.RegisterType <EpisodeType>();
            }));
        }
        public static IEnumerable <ICharacter> GetCharacter(
            IResolverContext context)
        {
            ICharacter          character  = context.Parent <ICharacter>();
            CharacterRepository repository = context.Service <CharacterRepository>();

            foreach (string friendId in character.Friends)
            {
                ICharacter friend = repository.GetCharacter(friendId);
                if (friend != null)
                {
                    yield return(friend);
                }
            }
        }
        private static Schema CreateSchema(int executionDepth)
        {
            var repository    = new CharacterRepository();
            var eventRegistry = new InMemoryEventRegistry();

            var services = new Dictionary <Type, object>();

            services[typeof(CharacterRepository)] = repository;
            services[typeof(Query)]          = new Query(repository);
            services[typeof(Mutation)]       = new Mutation();
            services[typeof(Subscription)]   = new Subscription();
            services[typeof(IEventSender)]   = eventRegistry;
            services[typeof(IEventRegistry)] = eventRegistry;

            var serviceResolver = new Func <Type, object>(
                t =>
            {
                if (services.TryGetValue(t, out object s))
                {
                    return(s);
                }
                return(null);
            });

            Mock <IServiceProvider> serviceProvider =
                new Mock <IServiceProvider>(MockBehavior.Strict);

            serviceProvider.Setup(t => t.GetService(It.IsAny <Type>()))
            .Returns(serviceResolver);

            return(Schema.Create(c =>
            {
                c.Options.MaxExecutionDepth = executionDepth;

                c.RegisterServiceProvider(serviceProvider.Object);

                c.RegisterDataLoader <HumanDataLoader>();

                c.RegisterQueryType <QueryType>();
                c.RegisterMutationType <MutationType>();
                c.RegisterSubscriptionType <SubscriptionType>();

                c.RegisterType <HumanType>();
                c.RegisterType <DroidType>();
                c.RegisterType <EpisodeType>();
            }));
        }
Exemple #5
0
        private static Schema CreateSchema()
        {
            CharacterRepository       repository = new CharacterRepository();
            Dictionary <Type, object> services   = new Dictionary <Type, object>();

            services[typeof(CharacterRepository)] = repository;
            services[typeof(Query)]    = new Query(repository);
            services[typeof(Mutation)] = new Mutation();


            Func <Type, object> serviceResolver = new Func <Type, object>(
                t =>
            {
                if (services.TryGetValue(t, out object s))
                {
                    return(s);
                }
                return(null);
            });

            Mock <IServiceProvider> serviceProvider =
                new Mock <IServiceProvider>(MockBehavior.Strict);

            serviceProvider.Setup(t => t.GetService(It.IsAny <Type>()))
            .Returns(serviceResolver);

            return(Schema.Create(c =>
            {
                c.RegisterServiceProvider(serviceProvider.Object);
                c.RegisterDataLoader <HumanDataLoader>();
                c.RegisterQueryType <QueryType>();
                c.RegisterMutationType <MutationType>();
                c.RegisterType <HumanType>();
                c.RegisterType <DroidType>();
                c.RegisterType <EpisodeType>();
            }));
        }
Exemple #6
0
 public HumanDataLoader(CharacterRepository repository)
     : base(new DataLoaderOptions <string>())
 {
     _repository = repository
                   ?? throw new ArgumentNullException(nameof(repository));
 }
Exemple #7
0
 public Query(CharacterRepository repository)
 {
     _repository = repository
                   ?? throw new System.ArgumentNullException(nameof(repository));
 }