https://github.com/apollo-server-integrations/apollo-server-integration-cloudflare-workers
An integration to use Cloudflare Workers as a hosting service with Apollo Server
https://github.com/apollo-server-integrations/apollo-server-integration-cloudflare-workers
apollo cloudflare cloudflare-workers graphql hacktoberfest
Last synced: 24 days ago
JSON representation
An integration to use Cloudflare Workers as a hosting service with Apollo Server
- Host: GitHub
- URL: https://github.com/apollo-server-integrations/apollo-server-integration-cloudflare-workers
- Owner: apollo-server-integrations
- License: mit
- Created: 2022-12-30T02:30:11.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2025-03-29T14:45:02.000Z (about 1 month ago)
- Last Synced: 2025-03-30T05:01:46.143Z (about 1 month ago)
- Topics: apollo, cloudflare, cloudflare-workers, graphql, hacktoberfest
- Language: TypeScript
- Homepage: https://github.com/kimyvgy/worker-apollo-server-template
- Size: 1.54 MB
- Stars: 19
- Watchers: 4
- Forks: 1
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# @as-integrations/cloudflare-workers
[](https://www.npmjs.com/package/@as-integrations/cloudflare-workers)
An integration to use Cloudflare Workers as a hosting service with Apollo Server.
## Quickstart
- Read the step-by-step tutorial: https://viblo.asia/p/y37Ldv3y4ov
- Checkout the repository template: https://github.com/kimyvgy/worker-apollo-server-template
- Live demo: https://worker-apollo-server.teguru.workers.dev## Install
```bash
npm add @apollo/server @as-integrations/cloudflare-workers graphql
```## Usage
You must enable Node.js compatibility feature by adding the following flag in the file `wrangler.toml`:
```toml
compatibility_flags = ["nodejs_compat"] # wrangler v4
# node_compat = true # or wrangler v3
``````typescript
import { ApolloServer } from '@apollo/server';
import { startServerAndCreateCloudflareWorkersHandler } from '@as-integrations/cloudflare-workers';
import { ApolloServerPluginLandingPageLocalDefault } from '@apollo/server/plugin/landingPage/default';const typeDefs = `#graphql
type Query {
example: String!
}
`;const resolvers = {
Query: {
example: () => {
return 'Hello universe!';
},
}
}interface Context {
token: string
}const server = new ApolloServer({
typeDefs,
resolvers,
introspection: true,
plugins: [
ApolloServerPluginLandingPageLocalDefault({ footer: false }),
],
});export interface Env {
// ...
}export default {
fetch: startServerAndCreateCloudflareWorkersHandler(server, {
context: async ({ env, request, ctx }) => {
return { token: 'secret' };
},
}),
};
```## Additional Bonus
### Cloudflare Boilerplate
If you’re looking for an alternative boilerplate, Cloudflare offers an official implementation built with [Hono](https://github.com/honojs/hono). You can explore it at [cloudflare/workers-graphql-server](https://github.com/cloudflare/workers-graphql-server).
### Deno Support
This example works with [Deno](https://deno.com) because it follows Web API standards and supports [fetch](https://developer.mozilla.org/en-US/docs/Web/API/Fetch_API). With Deno’s built-in [Deno.serve](https://docs.deno.com/api/deno/~/Deno.serve), you can run it with minimal changes.
**Important Notes:**
- If using Node.js packages, add an import map to `deno.json` or use the `npm:` prefix.
```typescript
# instead of:
import { ApolloServer } from '@apollo/server';# we use:
import { ApolloServer } from 'npm:@apollo/server';
```
- For more control, modify the setup:
```typescript
type Env = {};
type Context = { token: string };const handler = startServerAndCreateCloudflareWorkersHandler(server, {
context: async () => ({ token: 'secret' }),
});Deno.serve({ port: 3000 }, (req) => handler(req, {} satisfies Env, { token: '' } satisfies Context));
```
- Live demo: [Deno Playground](https://dash.deno.com/playground/apollo-integration-example)