-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
53 lines (41 loc) · 930 Bytes
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
const express = require('express')
const { ApolloServer, gql } = require('apollo-server-express')
const { books, authors } = require('./lib/data')
// The GraphQL schema in string form
const typeDefs = gql`
type Query {
books: [Book]
book(id: Int): Book
}
type Book {
title: String!
author: Author!
}
type Author {
name: String!
}
`
// The resolvers
const resolvers = {
Query: {
books: () => {
return Object.values(books)
},
book: (root, { id }) => {
return books[id]
}
},
Book: {
author: (book) => {
return authors[book.author]
}
}
}
// Initialize the app
const app = express()
const server = new ApolloServer({ typeDefs, resolvers })
server.applyMiddleware({ app })
// Start the server
app.listen(process.env.PORT || 3000, () => {
console.log(`Go to http://localhost:3000${server.graphqlPath} to run queries!`)
})