-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
45 lines (37 loc) · 1.5 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
const { makeExecutableSchema } = require('@graphql-tools/schema')
const { ApolloServer } = require('apollo-server')
const { envelop, useSchema } = require('@envelop/core')
const { useOperationFieldPermissions } = require('@envelop/operation-field-permissions')
const typeDefs = require('./src/schema')
const resolvers = require('./src/resolvers')
const { getUserPermissions } = require('./src/schema/permissions')
const executableSchema = makeExecutableSchema({ typeDefs, resolvers })
const getEnveloped = envelop({
plugins: [
useSchema(executableSchema),
useOperationFieldPermissions({
getPermissions: getUserPermissions
})
]
})
const apolloServer = new ApolloServer({
schema: executableSchema,
executor: async (requestContext) => {
let isAuthenticated = false
const token = requestContext.request.http.headers.get('Authorization')
if (token === 'example-token') isAuthenticated = true
const { schema, execute, contextFactory } = getEnveloped({
req: requestContext.request.http,
isAuthenticated
})
return execute({
schema,
document: requestContext.document,
contextValue: await contextFactory(),
variableValues: requestContext.request.variables,
operationName: requestContext.operationName
})
},
introspection: true
})
apolloServer.listen({ port: 4000 }).then((serverInfo) => console.log(`Server running on ${serverInfo.url}`))