public override Task <object> ResolveAsync( IResolveFieldContext <object> context, DataAccessUserContext userContext ) { return(base.ResolveCrudAsync(this.Config.ReadPolicy, this._crud, context, userContext)); }
public ContactRootMutation( GraphQLUserContextAccessor accessor, IContactManager contacts, INoteManager notes, IUnitOfWork unitOfWork ) { this._context = accessor.Context; this._contacts = contacts; this._notes = notes; this._unitOfWork = unitOfWork; this.AddNoteMutations(); }
public SpeciesRootMutation( GraphQLUserContextAccessor accessor, ISpeciesManager species, INoteManager notes, IUnitOfWork unitOfWork ) { this._context = accessor.Context; this._species = species; this._notes = notes; this._unitOfWork = unitOfWork; this.AddNoteMutations(); }
public LocationRootMutation( GraphQLUserContextAccessor accessor, ILocationManager locations, INoteManager notes, IUnitOfWork unitOfWork ) { this._context = accessor.Context; this._locations = locations; this._notes = notes; this._unitOfWork = unitOfWork; this.AddNoteMutations(); }
public BreedRootMutation( GraphQLUserContextAccessor accessor, IBreedManager breeds, INoteManager notes, IUnitOfWork unitOfWork ) { this._context = accessor.Context; this._breeds = breeds; this._notes = notes; this._unitOfWork = unitOfWork; this.AddNoteMutations(); }
public override Task <IEnumerable <TResolveEntity> > ResolvePageAsync( DataAccessUserContext userContext, int first, int after, string order ) { // The dynamic ordering is a bit too annoying to express with Managers, so we're accessing .Query var query = this._crud.IncludeAll(this._crud.Query()); query = this.OrderPageQuery(query, order); return(Task.FromResult( query.Skip(after) .Take(first) .AsEnumerable() )); }
public static void DefineConnectionAsync <TMyT, TGraphType, TSourceType>( this ObjectGraphType <TMyT> obj, string name, DataAccessUserContext userContext, ConnectionNodeResolver <TSourceType> nodeResolve ) where TGraphType : IGraphType { obj.Connection <TGraphType>() .Name(name) .Argument <IntGraphType>("first", "Only get the first n elements") .Argument <IntGraphType>("after", "Skip elements up to the 'after' element") .Argument <StringGraphType>("order", "Specifies how to order the data. This is type-specific, but you can generally just type the name of a field.") .ResolveAsync(async ctx => { var first = ctx.GetArgument <int>("first"); var after = ctx.GetArgument <int>("after"); var order = ctx.GetArgument <string>("order"); if (first >= PagingConstants.ItemsPerPage || first == 0) { first = PagingConstants.ItemsPerPage; } var values = await nodeResolve(userContext, first, after, order); return(new Connection <TSourceType> { TotalCount = values.Count(), PageInfo = new PageInfo { EndCursor = Convert.ToString(after + values.Count()), StartCursor = Convert.ToString(after), HasNextPage = values.Count() == PagingConstants.ItemsPerPage, HasPreviousPage = after > 0 }, Edges = values.Select(c => new Edge <TSourceType> { Cursor = "TODO", Node = c }).ToList() }); }); }
/// <summary> /// This is a helper method for resolvers to resolve an entity via an <see cref="ICrudAsync"/> /// </summary> /// <param name="context">The GraphQL context. Used to access parameters.</param> /// <param name="crud">The <see cref="ICrudAsync{EntityT}"/> used to resolve the entity.</param> /// <param name="idParamName">The name of the GraphQL parameter containing the entity's ID. Defaults to 'id'.</param> /// <param name="policy">The ASP Core policy that the user must pass in order to access this function.</param> /// <param name="userContext">Used to gain access to the user performing this action.</param> protected async Task <object> ResolveCrudAsync( string policy, ICrudAsync <TResolveEntity> crud, IResolveFieldContext <object> context, DataAccessUserContext userContext, string idParamName = "id" ) { await userContext.EnforceHasPolicyAsync(policy); // ARGS var id = context.GetArgument <int>(idParamName); // Find the entity var entity = await crud.GetByIdAsync(id); if (!entity.Succeeded) { throw new ExecutionError(entity.GatherErrorMessages().Aggregate((a, b) => $"{a}\n{b}")); } return(entity.Value); }
public abstract Task <IEnumerable <TResolveEntity> > ResolvePageAsync( DataAccessUserContext userContext, int first, int after, string order );
// This has to return Task<object> due to GraphQL.NET wanting it. public abstract Task <object> ResolveAsync( IResolveFieldContext <object> context, DataAccessUserContext userContext );
public static void AddNoteMutationsAsync <TSourceType>( this ObjectGraphType <TSourceType> mutator, DataAccessUserContext context, IUnitOfWork unitOfWork, INoteManager notes, string writeNotesPerm, GetNoteOwnerFunc <TSourceType> getNoteOwner ) where TSourceType : class { mutator.FieldAsync <BooleanGraphType>( "addNote", "Adds a note.", new QueryArguments( // category: String! new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "category" }, // content: String! new QueryArgument <NonNullGraphType <StringGraphType> > { Name = "content" } ), async ctx => { await context.EnforceHasPolicyAsync(writeNotesPerm); using (var scope = unitOfWork.Begin("Add note")) { var owner = getNoteOwner(ctx); var note = new NoteEntry { NoteOwner = owner, Category = ctx.GetArgument <string>("category"), Content = ctx.GetArgument <string>("content") }; await notes.CreateAsync(note); scope.Commit(); } return(true); } ); mutator.FieldAsync <BooleanGraphType>( "deleteNotes", "Deletes a set of notes.", new QueryArguments( // ids: [ID!]! new QueryArgument <NonNullGraphType <ListGraphType <NonNullGraphType <IdGraphType> > > > { Name = "ids" } ), async ctx => { await context.EnforceHasPolicyAsync(writeNotesPerm); using (var scope = unitOfWork.Begin("Delete notes")) { var owner = getNoteOwner(ctx); foreach (var id in ctx.GetArgument <List <int> >("ids")) { var note = await notes.GetByIdAsync(id); if (!note.Succeeded) { scope.Rollback("Note was not found"); return(false); } if (!owner.NoteEntries.Any(e => e.NoteEntryId == note.Value.NoteEntryId)) { scope.Rollback("Note does not belong to contact"); return(false); } notes.Delete(note.Value); } scope.Commit(); } return(true); } ); }