Пример #1
0
        public static Schema Create()
        {
            var repository = new CharacterRepository();
            var services   = new Dictionary <Type, object>();

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


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

            var 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.RegisterQueryType <QueryType>();
                c.RegisterMutationType <MutationType>();
                c.RegisterType <HumanType>();
                c.RegisterType <DroidType>();
                c.RegisterType <EpisodeType>();
            }));
        }
Пример #2
0
        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);
                }
            }
        }
Пример #3
0
 public Query(CharacterRepository repository)
 {
     _repository = repository
                   ?? throw new System.ArgumentNullException(nameof(repository));
 }