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>(); })); }
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); } } }
public Query(CharacterRepository repository) { _repository = repository ?? throw new System.ArgumentNullException(nameof(repository)); }