diff --git a/db.mjs b/db.mjs new file mode 100644 index 0000000..50509db --- /dev/null +++ b/db.mjs @@ -0,0 +1,25 @@ +let games = [ + {id: '1', title: 'Zelda, Tears of the Kingdom', platform: ['Switch']}, + {id: '2', title: 'Final Fantasy 7 Remake', platform: ['PS5', 'Xbox']}, + {id: '3', title: 'Elden Ring', platform: ['PS5', 'Xbox', 'PC']}, + {id: '4', title: 'Mario Kart', platform: ['Switch']}, + {id: '5', title: 'Pokemon Scarlet', platform: ['PS5', 'Xbox', 'PC']}, +] + +let authors = [ + {id: '1', name: 'mario', verified: true}, + {id: '2', name: 'yoshi', verified: false}, + {id: '3', name: 'peach', verified: true}, +] + +let reviews = [ + {id: '1', rating: 9, content: 'lorem ipsum', author_id: '1', game_id: '2'}, + {id: '2', rating: 10, content: 'lorem ipsum', author_id: '2', game_id: '1'}, + {id: '3', rating: 7, content: 'lorem ipsum', author_id: '3', game_id: '3'}, + {id: '4', rating: 5, content: 'lorem ipsum', author_id: '2', game_id: '4'}, + {id: '5', rating: 8, content: 'lorem ipsum', author_id: '2', game_id: '5'}, + {id: '6', rating: 7, content: 'lorem ipsum', author_id: '1', game_id: '2'}, + {id: '7', rating: 10, content: 'lorem ipsum', author_id: '3', game_id: '1'}, +] + +export default { games, authors, reviews } \ No newline at end of file diff --git a/schema.mjs b/schema.mjs new file mode 100644 index 0000000..6094eb1 --- /dev/null +++ b/schema.mjs @@ -0,0 +1,31 @@ +import { ApolloServer,gql } from "apollo-server"; + +export const typeDefs = `#graphql + type Game{ + id:ID! + title:String! + platform:[String!]! + reviews:[Review!] + } + type Review{ + id:ID! + rating:Int! + content:String! + game:Game! + author:Author! + } + type Author{ + id:ID! + name:String! + verified:Boolean! + reviews:[Review!] + } + type Query{ + reviews: [Review] + games: [Game] + review(id:ID!):Review + game(id:ID!):Game + author(id:ID!):Author + authors: [Author] + } +`; \ No newline at end of file diff --git a/server.mjs b/server.mjs new file mode 100644 index 0000000..d4e608f --- /dev/null +++ b/server.mjs @@ -0,0 +1,57 @@ +import { ApolloServer } from "@apollo/server"; +import { startStandaloneServer } from "@apollo/server/standalone"; + +import { typeDefs} from "./schema.mjs"; +import db from "./db.mjs"; + +const resolvers= { + Query: { + games(){ + return db.games + }, + reviews() { + return db.reviews + }, + authors() { + return db.authors + }, + review(_,args){ + return db.reviews.find((review)=>review.id===args.id); + }, + game(_,args){ + return db.games.find((game)=>game.id===args.id); + }, + author(_,args){ + return db.authors.find(author=>author.id===args.id); + } + }, + Game:{ + reviews(parent){ + return db.reviews.filter((r)=>r.game_id===parent.id); + } + }, + Author:{ + reviews(parent){ + return db.reviews.filter((r)=>r.author_id===parent.id); + } + }, + Review:{ + author(parent) { + return db.authors.find((a)=>a.id===parent.author_id); + }, + game(parent){ + return db.games.find((g)=>g.id===parent.game_id) + } + } + +} +const server = new ApolloServer({ + typeDefs, + resolvers +}) + +const { url } = await startStandaloneServer(server,(req, res) => { + listen = { port:4000 }; +}) + +console.log(`server listening on port ${url}`); \ No newline at end of file