Skip to content

Commit

Permalink
Merge pull request #51 from kimyvgy/next
Browse files Browse the repository at this point in the history
feat!: upgrade to @as-integrations/cloudflare-workers:^1.0.0
  • Loading branch information
kimyvgy authored Mar 29, 2024
2 parents d6dc18d + 24dd515 commit a83edbf
Show file tree
Hide file tree
Showing 26 changed files with 435 additions and 4,631 deletions.
11 changes: 9 additions & 2 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
# http://editorconfig.org
root = true

[*]
charset = utf-8
indent_size = 2
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
5 changes: 2 additions & 3 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,9 @@ jobs:
path: node_modules
key: ${{ runner.os }}-build-${{ env.cache-name }}
- name: 📥 Download deps
run: yarn install --frozen-lockfile
- name: 🧶 Publish worker to Cloudflare
run: npm install
- name: 🧶 Deploy
uses: cloudflare/wrangler-action@v3.4.1
with:
apiToken: ${{ secrets.CF_API_TOKEN }}
accountId: ${{ secrets.CF_ACCOUNT_ID }}
command: publish --env production
17 changes: 7 additions & 10 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
.vscode
.yalc
yalc.lock
.wrangler
.DS_Store
/node_modules
*-lock.*
*.lock
*.log

build
src/@types/schema.generated.ts
node_modules
.env

# yarn.lock
yarn-error.log
package-lock.json
/dist
19 changes: 19 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"semi": true,
"printWidth": 100,
"singleQuote": true,
"bracketSpacing": true,
"insertPragma": false,
"requirePragma": false,
"jsxSingleQuote": false,
"bracketSameLine": false,
"embeddedLanguageFormatting": "auto",
"htmlWhitespaceSensitivity": "css",
"vueIndentScriptAndStyle": true,
"quoteProps": "consistent",
"proseWrap": "preserve",
"trailingComma": "es5",
"arrowParens": "avoid",
"useTabs": false,
"tabWidth": 2
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2022 Nguyen Huu Kim
Copyright (c) 2024 Nguyen Huu Kim

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
118 changes: 18 additions & 100 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,119 +1,37 @@
# 👷 `worker-apollo-server-template` Quick start

A template for kick starting a Cloudflare worker project to deploy Apollo Server v4 to Cloudflare Workers.
A template for kick starting a Cloudflare Workers project to deploy Apollo Server v4.

[`index.ts`](blob/main/src/index.ts) is the content of the Workers script.

