Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions db.mjs
Original file line number Diff line number Diff line change
@@ -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 }
31 changes: 31 additions & 0 deletions schema.mjs
Original file line number Diff line number Diff line change
@@ -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]
}
`;
57 changes: 57 additions & 0 deletions server.mjs
Original file line number Diff line number Diff line change
@@ -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}`);