Skip to content

Commit

Permalink
feat(graphql-ws): add new @tsed/graphql-ws package to enable subscrip…
Browse files Browse the repository at this point in the history
…tion feature
  • Loading branch information
Romakita committed Nov 1, 2023
1 parent 17db6d6 commit 68ab810
Show file tree
Hide file tree
Showing 10 changed files with 331 additions and 0 deletions.
13 changes: 13 additions & 0 deletions packages/graphql/graphql-ws/.eslintignore
@@ -0,0 +1,13 @@
node_modules
docs
docs-references
lib
dist
coverage
.nyc_output
*-lock.json
*.lock
benchmarks.*
**/generated

**/*.js
1 change: 1 addition & 0 deletions packages/graphql/graphql-ws/.eslintrc.js
@@ -0,0 +1 @@
module.exports = require("@tsed/eslint/node.js");
6 changes: 6 additions & 0 deletions packages/graphql/graphql-ws/.npmignore
@@ -0,0 +1,6 @@
src
test
tsconfig.compile.json
tsconfig.json
__mock__
*.spec.js
43 changes: 43 additions & 0 deletions packages/graphql/graphql-ws/package.json
@@ -0,0 +1,43 @@
{
"name": "@tsed/graphql-ws",
"version": "7.41.2",
"description": "An extension to allow GraphQL over WebSockets using graphql-ws protocol",
"source": "./src/index.ts",
"main": "./lib/cjs/index.js",
"module": "./lib/esm/index.js",
"typings": "./lib/types/index.d.ts",
"exports": {
"types": "./lib/types/index.d.ts",
"import": "./lib/esm/index.js",
"require": "./lib/cjs/index.js",
"default": "./lib/esm/index.js"
},
"scripts": {
"build": "yarn barrels && yarn build:ts",
"barrels": "yarn barrelsby --delete -d ./src -e \"\\.spec\\.ts\" -e \"__mock__\" -e \".benchmark.ts\"",
"build:ts": "tsc --build tsconfig.json && tsc --build tsconfig.esm.json",
"lint": "eslint '**/*.{ts,js}'",
"lint:fix": "eslint '**/*.{ts,js}' --fix"
},
"dependencies": {
"tslib": "2.5.0"
},
"private": false,
"devDependencies": {
"@tsed/common": "7.41.2",
"@tsed/core": "7.41.2",
"@tsed/di": "7.41.2",
"@tsed/eslint": "7.39.1",
"@tsed/logger": ">=6.2.2",
"@tsed/typescript": "7.39.1",
"graphql-ws": "^5.14.2",
"eslint": "^8.12.0"
},
"peerDependencies": {
"@tsed/common": "^7.41.2",
"@tsed/core": "^7.41.2",
"@tsed/di": "^7.41.2",
"@tsed/logger": ">=6.2.2",
"graphql-ws": ">=5.14.2"
}
}
112 changes: 112 additions & 0 deletions packages/graphql/graphql-ws/readme.md
@@ -0,0 +1,112 @@
<p style="text-align: center" align="center">
<a href="https://tsed.io" target="_blank"><img src="https://tsed.io/tsed-og.png" width="200" alt="Ts.ED logo"/></a>
</p>

<div align="center">

<h1>GraphQL WS</h1>

