public GetSpaceshipsQuery(IDroidRepository droidRepository, ISpaceshipRepository spaceshipRepository, IMapper mapper) { // one spaceship Field <SpaceshipType>( "spaceship", arguments: new QueryArguments( new QueryArgument <IdGraphType> { Name = "id", Description = "The id of the spaceship" }), resolve: context => { var id = context.GetArgument <Guid>("id"); var spaceship = mapper.Map <SpaceshipViewModel>(spaceshipRepository.Get(id).Result); if (spaceship == null) { throw new ArgumentException("Wrong id for spaceship."); } spaceship.Commander = mapper.Map <DroidViewModel>(droidRepository.Get(spaceship.CommanderId).Result); return(spaceship); } ); // list of spaceships Field <ListGraphType <SpaceshipType> >( "spaceships", resolve: context => { var spaceships = mapper.Map <List <SpaceshipViewModel> >(spaceshipRepository.Get().Result); foreach (var spaceship in spaceships) { spaceship.Commander = mapper.Map <DroidViewModel>(droidRepository.Get(spaceship.CommanderId).Result); } return(spaceships); }); }
public SpaceshipController(ISpaceshipRepository repository, IMapper mapper) { _spaceshipRepository = repository; _mapper = mapper; }