const { ApolloServer, gql } = require("apollo-server");
const { readFileSync } = require("fs");
const { join } = require("path");
// The GraphQL schema
const typeDefs = gql`
type Book {
title: String
author: String
url: String
}
type Query {
books: [Book]
}
`;
// A map of functions which return data for the schema.
const resolvers = {
Query: {
// books: () => books,
books: () => {
return JSON.parse(readFileSync(join(__dirname, "books.json")).toString());
},
},
};
const server = new ApolloServer({
typeDefs,
resolvers,
playground: true,
});
server.listen().then(({ url }) => {
console.log(`🚀 Server ready at ${url}`);
});
// books.json
[{ "title": "malza' coding", "author": "malza", "url": "<http://malza.com>" }]