https://github.com/yandex-cloud-examples/yc-serverless-graphql-subscriptions
How to implement a basic GraphQL subscriptions using Yandex Cloud Serverless ecosystem.
https://github.com/yandex-cloud-examples/yc-serverless-graphql-subscriptions
api-gateway graphql serverless serverless-function yandex-cloud yandexcloud ydb
Last synced: 24 days ago
JSON representation
How to implement a basic GraphQL subscriptions using Yandex Cloud Serverless ecosystem.
- Host: GitHub
- URL: https://github.com/yandex-cloud-examples/yc-serverless-graphql-subscriptions
- Owner: yandex-cloud-examples
- License: apache-2.0
- Created: 2024-03-13T20:14:22.000Z (almost 2 years ago)
- Default Branch: main
- Last Pushed: 2025-06-11T11:09:45.000Z (8 months ago)
- Last Synced: 2025-06-11T12:25:10.539Z (8 months ago)
- Topics: api-gateway, graphql, serverless, serverless-function, yandex-cloud, yandexcloud, ydb
- Language: TypeScript
- Homepage:
- Size: 303 KB
- Stars: 1
- Watchers: 6
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Yandex Cloud Serverless GraphQL Subscriptions
Example on how to implement a basic GraphQL subscriptions using Yandex Cloud Serverless ecosystem
# Setup
Resources required:
- YDB
- 3 Cloud functions
- Api Gateway
## Infrastructure
As of now WebSocket and YDB features are not supported by YC's `terraform` or `serverless`, so we are to create resources and execute migrations manually.
- Create a folder
- Create a service-account. For simplicity give this account admin role to the folder. In production the role should be more granular
- [Create](https://cloud.yandex.ru/docs/functions/operations/function/function-create) `graphql-websocket-connect` function
- [Create](https://cloud.yandex.ru/docs/functions/operations/function/function-create) `graphql-websocket-message` function
- [Create](https://cloud.yandex.ru/docs/functions/operations/function/function-create) `graphql-websocket-disconnect` function
- [Create](https://cloud.yandex.ru/docs/ydb/operations/manage-database#create-db) a YDB database in a serverless mode
- In the database console execute SQL queries from [/src/migrations/](/src/migrations/) consequently
- [Create](https://cloud.yandex.ru/docs/api-gateway/operations/api-gw-create) Api Gateway with config similar to this:
```yaml
openapi: 3.0.0
info:
title: Sample API
version: 1.0.0
paths:
/:
x-yc-apigateway-websocket-connect:
x-yc-apigateway-integration:
type: cloud_functions
function_id:
tag: $latest
service_account_id:
x-yc-apigateway-websocket-message:
x-yc-apigateway-integration:
type: cloud_functions
function_id:
tag: $latest
service_account_id:
x-yc-apigateway-websocket-disconnect:
x-yc-apigateway-integration:
type: cloud_functions
function_id:
tag: $latest
service_account_id:
```
Note that function_ids and service_account_id should be replaced with corresponding data.
As long as you keep the ids you can specify any name for the function. In production this should be configured with terraform and CI scripts
## Development
- Install packages using `npm ci`
- Create service account with at least functions.editor role
- Create and save [authorized key](https://cloud.yandex.ru/docs/iam/operations/authorized-key/create) for this account
- Create and fill `.env` file in the repository root accordingly to [example.env](/example.env)
- Execute `npm dev`
This is gonna upload the functions to the cloud and start watch mode for development
## Testing
https://studio.apollographql.com/sandbox/explorer can be used for GraphQL query\subscription testing after deployment
# Code generation and tools
This project uses several tools some of which might be considered unorthodox:
### graphql-codegen
Production solution to generate typescript and resolver definitions from GraphQL Schema
### esbuild
One of the fastest Typescript to Javascript compilers
### ydb-codegen
WIP project to simplify working with Yandex Database
### serverless-esbuild
Plugin for esbuild which makes development process faster