[![Build & Release](https://github.com/tsedio/tsed/workflows/Build%20&%20Release/badge.svg)](https://github.com/tsedio/tsed/actions?query=workflow%3A%22Build+%26+Release%22)
[![PR Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg)](https://github.com/tsedio/tsed/blob/master/CONTRIBUTING.md)
[![Coverage Status](https://coveralls.io/repos/github/tsedio/tsed/badge.svg?branch=production)](https://coveralls.io/github/tsedio/tsed?branch=production)
[![npm version](https://badge.fury.io/js/%40tsed%2Fcommon.svg)](https://badge.fury.io/js/%40tsed%2Fcommon)
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
[![github](https://img.shields.io/static/v1?label=Github%20sponsor&message=%E2%9D%A4&logo=GitHub&color=%23fe8e86)](https://github.com/sponsors/romakita)
[![opencollective](https://img.shields.io/static/v1?label=OpenCollective%20sponsor&message=%E2%9D%A4&logo=OpenCollective&color=%23fe8e86)](https://opencollective.com/tsed)

</div>

<div align="center">
<a href="https://tsed.io/">Website</a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://tsed.io/tutorials/graphql.html">TypeGraphQL</a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://api.tsed.io/rest/slack/tsedio/tsed">Slack</a>
<span>&nbsp;&nbsp;&nbsp;&nbsp;</span>
<a href="https://twitter.com/TsED_io">Twitter</a>
</div>

<hr />

## Requirement

You need to install `@tsed/apollo` before using this module.

## Installation

To begin, install the GraphQL WS module for TS.ED:

```bash
npm install --save @tsed/graphql-ws graphql-ws
```

Now, we can configure the Ts.ED server by importing `@tsed/gr` in your Server:

```typescript
import {Configuration} from "@tsed/common";
import "@tsed/apollo";

@Configuration({
graphql: {
server1: {
path: "/",
schema: {},
resolvers: []
}
}
})
export class Server {}
```

## ApolloService

ApolloService let you to retrieve an instance of ApolloServer.

```typescript
import {Injectable, AfterRoutesInit} from "@tsed/common";
import {graphQLService} from "@tsed/apollo";
import {ApolloServer} from "apollo-server-express";

@Injectable()
export class UsersService implements AfterRoutesInit {
@Injec()
apolloService: ApolloService;

private server: ApolloServer;

$afterRoutesInit() {
this.server = this.apolloService.get("server1");
}
}
```

For more information about ApolloServer look his documentation [here](https://www.apollographql.com/docs/apollo-server/api/apollo-server.html);

## Contributors

Please read [contributing guidelines here](https://tsed.io/contributing.html)

<a href="https://github.com/tsedio/tsed/graphs/contributors"><img src="https://opencollective.com/tsed/contributors.svg?width=890" /></a>

## Backers

Thank you to all our backers! 🙏 [[Become a backer](https://opencollective.com/tsed#backer)]

<a href="https://opencollective.com/tsed#backers" target="_blank"><img src="https://opencollective.com/tsed/backers.svg?width=890"></a>

## Sponsors

Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [[Become a sponsor](https://opencollective.com/tsed#sponsor)]

## License

The MIT License (MIT)

Copyright (c) 2016 - 2022 Romain Lenzotti

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
55 changes: 55 additions & 0 deletions packages/graphql/graphql-ws/src/GraphQLWSModule.ts
@@ -0,0 +1,55 @@
import {Constant, Inject, InjectorService, Module} from "@tsed/di";
import {useServer} from "graphql-ws/lib/use/ws";
import Http from "http";
import Https from "https";
import {WebSocketServer} from "ws";
import {GraphQLWSOptions} from "./GraphQLWSOptions";

@Module()
export class GraphQLWSModule {
@Constant("graphqlWs", {})
private settings: GraphQLWSOptions;

@Inject(Http.Server)
private httpServer: Http.Server | null;

@Inject(Https.Server)
private httpsServer: Https.Server | null;

@Inject()
private injector: InjectorService;

createWSServer(settings: GraphQLWSOptions) {
const wsServer = new WebSocketServer({
...(this.settings.wsServerOptions || {}),
...settings.wsServerOptions,
server: this.httpsServer || this.httpServer!,
path: settings.path
});

return useServer(
{
...(this.settings.wsUseServerOptions || {}),
...settings.wsUseServerOptions,
schema: settings.schema
},
wsServer
);
}

async $alterApolloServerPlugins(plugins: any[], settings: GraphQLWSOptions) {
const wsServer = await this.createWSServer(settings);

this.injector.logger.info(`Create GraphQL WS server on: ${settings.path}`);

return plugins.concat({
serverWillStart() {
return {
async drainServer() {
await wsServer.dispose();
}
};
}
} as any);
}
}
18 changes: 18 additions & 0 deletions packages/graphql/graphql-ws/src/GraphQLWSOptions.ts
@@ -0,0 +1,18 @@
import {ServerOptions} from "graphql-ws/lib/server";
import {ServerOptions as WSServerOptions} from "ws";

export interface GraphQLWSOptions {
path: string;
schema: any;
wsUseServerOptions?: ServerOptions;
wsServerOptions?: WSServerOptions;
}

declare global {
namespace TsED {
interface ApolloSettings {
wsUseServerOptions?: ServerOptions;
wsServerOptions?: WSServerOptions;
}
}
}
5 changes: 5 additions & 0 deletions packages/graphql/graphql-ws/src/index.ts
@@ -0,0 +1,5 @@
/**
* @file Automatically generated by barrelsby.
*/

export * from "./GraphQLWSModule";
39 changes: 39 additions & 0 deletions packages/graphql/graphql-ws/tsconfig.esm.json
@@ -0,0 +1,39 @@
{
"extends": "@tsed/typescript/tsconfig.node.json",
"compilerOptions": {
"baseUrl": ".",
"module": "ES2020",
"rootDir": "src",
"outDir": "./lib/esm",
"declaration": true,
"declarationDir": "./lib/types",
"composite": true,
"noEmit": false
},
"include": ["src", "src/**/*.json"],
"exclude": [
"node_modules",
"test",
"lib",
"benchmark",
"coverage",
"spec",
"**/*.benchmark.ts",
"**/*.spec.ts",
"keys",
"jest.config.js",
"**/__mock__/**",
"webpack.config.js"
],
"references": [
{
"path": "../../platform/common"
},
{
"path": "../../core"
},
{
"path": "../../di"
}
]
}
39 changes: 39 additions & 0 deletions packages/graphql/graphql-ws/tsconfig.json
@@ -0,0 +1,39 @@
{
"extends": "@tsed/typescript/tsconfig.node.json",
"compilerOptions": {
"baseUrl": ".",
"module": "commonjs",
"rootDir": "src",
"outDir": "./lib/cjs",
"declaration": true,
"declarationDir": "./lib/types",
"composite": true,
"noEmit": false
},
"include": ["src", "src/**/*.json"],
"exclude": [
"node_modules",
"test",
"lib",
"benchmark",
"coverage",
"spec",
"**/*.benchmark.ts",
"**/*.spec.ts",
"keys",
"jest.config.js",
"**/__mock__/**",
"webpack.config.js"
],
"references": [
{
"path": "../../platform/common"
},
{
"path": "../../core"
},
{
"path": "../../di"
}
]
}

0 comments on commit 68ab810

Please sign in to comment.