Esempio n. 1
0
        public Mutation(IBooksDataSource booksDataSource)
        {
            Name = "Mutation";

            Field <BookType>(
                "newBook",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <BookInputType> > {
                Name = "book"
            }
                    ),
                resolve: context =>
            {
                var book = context.GetArgument <Book>("book");
                return(booksDataSource.NewBook(book));
            });

            Field <BookType>(
                "editBook",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id"
            },
                    new QueryArgument <NonNullGraphType <BookInputType> > {
                Name = "book"
            }
                    ),
                resolve: context =>
            {
                var id   = context.GetArgument <int>("id");
                var book = context.GetArgument <Book>("book");
                book.Id  = id;
                return(booksDataSource.EditBook(book));
            });

            Field <BooleanGraphType>(
                "delete",
                arguments: new QueryArguments(
                    new QueryArgument <StringGraphType> {
                Name = "id"
            }
                    ),
                resolve: context =>
            {
                var id = context.GetArgument <string>("id");
                if (int.TryParse(id, out int idValue))
                {
                    return(booksDataSource.DeleteBook(idValue));
                }
                return(false);
            });
        }
Esempio n. 2
0
        public Mutation(IBooksDataSource booksDataSource, IAuthorsDataSource authorsDataSource)
        {
            Name = "Mutation";

            Field <BookType>(
                "newBook",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <BookInputType> > {
                Name = "book"
            }
                    ),
                resolve: context =>
            {
                var book = context.GetArgument <Book>("book");
                return(booksDataSource.NewBook(book));
            });

            Field <BookType>(
                "editBook",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id"
            },
                    new QueryArgument <NonNullGraphType <BookInputType> > {
                Name = "book"
            }
                    ),
                resolve: context =>
            {
                var id   = context.GetArgument <int>("id");
                var book = context.GetArgument <Book>("book");
                book.Id  = id;
                return(booksDataSource.EditBook(book));
            });

            Field <BooleanGraphType>(
                "deleteBook",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id"
            }
                    ),
                resolve: context =>
            {
                var id = context.GetArgument <int>("id");
                return(booksDataSource.DeleteBook(id));
            });

            Field <AuthorType>(
                "newAuthor",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <AuthorInputType> > {
                Name = "author"
            }
                    ),
                resolve: context =>
            {
                var author = context.GetArgument <Author>("author");
                return(authorsDataSource.NewAuthor(author));
            });

            Field <AuthorType>(
                "editAuthor",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id"
            },
                    new QueryArgument <NonNullGraphType <AuthorInputType> > {
                Name = "author"
            }
                    ),
                resolve: context =>
            {
                var id     = context.GetArgument <int>("id");
                var author = context.GetArgument <Author>("author");
                author.Id  = id;
                return(authorsDataSource.EditAuthor(author));
            });

            Field <BooleanGraphType>(
                "deleteAuthor",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id"
            }
                    ),
                resolve: context =>
            {
                var id = context.GetArgument <int>("id");
                return(authorsDataSource.DeleteAuthor(id));
            });
        }