This is a GraphQL API for a library that can do queries and mutations using GraphQL Apollo Server on a Librarry database that has the following entities
- Book
- Author
- Genre
A book can have many different authors and many authors can write a book, hence Book and Author are in many-to-many relation. Similiarly, Book and Genre are in many-to-many relation. These relationships are handles via explicit junction tables(join table) inside the database that is set up using Prisma as the ORM. You can locally inspect the database with the npm command one the project is set up and running
The API implements cursor based pagination using the pagination specification from Relay GraphQL specifications
The API is written in Typescript and ES6 modules and other features like uses top-level await. Inspect the tsconfig.json file for more information
npx prisma studio
clone the repository with
git clone https://github.com/OsmosysSoftware/graphql-app
cd graphql-app
In the graphql-app folder create an environment file named .env for setting up local environment with the following variables
- DATABASE_URL="postgres://your-username:your-password@yourhost:DB_PORT/databasename"
- PORT=4000
see example.env for example configuration Make sure postgreSQL is installed and service is started
Make sure are in the graphql-app directory and node and npm are installed, then install the packaes with npm
npm i
This will install all the required packages
We will use the prisma commands to create and seed the postgres database with the following npm commands
npx prisma db push
npx prisma db seed
Once everything is set up the graphQL API will be ready to use. To run type in the terminal
npm run dev
The documentation for the GraphQL API can be found once the server has started. It will launch the server at http://localhost:PORT with the apollo playground page displaying the API documentation
The GraphQL API has the following Types that abstract the underlying Database. (fields with ? are optional fields)
-
getAllBooks (after:ID cursorID before:ID cursorID first:Int n Last:Int n): Returns paginated list of Books with their cursor and pageInfo and takes args to return first n Books after a certain cursorID or last n Books before a certain currsorID
-
getAllAuthors (after:ID cursorID before:ID cursorID first:Int n Last:Int n): Returns paginated list of Authors with their cursor and pageInfo and takes args to return first n Authors after a certain currsorID or last n Authors before a certain currsorID
-
getAllGenres (after:ID cursorID before:ID cursorID first:Int n Last:Int n): Returns paginated list of Genres with their cursor and pageInfo and takes args to return first n Genres after a certain currsorID or last n Genres before a certain currsorID
-
getBook(id:Int id name:String name ): returns the Book with the specified id or name
-
getAuthor(id:Int id name:String name ): returns the Author with the specified id or name
-
getGenre(id:Int id name:String name ): returns the Genre with the specified id or name
-
createBook(book:bookInput! authors:[authorInput!] !genres: [genreInput!]!): creates a new Book with the specified arguments
-
createAuthor(author:authorInput! ): creates a new Author with the specified arguments
-
createGenre(genre:genreInput! ): creates a new Genre with the specified arguments
-
updateBook( id:int name:string book:bookInput! authors:[authorInput!] genres:[genreInput!]): updates the Book having the id or name as provided with the specified arguments
-
updateAuthor(id:int name:string author:authorInput! books:[bookInput!]): updates the Author having the id or name as provided with the specified arguments
-
updateGenre(id:int name:string genre:genreInput! books:[bookInput!]):updates the Genre having the id or name with the specified arguments
-
deleteBook(id:int name:string): deletes the Book having the specidfied id or name
-
deleteAuthor(id:int name:string): deletes the Author having the specidfied id or name
-
deleteGenre (id:int name:string): deletes the Genre having the specified id or name