- [`handlers/apollo.ts`](blob/main/src/handlers/apollo.ts): Route handler for GraphQL query request
- Cloudflare Workers - ESModule syntax + TypeScript
- [@as-integrations/cloudflare-workers](https://www.npmjs.com/package/@as-integrations/cloudflare-workers)
- Enable Playground / Sandbox by adding `plugins` + `introspection`:

```javascript
const server = new ApolloServer<ContextValue>({
typeDefs,
resolvers,
introspection: true,
plugins: [
ApolloServerPluginLandingPageLocalDefault({ footer: false }),
// ApolloServerPluginLandingPageProductionDefault({
// graphRef: 'my-graph-id@my-graph-variant',
// footer: false,
// })
],
});
```
- [@apollo/datasource-rest](https://www.npmjs.com/package/@apollo/datasource-rest) integrated
- [GraphQL Codegen](https://the-guild.dev/graphql/codegen) integrated

## Example
Live demo: https://worker-apollo-server.ds101.workers.dev

Live: https://worker-apollo-server.ds101.workers.dev

```graphql
query {
example
pokemon(id: 1) {
id
name
}
}
```
## Usage

Response:
- Click on the button `Use this template`
- Install npm dependencies

```json
{
"data": {
"example": "Hello world!",
"pokemon": {
"id": "1",
"name": "bulbasaur"
}
}
}
```bash
npm install
```

## Development

Click [Use this template](https://github.com/kimyvgy/worker-apollo-server/generate) button to create your own repository.

Start your dev server with following commands:
- Start app in the dev mode

```sh
yarn install
yarn dev
```bash
npm run dev
```

Open up [http://0.0.0.0:8787](http://0.0.0.0:8787) and you should be ready to go!

> This template uses `graphql-codegen` to auto-generate Typescript types from `schema.graphql`. The types are generated to file `src/@types/schema.generated.ts`. So you can import Resolver Types from there.
- If you edit the `src/schema.ts`, you must re-generate the schema type definitions:

## Deployment

If you don't already have an account, then [create a cloudflare account here](https://dash.cloudflare.com/sign-up) and after verifying your email address with Cloudflare, go to your dashboard and set up your free custom Cloudflare Workers subdomain.

Please change the environment variables for Cloudflare Workers in `wrangler.toml` file:
```toml
# Example
[env.production]
name = "worker-apollo-server"
[[env.production.kv_namespaces]]
binding = "GRAPHQL_CACHE"
id = "ba46c41d96544abba4aae3600c4955eb"
[env.production.vars]
GRAPHQL_BASE_ENDPOINT = "/"
GRAPHQL_KV_CACHE = "true"
```bash
npm run generate
```

Once that's done, you should be able to deploy your app:

```sh
yarn deploy --env production
```
- Run tests:

```bash
yarn run v1.22.19
$ wrangler publish
⛅️ wrangler 2.1.9
-------------------
Running custom build: npm run generate && npm run build

> generate
> graphql-codegen --config codegen.yml

✔ Parse Configuration
✔ Generate outputs

> build
> NODE_ENV=production node worker.build.js

Your worker has access to the following bindings:
- KV Namespaces:
- GRAPHQL_CACHE: dbdc624f2c684f1bb88fa38ab249a13e
- Vars:
- GRAPHQL_BASE_ENDPOINT: "/"
- GRAPHQL_KV_CACHE: "true"
Total Upload: 1520.16 KiB / gzip: 285.68 KiB
Uploaded worker-apollo-server (2.71 sec)
Published worker-apollo-server (0.28 sec)
https://worker-apollo-server.ds101.workers.dev
Done in 7.29s.
npm run test
```
10 changes: 0 additions & 10 deletions bindings.d.ts

This file was deleted.

19 changes: 19 additions & 0 deletions codegen.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { CodegenConfig } from '@graphql-codegen/cli';
import schema from './src/schema';

const config: CodegenConfig = {
schema,
generates: {
'src/@types/apollo-server.d.ts': {
plugins: ['typescript', 'typescript-resolvers'],
config: {
enumsAsConst: true,
namingConvention: {
enumValues: "keep"
}
}
}
}
}

export default config;
16 changes: 0 additions & 16 deletions codegen.yml

This file was deleted.

44 changes: 19 additions & 25 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
{
"name": "worker-apollo-server",
"main": "./build/index.js",
"name": "template-worker-typescript",
"version": "1.0.0",
"private": true,
"scripts": {
"generate": "graphql-codegen --config codegen.yml",
"build": "NODE_ENV=production node worker.build.js",
"dev": "miniflare --watch --debug --host 0.0.0.0 --kv GRAPHQL_CACHE",
"deploy": "wrangler publish",
"deploy-prod": "wrangler publish --env production"
"deploy": "wrangler deploy src/index.ts",
"dev": "wrangler dev",
"start": "wrangler dev",
"test": "vitest run",
"generate": "graphql-codegen"
},
"devDependencies": {
"@cloudflare/workers-types": "^4.20221111.1",
"@esbuild-plugins/node-globals-polyfill": "^0.2.0",
"@esbuild-plugins/node-modules-polyfill": "^0.2.0",
"@graphql-codegen/cli": "^5.0.0",
"@graphql-codegen/typescript": "^4.0.0",
"@graphql-codegen/typescript-resolvers": "^4.0.0",
"@graphql-tools/optimize": "^2.0.0",
"@luckycatfactory/esbuild-graphql-loader": "^3.7.0",
"esbuild": "^0.20.0",
"miniflare": "3.20240320.1",
"typescript": "^5.0.0"
},
"engines": {
"node": ">=16"
"@cloudflare/workers-types": "^4.20231016.0",
"@graphql-codegen/cli": "^5.0.2",
"@graphql-codegen/typescript": "^4.0.6",
"@graphql-codegen/typescript-resolvers": "^4.0.6",
"typescript": "^5.0.4",
"vitest": "^1.4.0",
"wrangler": "^3.0.0"
},
"dependencies": {
"@apollo/datasource-rest": "^6.0.0",
"@apollo/server": "^4.3.0",
"@as-integrations/cloudflare-workers": "^0.2.0",
"graphql": "^16.6.0"
"@apollo/datasource-rest": "^6.2.2",
"@apollo/server": "^4.10.2",
"@as-integrations/cloudflare-workers": "^1.0.0-alpha",
"graphql": "^16.8.1"
}
}
3 changes: 1 addition & 2 deletions renovate.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,5 @@
}
],
"automergeType": "branch",
"platformAutomerge": true,
"ignoreTests": true
"platformAutomerge": true
}
Loading

0 comments on commit a83edbf

Please sign in to comment.