public NotesMutation(NotesRepository notes, AuthorsRepository authors) { FieldAsync <NonNullGraphType <NoteGraphType> >( name: "addNote", description: "Add a new note.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <NoteInputType> >() { Name = "note", Description = "The new note to be added." }), resolve: async ctx => { var userName = ctx.RequireUserName(); var creator = await authors.GetByUserName(userName, ctx.CancellationToken); if (!creator.HasValue) { return(Errors.InvalidUsernameError <object>()); } var newNote = ctx.GetRequiredArgument <NotesRepository.NoteInput>("note"); return(await notes.AddNote(newNote, creator.Value, ctx.CancellationToken)); }); FieldAsync <NoteGraphType>( name: "addKeyword", description: "Add a new keyword to an existing note with the given id.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> >() { Name = "noteId", Description = "The id of the note the keyword should be added to." }, new QueryArgument <NonNullGraphType <KeywordGraphType> >() { Name = "keyword", Description = "The keyword to add to the note." }), resolve: async ctx => { var prefixedId = ctx.GetRequiredArgument <string>("noteId"); var id = IdParser.ParsePrefixedId(prefixedId, NoteGraphType.IdPrefix); var keyword = ctx.GetRequiredArgument <Keyword>("keyword"); var userName = ctx.RequireUserName(); var editor = await authors.GetByUserName(userName, ctx.CancellationToken); if (!editor.HasValue) { return(Errors.InvalidUsernameError <object>()); } var found = await notes.AddKeywordToNote(id, keyword, editor.Value, ctx.CancellationToken); if (!found) { return(Errors.NotFound <object>("Note", prefixedId)); } return(await notes.GetById(id, ctx.CancellationToken)); }); FieldAsync <BooleanGraphType>( name: "deleteNote", description: "Deletes the note with the given id, if present.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> >() { Name = "noteId", Description = "The id of the note that should be deleted." }), resolve: async ctx => { var prefixedId = ctx.GetRequiredArgument <string>("noteId"); var id = IdParser.ParsePrefixedId(prefixedId, NoteGraphType.IdPrefix); var found = await notes.Remove(id, ctx.CancellationToken); if (!found) { return(Errors.NotFound <bool>("Note", prefixedId)); } return(true); }); FieldAsync <NonNullGraphType <AuthorGraphType> >( name: "signup", description: "Create a new author.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <AuthorInputType> >() { Name = "author", Description = "The new author to be added." }), resolve: async ctx => { var newAuthor = ctx.GetRequiredArgument <AuthorsRepository.AuthorInput>("author"); return(await authors.AddAuthor(newAuthor, ctx.CancellationToken)); }); }
public NotesQuery(NotesRepository notes, AuthorsRepository authors) { FieldAsync <NonNullGraphType <ListGraphType <NonNullGraphType <NoteGraphType> > > >( name: "notes", description: "Returns all existing notes.", resolve: async ctx => await notes.GetAll(ctx.CancellationToken)); FieldAsync <NoteGraphType>( name: "noteById", description: "Returns the note identified by the given id, if present.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> >() { Name = "id", Description = "The id of the note you are looking for." }), resolve: async ctx => { var prefixedId = ctx.GetRequiredArgument <string>("id"); var id = IdParser.ParsePrefixedId(prefixedId, NoteGraphType.IdPrefix); return(await notes.GetById(id, ctx.CancellationToken)); }); FieldAsync <NonNullGraphType <ListGraphType <NonNullGraphType <NoteGraphType> > > >( name: "notesByTitle", description: "Returns all notes whose titles start with the given prefix.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <StringGraphType> >() { Name = "title", Description = "A prefix of the title of the notes you are looking for." }), resolve: async ctx => { var titlePrefix = ctx.GetRequiredArgument <string>("title"); return(await notes.GetByTitle(titlePrefix, ctx.CancellationToken)); }); FieldAsync <NonNullGraphType <ListGraphType <NonNullGraphType <NoteGraphType> > > >( name: "notesByAuthor", description: "Returns all notes manipulated by the given author.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> >() { Name = "authorId", Description = "The id of the author whose notes you are looking for." }), resolve: async ctx => { var prefixedId = ctx.GetRequiredArgument <string>("authorId"); var authorId = IdParser.ParsePrefixedId(prefixedId, AuthorGraphType.IdPrefix); var author = await authors.GetById(authorId, ctx.CancellationToken); if (!author.HasValue) { return(Errors.InvalidArgumentException <object>("authorId", "Author could not be found")); } return(notes.GetByAuthor(author.Value, ctx.CancellationToken)); }); FieldAsync <NonNullGraphType <ListGraphType <NonNullGraphType <NoteGraphType> > > >( name: "notesForKeyword", description: "Returns all notes with the given keyword.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <KeywordGraphType> >() { Name = "keyword", Description = "The keyword of the notes your are looking for." }), resolve: async ctx => { var keyword = ctx.GetRequiredArgument <Keyword>("keyword"); return(await notes.GetByKeyword(keyword, ctx.CancellationToken)); }); FieldAsync <NonNullGraphType <ListGraphType <NonNullGraphType <AuthorGraphType> > > >( name: "authors", description: "Returns all existing authors.", resolve: async ctx => await authors.GetAll(ctx.CancellationToken)); FieldAsync <AuthorGraphType>( name: "authorById", description: "Returns the author identified by the given id, if present.", arguments: new QueryArguments( new QueryArgument <NonNullGraphType <IdGraphType> >() { Name = "id", Description = "The id of the author you are looking for." }), resolve: async ctx => { var prefixedId = ctx.GetRequiredArgument <string>("id"); var id = IdParser.ParsePrefixedId(prefixedId, AuthorGraphType.IdPrefix); return(await authors.GetById(id, ctx.CancellationToken)); }); }