public StarWarsQuery(ITrilogyHeroes trilogyHeroes, Core.Data.IDroidRepository droidRepository, Core.Data.IHumanRepository humanRepository, IMapper mapper)
        {
            Name = "Query";

            Field <CharacterInterface>(
                "hero",
                arguments: new QueryArguments(
                    new QueryArgument <EpisodeEnum> {
                Name = "episode", Description = "If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode."
            }
                    ),
                resolve: context =>
            {
                var episode   = context.GetArgument <Episodes?>("episode");
                var character = trilogyHeroes.GetHero((int?)episode).Result;
                var hero      = mapper.Map <Character>(character);
                return(hero);
            }
                );

            Field <HumanType>(
                "human",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "id", Description = "id of the human"
            }
                    ),
                resolve: context =>
            {
                var id    = context.GetArgument <int>("id");
                var human = humanRepository.Get(id).Result;
                if (human != null)
                {
                    Console.Out.WriteLine("Human found: " + human.Id);
                }
                var mapped = mapper.Map <Human>(human);
                return(mapped);
            }
                );
            Field <DroidType>(
                "droid",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "id", Description = "id of the droid"
            }
                    ),
                resolve: context =>
            {
                var id     = context.GetArgument <int>("id");
                var droid  = droidRepository.Get(id).Result;
                var mapped = mapper.Map <Droid>(droid);
                return(mapped);
            }
                );
        }
Exemplo n.º 2
0
        public StarWarsMutation(Core.Data.IHumanRepository humanRepository, IMapper mapper)
        {
            Name        = "Mutation";
            Description = "These are the things we can change";

            Field <HumanType>(
                "CreateOrUpdateHuman",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "id", Description = "id of the human"
            },
                    new QueryArgument <NonNullGraphType <StringGraphType> > {
                Name = "name", Description = "name of the human"
            }
                    ),
                resolve: context =>
            {
                var id   = context.GetArgument <int>("id");
                var name = context.GetArgument <string>("name");

                var human = humanRepository.Get(id).Result;
                if (human == null)
                {
                    human = new StarWars.Core.Models.Human {
                        Id = id, Name = name
                    };
                    humanRepository.Add(human);
                }
                else
                {
                    human.Id   = id;
                    human.Name = name;
                    humanRepository.Update(human);
                }
                humanRepository.SaveChangesAsync();

                var mapped = mapper.Map <Human>(human);
                return(mapped);
            }
                );
        }