-
-
Notifications
You must be signed in to change notification settings - Fork 33
/
graphql.ts
112 lines (100 loc) · 2.98 KB
/
graphql.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
import { useEngine } from '@envelop/core'
import type { Handle } from '@sveltejs/kit'
import * as GraphQLJS from 'graphql'
import { createSchema, createYoga, type Plugin, type YogaInitialContext } from 'graphql-yoga'
export type KitQLHandleGraphQL<TUserContext, TServerContext extends Record<string, any>> = {
/**
* The path to access your own GraphqiQL.
*
* Note that you will need to add `handleGraphiql` to have it working.
*
* **Default**: `undefined`
*
* ---
* _Note:_ By default graphiQLPath is undefined, so you will get a 404 on a GET request. (Security by default)
*/
graphiQLPath?: string
/**
* The path to your graphql endpoint
*
* **Default**: `/api/graphql`
*/
endpoint?: string
/**
* If you want to use your own schema, you can pass it here.
* let's have a look at the type only after Yoga v3 is out!
*/
schema: any
/**
* THE context.
*/
context?:
| ((initialContext: YogaInitialContext & TServerContext) => Promise<TUserContext> | TUserContext)
| Promise<TUserContext>
| TUserContext
/**
* List of plugins.
*/
plugins?: Plugin[]
}
export function handleGraphql<TUserContext, TServerContext>(
options?: KitQLHandleGraphQL<TUserContext, TServerContext>
): Handle {
// set defaults
const { graphiQLPath, endpoint, plugins, context, schema } = {
graphiQLPath: undefined,
endpoint: '/api/graphql',
plugins: [],
context: () => {
return {} as TUserContext
},
schema: createSchema({
typeDefs: `
type Query {
_greetings: String
}
`,
resolvers: {
Query: {
_greetings: () =>
'Yes yoga is up and running! Now, to make it work with your own schema, you need to `useKitqlModules(modules)` via plugins',
},
},
}),
...options,
}
if (!endpoint.startsWith('/')) {
throw new Error("graphql endpoint must start with '/'")
}
if (graphiQLPath && !graphiQLPath.startsWith('/')) {
throw new Error("graphiQLPath path must start with '/'")
}
// defaults plugins of kitql
const kitqlPlugins = [useEngine(GraphQLJS)]
const kitqlServer = createYoga<YogaInitialContext, TUserContext>({
logging: true,
// will be overwritten by modules
schema,
context,
plugins: kitqlPlugins.concat(plugins || []),
graphqlEndpoint: endpoint,
fetchAPI: globalThis,
})
return async ({ event, resolve }) => {
if (event.url && event.url.pathname === endpoint) {
if (event.request.method === 'GET') {
// If we know graphiQLPath, let's go there
if (graphiQLPath) {
return new Response('Redirect', { status: 303, headers: { Location: graphiQLPath } })
}
// if not, let's bring the 404!
return new Response(`${endpoint} Not found`, { status: 404 })
}
if (event.request.method === 'POST') {
return kitqlServer.handleRequest(event.request, null)
}
}
// Fallback to normal request
return resolve(event)
}
}