- Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathwundergraph.server.ts
97 lines (91 loc) · 3.18 KB
/
wundergraph.server.ts
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import{configureWunderGraphServer}from"@wundergraph/sdk";
importtype{HooksConfig}from"./generated/wundergraph.hooks";
importtype{InternalClient}from"./generated/wundergraph.internal.client";
import{GraphQLObjectType,GraphQLSchema,GraphQLString}from"graphql";
import{GraphQLExecutionContext}from"./generated/wundergraph.server";
constsuperAdmins=[
"jens@wundergraph.com",
]
exportdefaultconfigureWunderGraphServer<HooksConfig,
InternalClient>((serverContext)=>({
hooks: {
global: {
httpTransport: {
onRequest: {
hook: async(ctx,request)=>{
console.log(`onRequest hook: ${JSON.stringify(request)}`);
return{
...request,
}
},
enableForOperations: [
"Countries"
]
}
},
},
authentication: {
postAuthentication: async(user)=>{
if(user.email){
try{
awaitserverContext.internalClient.mutations.SetLastLogin({email: user.email});
}catch(e){
console.log(e);
}
}
},
mutatingPostAuthentication: asyncuser=>{
console.log(`mutatingPostAuthentication hook: ${JSON.stringify(user)}`);
if(!user.email){
return{
status: "deny",
message: "No email address provided"
}
}
if(superAdmins.find(s=>s===user.email)!==undefined){
return{
status: "ok",
user: {
...user,
roles: [
"user",
"superadmin"
]
}
}
}
return{
status: "ok",
user: {
...user,
roles: [
"user"
]
}
}
}
},
queries: {},
mutations: {},
},
graphqlServers: [
{
apiNamespace: "gql",
serverName: "gql",
schema: newGraphQLSchema({
query: newGraphQLObjectType<any,GraphQLExecutionContext>({
name: 'RootQueryType',
fields: {
hello: {
type: GraphQLString,
resolve(root,args,ctx){
ctx.log.info(`headers: ${JSON.stringify(ctx.requestContext.clientRequest.headers)}`);
returnctx.requestContext.clientRequest.headers["User-Agent"]||"world";
},
},
},
}),
}),
}
]
}));