public BookType(IBooksDataSource booksDataSource, IAuthorsDataSource authorsDataSource)
        {
            Name = "Book";

            Field(b => b.Id).Description("The id of the book.");
            Field(b => b.Name).Description("The name of the book.");

            Field <AuthorType>(
                "author",
                resolve: context => authorsDataSource.GetAuthor(context.Source.AuthorId)
                );
        }
Beispiel #2
0
        public AuthorType(IBooksDataSource booksDataSource, IAuthorsDataSource authorsDataSource)
        {
            Name = "Author";

            Field(a => a.Id).Description("The id of the author.");
            Field(a => a.Name).Description("The name of the author.");
            Field(a => a.Email).Description("The email of the author.");

            Field <ListGraphType <BookType> >(
                "books",
                resolve: context => booksDataSource.GetBooksByAuthor(context.Source.Id)
                );
        }
Beispiel #3
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));
            });
        }
Beispiel #4
0
        public Query(IBooksDataSource booksDataSource, IAuthorsDataSource authorsDataSource)
        {
            Name = "Query";

            Field <BookType>(
                "book",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id", Description = "id of the book"
            }
                    ),
                resolve: context => booksDataSource.GetBook(context.GetArgument <int>("id"))
                );

            Field <StringGraphType>(
                "bookSource",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id", Description = "id of the book"
            }
                    ),
                resolve: context => JsonConvert.SerializeObject(booksDataSource.GetBook(context.GetArgument <int>("id")))
                );

            Field <ListGraphType <BookType> >(
                "books",
                resolve: context => booksDataSource.GetBooks()
                );

            Field <IntGraphType>(
                "booksCount",
                resolve: context => booksDataSource.GetBooks().Count
                );

            Field <AuthorType>(
                "author",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id", Description = "id of the author"
            }
                    ),
                resolve: context => authorsDataSource.GetAuthor(context.GetArgument <int>("id"))
                );

            Field <StringGraphType>(
                "authorSource",
                arguments: new QueryArguments(
                    new QueryArgument <NonNullGraphType <IdGraphType> > {
                Name = "id", Description = "id of the author"
            }
                    ),
                resolve: context => JsonConvert.SerializeObject(authorsDataSource.GetAuthor(context.GetArgument <int>("id")))
                );

            Field <ListGraphType <AuthorType> >(
                "authors",
                resolve: context => authorsDataSource.GetAuthors()
                );

            Field <IntGraphType>(
                "authorsCount",
                resolve: context => authorsDataSource.GetAuthors().Count
                );
        }
Beispiel #5
0
 public Query(IBooksDataSource booksDataSource, IAuthorsDataSource authorsDataSource, IHttpContextAccessor contextAccessor)
 {
     this.contextAccessor   = contextAccessor;
     this.authorsDataSource = authorsDataSource;
     this.booksDataSource   = booksDataSource;
 }