Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
152 lines (121 loc) · 4.42 KB
/
Copy pathindex.js
File metadata and controls
152 lines (121 loc) · 4.42 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
const crypto = require('crypto');
const {
graphql,
buildSchema,
} = require('graphql');
const playground = require('./handlers/playground');
const setCors = require('./utils/setCors');
const typeDefs = require('./schema');
const resolvers = require('./resolvers');
require('./loader');
const nightbot = require('./custom-endpoints/nightbot');
const schema = buildSchema(typeDefs);
/**
* Example of how router can be used in an application
* */
addEventListener('fetch', event => {
event.respondWith(handleRequest(event.request))
});
async function graphqlHandler(request, graphQLOptions) {
const url = new URL(request.url);
let query = false;
let variables = false;
if(request.method === 'POST'){
try {
const requestBody = await request.json();
query = requestBody.query;
variables = requestBody.variables;
} catch (jsonError){
console.error(jsonError);
return new Response(null, {
status: 503,
});
}
}
if(request.method === 'GET'){
query = url.searchParams.get('query');
variables = url.searchParams.get('variables');
}
const queryHashString = JSON.stringify({
query: query,
variables: variables,
});
const queryHash = crypto.createHash('md5').update(queryHashString).digest('hex');
if(!url.hostname.includes('localhost') && !url.hostname.includes('tutorial.cloudflareworkers.com')){
const cachedResponse = await QUERY_CACHE.get(queryHash, 'json');
if(cachedResponse){
return new Response(JSON.stringify(cachedResponse), {
headers: {
'content-type': 'application/json',
},
});
}
}
await resolvers.itemInit();
const result = await graphql(schema, query, resolvers, {}, variables);
const body = JSON.stringify(result);
if(!result.errors && !url.hostname.includes('localhost') && !url.hostname.includes('tutorial.cloudflareworkers.com')){
await QUERY_CACHE.put(queryHash, body, {expirationTtl: 300});
}
return new Response(body, {
headers: {
'content-type': 'application/json',
},
});
};
const graphQLOptions = {
// Set the path for the GraphQL server
baseEndpoint: '/graphql',
// Set the path for the GraphQL playground
// This option can be removed to disable the playground route
playgroundEndpoint: '/___graphql',
// When a request's path isn't matched, forward it to the origin
forwardUnmatchedRequestsToOrigin: false,
// Enable debug mode to return script errors directly in browser
debug: true,
// Enable CORS headers on GraphQL requests
// Set to `true` for defaults (see `utils/setCors`),
// or pass an object to configure each header
// cors: true,
cors: {
allowCredentials: 'true',
allowHeaders: 'Content-type',
allowOrigin: '*',
allowMethods: 'GET, POST, PUT',
},
// Enable KV caching for external REST data source requests
// Note that you'll need to add a KV namespace called
// WORKERS_GRAPHQL_CACHE in your wrangler.toml file for this to
// work! See the project README for more information.
kvCache: false,
};
const handleRequest = async request => {
const url = new URL(request.url);
try {
if(url.pathname === '/webhook/nightbot'){
return nightbot(request);
}
if(url.pathname === '/webhook/stream-elements'){
return nightbot(request);
}
if(url.pathname === '/webhook/moobot'){
return nightbot(request);
}
if (url.pathname === graphQLOptions.baseEndpoint) {
const response = request.method === 'OPTIONS' ? new Response('', { status: 204 }) : await graphqlHandler(request, graphQLOptions)
if (graphQLOptions.cors) {
setCors(response, graphQLOptions.cors)
}
return response;
}
if ( graphQLOptions.playgroundEndpoint && url.pathname === graphQLOptions.playgroundEndpoint ) {
return playground(request, graphQLOptions)
}
if (graphQLOptions.forwardUnmatchedRequestsToOrigin) {
return fetch(request)
}
return new Response('Not found', { status: 404 })
} catch (err) {
return new Response(graphQLOptions.debug ? err : 'Something went wrong', { status: 500 })
}
};