const { ApolloServer, gql } = require('apollo-server');
const { readFileSync, writeFileSync } = require('fs');
const { join } = require('path');
// The GraphQL schema
const typeDefs = gql`
type Query {
"A simple type for getting started!"
hello: String
books: [Book]
book(bookId: Int): Book
}
type Mutation {
addBook(title: String, message: String, author: String, url: String): Book
editBook(
bookId: Int
title: String
message: String
author: String
url: String
): Book
deleteBook(bookId: Int): Book
}
type Book {
bookId: Int
title: String
message: String
author: String
url: String
}
`;
// A map of functions which return data for the schema.
const resolvers = {
Query: {
hello: () => 'Hello World',
books: () => {
return JSON.parse(readFileSync(join(__dirname, 'books.json')).toString());
},
book: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, 'books.json')).toString()
);
return books.find((book) => book.bookId === args.bookId);
},
},
Mutation: {
addBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, 'books.json')).toString()
);
const maxId = Math.max(...books.map((book) => book.bookId));
const newBook = { bookId: maxId + 1, ...args };
writeFileSync(
join(__dirname, 'books.json'),
JSON.stringify([...books, newBook])
);
return newBook;
},
editBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, 'books.json')).toString()
);
const newBooks = books.map((book) => {
if (book.bookId === args.bookId) {
return args;
} else {
return book;
}
});
writeFileSync(join(__dirname, 'books.json'), JSON.stringify(newBooks));
return args;
},
deleteBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, 'books.json')).toString()
);
const deletedBook = books.find((book) => book.bookId === args.bookId);
const newBooks = books
.filter((book) => book.bookId !== args.bookId)
.map((book, index) => {
book.bookId = index;
return { ...book };
});
writeFileSync(join(__dirname, 'books.json'), JSON.stringify(newBooks));
return deletedBook;
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true,
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
const typeDefs = gql`
type Query {
hello: String
books:[Book]
// bookId를 받아오는 쿼리
book(bookId : Int) : Book
},
type Book {
bookId: Int,
title: String,
message: String,
author: String,
url: String
}
`;
const resolvers = {
Query: {
hello: () => 'Hello World',
books: () => {
return JSON.parse(
readFileSync(join(__dirname, 'books.json')).toString()
);
},
book: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, 'books.json')).toString()
);
return books.find((book) => book.bookId === args.bookId);
},
},
};
const typeDefs = gql`
type Mutation {
addBook(title: String, message: String, author: String, url: String): Book
}
`;
const resolvers = {
Mutation: {
addBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, 'books.json')).toString()
);
const maxId = Math.max(...books.map((book) => book.bookId));
const newBook = { bookId: maxId + 1, ...args };
writeFileSync(
join(__dirname, 'books.json'),
JSON.stringify([...books, newBook])
);
return newBook;
},
},
};
const typeDefs = gql`
type Mutation {
deleteBook(bookId: Int): Book
}
`;
const resolvers = {
Mutation: {
deleteBook: (parent, args, context, info) => {
const books = JSON.parse(
readFileSync(join(__dirname, 'books.json')).toString()
);
// 객체 배열 books를 find 함수를 이용해 받아온 bookId와 같은 데이터를 찾아 deletedBook에 할당
const deletedBook = books.find((book) => book.bookId === args.bookId);
// 객체 배열 books를 filter 함수를 이용해 받아온 bookId와 같은 데이터를 걸러 newBooks에 할당
// 할당된 데이터들의 bookId를 0부터 순서대로 재할당
const newBooks = books
.filter((book) => book.bookId !== args.bookId)
.map((book, index) => {
book.bookId = index;
return { ...book };
});
writeFileSync(join(__dirname, 'books.json'), JSON.stringify(newBooks));
// 삭제한 데이터를 반환
return deletedBook;
},
},
};