generated from apollographql/typescript-repo-template
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: initial package implementation
- Loading branch information
1 parent
900eb66
commit 29aeedc
Showing
1 changed file
with
66 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,67 @@ | ||
export function helloWorld() { | ||
return 'Hello World!'; | ||
import { | ||
ApolloServer, | ||
BaseContext, | ||
ContextFunction, | ||
HeaderMap, | ||
} from '@apollo/server'; | ||
import { parse } from 'url'; | ||
import { http, type HttpFunction } from '@google-cloud/functions-framework'; | ||
import type { WithRequired } from '@apollo/utils.withrequired'; | ||
|
||
interface Options<Context extends BaseContext> { | ||
context?: ContextFunction<Parameters<HttpFunction>, Context>; | ||
functionName: string; | ||
} | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const defaultContext: ContextFunction<[], any> = async () => ({}); | ||
|
||
export function startServerAndCreateGoogleCloudFunctionsHandler( | ||
server: ApolloServer<BaseContext>, | ||
options: Options<BaseContext>, | ||
): any; | ||
export function startServerAndCreateGoogleCloudFunctionsHandler< | ||
Context extends BaseContext, | ||
>( | ||
server: ApolloServer<Context>, | ||
options: WithRequired<Options<Context>, 'context'>, | ||
): any; | ||
export function startServerAndCreateGoogleCloudFunctionsHandler< | ||
Context extends BaseContext, | ||
>(server: ApolloServer<Context>, options: Options<Context>) { | ||
server.startInBackgroundHandlingStartupErrorsByLoggingAndFailingAllRequests(); | ||
|
||
const contextFunction = options?.context || defaultContext; | ||
const handler = http(options.functionName, async (req, res) => { | ||
const headers = new HeaderMap(); | ||
|
||
for (const [key, value] of Object.entries(req.headers)) { | ||
if (typeof value === 'string') { | ||
headers.set(key, value); | ||
} | ||
} | ||
|
||
const httpGraphQLResponse = await server.executeHTTPGraphQLRequest({ | ||
context: () => contextFunction(req, res), | ||
httpGraphQLRequest: { | ||
body: req.body, | ||
headers, | ||
method: req.method || 'POST', | ||
search: req.url ? parse(req.url).search || '' : '', | ||
}, | ||
}); | ||
|
||
res.statusCode = httpGraphQLResponse.status || 200; | ||
|
||
if (httpGraphQLResponse.body.kind === 'complete') { | ||
res.send(httpGraphQLResponse.body.string); | ||
} else { | ||
for await (const chunk of httpGraphQLResponse.body.asyncIterator) { | ||
res.write(chunk); | ||
} | ||
res.end(); | ||
} | ||
}); | ||
|
||
return handler; | ||
} |