public DroidObject(IDroidRepository droidRepository) { this.Name = "Droid"; this.Description = "A mechanical creature in the Star Wars universe."; this.Field(x => x.Id, type: typeof(NonNullGraphType <IdGraphType>)) .Description("The unique identifier of the droid."); this.Field(x => x.Name) .Description("The name of the droid."); this.Field(x => x.ChargePeriod) .Description("The time the droid can go without charging its batteries."); this.Field(x => x.Created) .Description("The date the droid was created."); this.Field(x => x.PrimaryFunction, nullable: true) .Description("The primary function of the droid."); this.Field(x => x.AppearsIn, type: typeof(ListGraphType <EpisodeEnumeration>)) .Description("Which movie they appear in."); this.FieldAsync <ListGraphType <CharacterInterface>, List <Character> >( nameof(Droid.Friends), "The friends of the character, or an empty list if they have none.", resolve: context => droidRepository.GetFriends(context.Source, context.CancellationToken)); this.Interface <CharacterInterface>(); }
public StarWarsQuery(IDroidRepository droidRepository) { Field <ListGraphType <DroidType> >("hero", arguments: new QueryArguments(new QueryArgument <IntGraphType> { Name = "id" }, new QueryArgument <StringGraphType> { Name = "name" }), resolve: context => { var id = context.GetArgument <int?>("id"); if (id.HasValue) { return(Task.FromResult(new List <Droid> { droidRepository.Get(id.Value).Result })); } var name = context.GetArgument <string>("name"); if (!string.IsNullOrEmpty(name)) { return(droidRepository.GetByName(name)); } return(null); }); }
public StarWarsQuery(IDroidRepository _droidRepository) { Field <DroidType>( "hero", resolve: context => _droidRepository.Get(1) ); }
public RootQuery( IDroidRepository droidRepository, IHumanRepository humanRepository) { this.Name = "Query"; this.Description = "The query type, represents all of the entry points into our object graph."; this.FieldAsync <DroidObject, Droid>( "droid", arguments: new QueryArguments( new QueryArgument <IdGraphType> { Name = "id", Description = "The unique identifier of the droid.", }), resolve: context => droidRepository.GetDroid( context.GetArgument("id", defaultValue: new Guid("1ae34c3b-c1a0-4b7b-9375-c5a221d49e68")), context.CancellationToken)); this.FieldAsync <HumanObject, Human>( "human", arguments: new QueryArguments( new QueryArgument <IdGraphType>() { Name = "id", Description = "The unique identifier of the human.", }), resolve: context => humanRepository.GetHuman( context.GetArgument("id", defaultValue: new Guid("94fbd693-2027-4804-bf40-ed427fe76fda")), context.CancellationToken)); }
public QueryResolver( IDroidRepository droidRepository, IHumanRepository humanRepository) { this.droidRepository = droidRepository; this.humanRepository = humanRepository; }
public Query(ITrilogyHeroes trilogyHeroes, IDroidRepository droidRepository, IHumanRepository humanRepository) { _trilogyHeroes = trilogyHeroes; _droidRepository = droidRepository; _humanRepository = humanRepository; }
public DroidQuery(IDataLoaderContextAccessor accessor, IDroidRepository droids) : base(accessor) { Field <DroidType>( "droid", arguments: new QueryArguments( new QueryArgument <IntGraphType> { Name = "id" }, new QueryArgument <StringGraphType> { Name = "name" } ), resolve: context => { var id = context.GetArgument <int>("id"); var name = context.GetArgument <string>("name"); return(Defer("GetDroid", () => id > 0 ? droids.Get(id) : droids.Get(name) )); }); Field <ListGraphType <DroidType> >( "droids", resolve: context => Defer("GetDroids", () => droids.Get()) ); }
public QueryObject( IDroidRepository droidRepository, IHumanRepository humanRepository) { this.Name = "Query"; this.Description = "The query type, represents all of the entry points into our object graph."; this.FieldAsync <DroidObject, Droid>( "droid", "Get a droid by its unique identifier.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> > { Name = "id", Description = "The unique identifier of the droid.", }), resolve: context => droidRepository.GetDroid( context.GetArgument("id", defaultValue: new Guid("1ae34c3b-c1a0-4b7b-9375-c5a221d49e68")), context.CancellationToken)); this.FieldAsync <DroidObject, Droid>( "randomDroid", "Get a random droid from the database.", resolve: context => droidRepository.GetRandomDroid(context.CancellationToken)); this.FieldAsync <HumanObject, Human>( "human", "Get a human by its unique identifier.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> >() { Name = "id", Description = "The unique identifier of the human.", }), resolve: context => humanRepository.GetHuman( context.GetArgument("id", defaultValue: new Guid("94fbd693-2027-4804-bf40-ed427fe76fda")), context.CancellationToken)); this.FieldAsync <HumanObject, Human>( "randomHuman", "Get a random human from the database.", resolve: context => humanRepository.GetRandomHuman(context.CancellationToken)); // Just include static info for info query in this case as it's not hitting the dummy repository/database. this.Field <InfoObject>( "info", resolve: context => { return(new Info { Id = "ed7584-2124-98fs-00s3-t739478t", Name = "maana.io.template", Description = "Dockerized ASP.NET Core GraphQL Template" }); }); }
public StarWarsQuery(ITrilogyHeroesRepository trilogyHeroesRepository, IDroidRepository droidRepository, IHumanRepository humanRepository, IMapper mapper) { Name = "Query"; FieldAsync <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: async context => { var episode = context.GetArgument <Episodes?>("episode"); var character = await trilogyHeroesRepository.GetHero((int?)episode); var hero = mapper.Map <Character>(character); return(hero); } ); FieldAsync <HumanType>( "human", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "id", Description = "id of the human" } ), resolve: async context => { var id = context.GetArgument <int>("id"); var human = await humanRepository.GetAsync(id, "HomePlanet"); var mapped = mapper.Map <Human>(human); return(mapped); } ); FieldAsync <DroidType>( "droid", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "id", Description = "id of the droid" } ), resolve: async context => { var id = context.GetArgument <int>("id"); var droid = await droidRepository.GetAsync(id); var mapped = mapper.Map <Droid>(droid); return(mapped); } ); }
public DroidMutation(IDataLoaderContextAccessor accessor, IDroidRepository droids) { Field <IntGraphType>( "addDroid", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <DroidInputType> > { Name = "droid" } ), resolve: context => droids.Add(context.GetArgument <Droid>("droid")) ); }
public StarWarsQuery(IDroidRepository _droidRepository) { Field <DroidType>( "hero", arguments: new QueryArguments(new QueryArgument <IdGraphType> { Name = "id", Description = "The ID of the droid." }), resolve: context => { var id = context.GetArgument <int>("id"); return(_droidRepository.GetAll().Where(x => x.Id == id).FirstOrDefault()); } ); }
private async static Task <object> ResolveConnection( IDroidRepository droidRepository, ResolveConnectionContext <object> context) { var first = context.First; if (!context.First.HasValue && !context.Last.HasValue) { first = context.PageSize; } var afterCursor = Cursor.FromNullableCursor <DateTime>(context.After); var last = context.Last; var beforeCursor = Cursor.FromNullableCursor <DateTime>(context.Before); var cancellationToken = context.CancellationToken; var getDroidsTask = GetDroids(droidRepository, first, afterCursor, last, beforeCursor, cancellationToken); var getHasNextPageTask = GetHasNextPage(droidRepository, first, afterCursor, cancellationToken); var getHasPreviousPageTask = GetHasPreviousPage(droidRepository, last, beforeCursor, cancellationToken); var totalCountTask = droidRepository.GetTotalCount(cancellationToken); await Task.WhenAll(getDroidsTask, getHasNextPageTask, getHasPreviousPageTask, totalCountTask); var droids = getDroidsTask.Result; var hasNextPage = getHasNextPageTask.Result; var hasPreviousPage = getHasPreviousPageTask.Result; var totalCount = totalCountTask.Result; var(firstCursor, lastCursor) = Cursor.GetFirstAndLastCursor(droids, x => x.Created); return(new Connection <Droid>() { Edges = droids .Select(x => new Edge <Droid>() { Cursor = Cursor.ToCursor(x.Created), Node = x }) .ToList(), PageInfo = new PageInfo() { HasNextPage = hasNextPage, HasPreviousPage = hasPreviousPage, StartCursor = firstCursor, EndCursor = lastCursor, }, TotalCount = totalCount, }); }
private static async Task <bool> GetHasPreviousPage( IDroidRepository droidRepository, int?last, DateTime?beforeCursor, CancellationToken cancellationToken) { if (last.HasValue) { return(await droidRepository.GetHasPreviousPage(last, beforeCursor, cancellationToken)); } else { return(false); } }
private static Task <bool> GetHasNextPageAsync( IDroidRepository droidRepository, int?first, DateTime?afterCursor, CancellationToken cancellationToken) { if (first.HasValue) { return(droidRepository.GetHasNextPageAsync(first, afterCursor, cancellationToken)); } else { return(Task.FromResult(false)); } }
private static Task <bool> GetHasPreviousPageAsync( IDroidRepository droidRepository, int?last, DateTime?beforeCursor, CancellationToken cancellationToken) { if (last.HasValue) { return(droidRepository.GetHasPreviousPageAsync(last, beforeCursor, cancellationToken)); } else { return(Task.FromResult(false)); } }
private static async Task <bool> GetHasNextPage( IDroidRepository droidRepository, int?first, DateTime?afterCursor, CancellationToken cancellationToken) { if (first.HasValue) { return(await droidRepository.GetHasNextPage(first, afterCursor, cancellationToken)); } else { return(false); } }
private static async Task <object> ResolveConnectionAsync( IDroidRepository droidRepository, ResolveConnectionContext <object> context) { var first = context.First; var afterCursor = Cursor.FromCursor <DateTime?>(context.After); var last = context.Last; var beforeCursor = Cursor.FromCursor <DateTime?>(context.Before); var cancellationToken = context.CancellationToken; var getDroidsTask = GetDroidsAsync(droidRepository, first, afterCursor, last, beforeCursor, cancellationToken); var getHasNextPageTask = GetHasNextPageAsync(droidRepository, first, afterCursor, cancellationToken); var getHasPreviousPageTask = GetHasPreviousPageAsync(droidRepository, last, beforeCursor, cancellationToken); var totalCountTask = droidRepository.GetTotalCountAsync(cancellationToken); await Task.WhenAll(getDroidsTask, getHasNextPageTask, getHasPreviousPageTask, totalCountTask).ConfigureAwait(false); var droids = await getDroidsTask.ConfigureAwait(false); var hasNextPage = await getHasNextPageTask.ConfigureAwait(false); var hasPreviousPage = await getHasPreviousPageTask.ConfigureAwait(false); var totalCount = await totalCountTask.ConfigureAwait(false); var(firstCursor, lastCursor) = Cursor.GetFirstAndLastCursor(droids, x => x.Manufactured); return(new Connection <Droid>() { Edges = droids .Select(x => new Edge <Droid>() { Cursor = Cursor.ToCursor(x.Manufactured), Node = x, }) .ToList(), PageInfo = new PageInfo() { HasNextPage = hasNextPage, HasPreviousPage = hasPreviousPage, StartCursor = firstCursor, EndCursor = lastCursor, }, TotalCount = totalCount, }); }
public DroidGType(IDroidRepository droidRepository, IDataLoaderContextAccessor dataLoader) { _droidRepository = droidRepository; Connection <DroidGType>() .Name("droids") .Unidirectional() .PageSize(10) .ResolveAsync(async context => { var loader = dataLoader.Context.GetOrAddCollectionBatchLoader <Guid, Character>("FriendsLoader", _droidRepository.GetFriendsAsync); // IMPORTANT: In order to avoid deadlocking on the loader we use the following construct (next 2 lines): var loadHandle = loader.LoadAsync(context.Source.Id); var result = await loadHandle; return(await result.ToConnection(context)); }); Name = "Droid"; Description = "A mechanical creature in the Star Wars universe."; Field(x => x.Id, type: typeof(IdGraphType)).Description("The unique identifier of the droid."); Field(x => x.Name).Description("The name of the droid."); Field(x => x.PrimaryFunction, nullable: true).Description("The primary function of the droid."); //Authorization on Field scope Field <ListGraphType <EpisodeGType> >(nameof(Droid.AppearsIn), "Which movie they appear in.").AuthorizeWith(Constants.Policies.AdminPolicy); FieldAsync <ListGraphType <CharacterInterface>, List <Character> >( nameof(Droid.Friends), "The friends of the character, or an empty list if they have none.", resolve: context => { // var userContext = context.UserContext as GraphQLUserContext; // var authenticated = userContext.User?.Identity.IsAuthenticated ?? false; // if (userContext!=null && userContext.User.Claims.Any(x => x.Value.Equals("Admin"))) // { // context.ReportError(new ValidationError( //context.OriginalQuery, //"auth-required", //$"Authorization is required to access {op.Name}.", //op)); // } return(droidRepository.GetFriendsAsync(context.Source, context.CancellationToken)); }); Interface <CharacterInterface>(); }
public Mutation(ITrilogyHeroes trilogyHeroes, IDroidRepository droidRepository, IHumanRepository humanRepository) { _trilogyHeroes = trilogyHeroes; _droidRepository = droidRepository; _humanRepository = humanRepository; Name = "Mutation"; Field <DroidType>( Name = "addDroid", arguments: new QueryArguments( new QueryArgument <DroidTypeInput> { Name = "droid", DefaultValue = null }), resolve: AddDroid ); }
public Query(ITrilogyHeroes trilogyHeroes, IDroidRepository droidRepository, IHumanRepository humanRepository) { _trilogyHeroes = trilogyHeroes; _droidRepository = droidRepository; _humanRepository = humanRepository; Name = "Query"; Field <CharacterInterface>( "hero", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <EpisodeEnum> > { Name = "episode", Description = "If omitted, returns the hero of the whole saga. If provided, returns the hero of that particular episode." } ), resolve: GetHero ); Field <HumanType>( "human", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IntGraphType> > { Name = "id", Description = "id of the human" } ), resolve: GetHuman ); Field <DroidType>( "droid", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IntGraphType> > { Name = "id", Description = "id of the droid" } ), resolve: GetDroid); Field <ListGraphType <CharacterInterface> >("heroes", resolve: GetHeroes); Field <ListGraphType <HumanType> >("humans", resolve: GetHumans); Field <ListGraphType <DroidType> >("droids", resolve: GetDroids); }
public StarWarsQueryObject( IDroidRepository droidRepository, IHumanRepository humanRepository) { this.Name = "StarWarsQuery"; this.Description = "The query type, represents all of the entry points into our object graph."; this.FieldAsync <DroidObject, Droid>( "droid", "Get a droid by its unique identifier.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> > { Name = "id", Description = "The unique identifier of the droid.", }), resolve: context => droidRepository.GetDroidAsync( context.GetArgument("id", defaultValue: new Guid("1ae34c3b-c1a0-4b7b-9375-c5a221d49e68")), context.CancellationToken)); this.FieldAsync <HumanObject, Human>( "human", "Get a human by its unique identifier.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> >() { Name = "id", Description = "The unique identifier of the human.", }), resolve: context => humanRepository.GetHumanAsync( context.GetArgument("id", defaultValue: new Guid("94fbd693-2027-4804-bf40-ed427fe76fda")), context.CancellationToken)); this.Connection <DroidObject>() .Name("droids") .Description("Gets pages of droids.") // Enable the last and before arguments to do paging in reverse. .Bidirectional() // Set the maximum size of a page, use .ReturnAll() to set no maximum size. .PageSize(MaxPageSize) .ResolveAsync(context => ResolveConnectionAsync(droidRepository, context)); }
private static Task <List <Droid> > GetDroids( IDroidRepository droidRepository, int?first, DateTime?afterCursor, int?last, DateTime?beforeCursor, CancellationToken cancellationToken) { Task <List <Droid> > getDroidsTask; if (first.HasValue) { getDroidsTask = droidRepository.GetDroids(first, afterCursor, cancellationToken); } else { getDroidsTask = droidRepository.GetDroidsReverse(last, beforeCursor, cancellationToken); } return(getDroidsTask); }
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 DroidResolver(IDroidRepository droidRepository) => this.droidRepository = droidRepository;
public TrilogyHeroes(IEpisodeRepository episodeRepository, IDroidRepository droidRepository) { _episodeRepository = episodeRepository; _droidRepository = droidRepository; }
public DroidDataLoader(IBatchScheduler batchScheduler, IDroidRepository repository) : base(batchScheduler, new DataLoaderOptions <Guid>()) => this.repository = repository;
public DroidsController(IDroidRepository repository) { droidRepo = repository; }
public Task <IQueryable <Droid> > GetDroidsAsync( [Service] IDroidRepository droidRepository, CancellationToken cancellationToken) => droidRepository.GetDroidsAsync(cancellationToken);
public Task <List <Character> > GetFriendsAsync( [Service] IDroidRepository droidRepository, [Parent] Droid droid, CancellationToken cancellationToken) => droidRepository.GetFriendsAsync(droid, cancellationToken);
public RootQuery( IDroidRepository droidRepository, IHumanRepository humanRepository) { Connection <HumanGType>() .Name("humans") .Argument <StringGraphType>("filter", "Filter humans") .Unidirectional() .PageSize(10) .ResolveAsync(async context => { var filter = context.GetArgument <string>("filter"); Console.WriteLine($"filter => {filter}"); if (filter == null) { var result = await humanRepository.GetAll(context.CancellationToken); return(result.ToConnection(context)); } else { var result = await humanRepository.GetAll(context.CancellationToken); return(await result.AsQueryable().Where(filter).ToConnection(context)); } }); Connection <DroidGType>() .Name("droids") .Argument <StringGraphType>("filter", "Filter droids") .Unidirectional() .PageSize(10) .ResolveAsync(async context => { var filter = context.GetArgument <string>("filter"); Console.WriteLine($"filter => {filter}"); if (filter == null) { var result = await droidRepository.GetAllAsync(context.CancellationToken); return(await result.ToConnection(context)); } else { var result = await droidRepository.GetAllAsync(context.CancellationToken); return(await result.AsQueryable().Where(filter).ToConnection(context)); } }); //this.Name = "Query"; //this.Description = "The query type, represents all of the entry points into our object graph."; //this.FieldAsync<DroidGType, Droid>( // "droid", // arguments: new QueryArguments( // new QueryArgument<IdGraphType> // { // Name = "id", // Description = "The unique identifier of the droid.", // }), // resolve: context => // droidRepository.GetAsync( // context.GetArgument("id", defaultValue: new Guid("1ae34c3b-c1a0-4b7b-9375-c5a221d49e68")), // context.CancellationToken)); //this.FieldAsync<HumanGType, Human>( // "human", // arguments: new QueryArguments( // new QueryArgument<IdGraphType>() // { // Name = "id", // Description = "The unique identifier of the human.", // }), // resolve: context => humanRepository.GetAsync( // context.GetArgument("id", defaultValue: new Guid("94fbd693-2027-4804-bf40-ed427fe76fda")), // context.CancellationToken)); //this.FieldAsync<ListGraphType<HumanGType>, List<Human>>( // "humans", // resolve: context => humanRepository.GetHumans(context.CancellationToken)); }