-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
61 lines (52 loc) · 1.11 KB
/
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
54
55
56
57
58
59
60
61
const express = require('express')
//Include our server libraries
const { VoyagerServer, gql } = require('@aerogear/voyager-server')
//Provide your graphql schema
const typeDefs = gql`
type Query {
info: String!
addressBook: [Person!]!
}
type Mutation {
post(name: String!, address: String!): Person!
}
type Person {
id: ID!
address: String!
name: String!
}
`
let persons = [{
id: 'person-0',
name: 'Alice Roberts',
address: '1 Red Square, Waterford'
}]
let idCount = persons.length
const resolvers = {
Query: {
info: () => `This is a simple example`,
addressBook: () => persons,
},
Mutation: {
post: (parent, args) => {
const person = {
id: `person-${idCount++}`,
address: args.address,
name: args.name,
}
persons.push(person)
return person
}
},
}
//Initialize the library with your GraphQL information
const server = VoyagerServer({
typeDefs,
resolvers
})
//Connect the server to express
const app = express()
server.applyMiddleware({ app })
app.listen(4000, () =>
console.log(`🚀 Server ready at http://localhost:4000/graphql`)
)