-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
49 lines (42 loc) · 1.17 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
const express = require('express')
const { ApolloServer, gql, AuthenticationError, UserInputError } = require('apollo-server-express')
const port = 8000
const typeDefs = gql`
type Query {
helloPublic: String
helloPrivate: String
helloVariable(someNumber: Int!): String
helloBadInput(lessThanFive: Int!): Boolean
}
type Mutation {
helloChangeSomething: Boolean
}
`
const resolvers = {
Query: {
helloPublic: () => 'Hello public',
helloPrivate: (_, __, context) => {
if (context.user) {
return 'Hello private'
}
throw new AuthenticationError('Not authenticated')
},
helloVariable: (_, args) => `Your number was ${args.someNumber}`,
helloBadInput: (_, args) => {
if (args.lessThanFive > 5) {
throw new UserInputError(
`Argument lessThanFive must be less than 5 (received ${args.lessThanFive})`
)
}
return true
},
},
}
const apolloServer = new ApolloServer({
typeDefs,
resolvers,
context: ({ req }) => ({ user: req.header('user') }),
})
const app = express()
apolloServer.applyMiddleware({ app })
app.listen(port, () => console.log(`Listening on port ${port}`))