Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bun server with graphql examples #584

Merged
merged 7 commits into from
Apr 5, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions examples/yoga-graphql-streaming-zip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Yoga Graphql response streaming example

This example show how to use Lambda Web Adapter to run a yoga server application with response streaming via a [AWS Lambda](https://aws.amazon.com/lambda) Function URL.

### Build and Deploy

Run the following commands to build and deploy the application to lambda.
```

```bash
sam build

sam deploy --guided
```
When the deployment completes, the Function URL will appear in the output list, which is the entrypoint for accessing

### Verify it works

When you open the Function URL in a browser:

1. Write and execute GraphQL queries in the left panel
2. See the results in the right panel
3. Explore the API documentation using the "Docs" button

Try this sample subscription query to test streaming:

```graphql
subscription {
stream(addition: "testing")
}
```

You'll see each character stream in one by one with a small delay between them.

For command line testing, you can use curl:

```bash
curl --no-buffer -N \
-H "Content-Type: application/json" \
-H "Accept: text/event-stream" \
-d '{"query": "subscription { stream(addition: \"test\") }"}' \
https://your-function-url.lambda-url.us-east-1.on.aws/graphql
```

### Thanks

@sumcoding
11 changes: 11 additions & 0 deletions examples/yoga-graphql-streaming-zip/app/.eslintrc.cjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module.exports = {
extends: ["eslint:recommended"],
parserOptions: {
ecmaVersion: 2022,
sourceType: "module"
},
env: {
node: true,
es2022: true
}
};
13 changes: 13 additions & 0 deletions examples/yoga-graphql-streaming-zip/app/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
/dist

### IntelliJ IDEA ###
.idea
*.iws
*.iml
*.ipr

### VS Code ###
.vscode/

.aws-sam
node_modules
10 changes: 10 additions & 0 deletions examples/yoga-graphql-streaming-zip/app/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.PHONY: build-GraphqlYogaFunction build

build-GraphqlYogaFunction:
pnpm i
pnpm build
mkdir -p $(ARTIFACTS_DIR)
cp -r dist/* $(ARTIFACTS_DIR)/

build:
$(MAKE) build-GraphqlYogaFunction
49 changes: 49 additions & 0 deletions examples/yoga-graphql-streaming-zip/app/esbuild.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import * as esbuild from 'esbuild';
import { mkdir, writeFile } from 'fs/promises';
import { fileURLToPath } from 'url';
import { dirname, join } from 'path';

const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);

async function build() {
// Create dist directory if it doesn't exist
await mkdir('dist', { recursive: true });

// Build the project using esbuild
const result = await esbuild.build({
entryPoints: ['server.ts'],
bundle: true,
minify: false,
platform: 'node',
target: 'node22',
outfile: 'dist/server.js',
format: 'cjs',
mainFields: ['module', 'main'],
external: [
"node:*"
],
metafile: true,
});

// Create a minimal package.json for the dist folder
const minimalPackageJson = {
"name": "graphql-yoga",
"version": "1.0.0",
"type": "commonjs",
"dependencies": {}
};

await writeFile(
join(__dirname, 'dist', 'package.json'),
JSON.stringify(minimalPackageJson, null, 2)
);

console.log('Build complete!');
console.log(`Bundle size: ${Math.round(result.metafile.outputs['dist/server.js'].bytes / 1024)}KB`);
}

build().catch(err => {
console.error('Build failed:', err);
process.exit(1);
});
27 changes: 27 additions & 0 deletions examples/yoga-graphql-streaming-zip/app/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"name": "graphql-yoga",
"version": "0.1.0",
"private": true,
"type": "module",
"scripts": {
"build": "node esbuild.config.js && cp run.sh dist/",
"dev": "tsc --noEmit && node --loader ts-node/esm ./server.ts",
"lint": "eslint --ignore-path .gitignore --cache --cache-location ./node_modules/.cache/eslint .",
"preview": "node ./dist/server.js",
"typecheck": "tsc"
},
"dependencies": {
"cross-env": "^7.0.3",
"graphql": "^16.10.0",
"graphql-yoga": "^5.13.1"
},
"devDependencies": {
"@types/node": "^22.13.10",
"@typescript-eslint/eslint-plugin": "^8.26.1",
"@typescript-eslint/parser": "^8.26.1",
"esbuild": "^0.25.1",
"eslint": "^9.22.0",
"ts-node": "^10.9.2",
"typescript": "^5.8.2"
}
}
Loading