Skip to content

Commit

Permalink
fix: various improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
GauBen committed Jul 7, 2022
1 parent 5eb97ca commit 6be6e47
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 21 deletions.
14 changes: 8 additions & 6 deletions doc/src/markdown/basics/subscriptions.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ category: Basics

## Subscriptions

Zeus supports [GraphQL subscriptions](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md) out-of-the-box and is compatible with many popular GraphQL servers.
Zeus supports [GraphQL over WebSocket subscriptions](https://github.com/enisdenjo/graphql-ws/blob/master/PROTOCOL.md) out-of-the-box and is compatible with many popular GraphQL servers.

Two standards are supported:
Two implementations are supported:

- **graphql-ws**: the modern WebSocket-based transport, implemented by [the graphql-ws package](https://www.npmjs.com/package/graphql-ws). It is the standard [used by Apollo](https://www.apollographql.com/docs/react/data/subscriptions/#choosing-a-subscription-library).
- **legacy** (default): a transport based on raw WebSockets.
Expand All @@ -25,20 +25,20 @@ zeus schema.gql ./ --subscriptions graphql-ws
npm install graphql-ws
```

If you want to use **legacy**, use `--subscriptions legacy` instead.
If you want to use **legacy**, use `--subscriptions legacy` instead. You may need to install [ws](https://www.npmjs.com/package/ws) depending on your setup.

No matter what implementation you chose, usage is the same:

```ts
// Create a new Subscription with some authentication headers
const sub = Subscription('wss:https://localhost:4000/graphql', {
const wsChain = Subscription('wss:https://localhost:4000/graphql', {
get headers() {
return { Authorization: `Bearer ${getToken()}` };
},
});

// Subscribe to new messages
sub('subscription')({
wsChain('subscription')({
message: {
body: true,
},
Expand All @@ -51,7 +51,7 @@ If you need to unsubscribe from a subscription (e.g. you are developing as Singl

```ts
// Subscribe to new messages
const onMessage = sub('subscription')({
const onMessage = wsChain('subscription')({
message: {
body: true,
},
Expand All @@ -63,3 +63,5 @@ onMessage.on(({ message }) => {
// Close the underlying connection
onMessage.ws.close();
```

While you may use `wsChain('query')` or `wsChain('mutation')`, [Apollo strongly discourages this practice.](https://www.apollographql.com/docs/react/data/subscriptions/#3-split-communication-by-operation-recommended)
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@
"cz-conventional-changelog": "^3.1.0",
"eslint": "^7.19.0",
"eslint-config-prettier": "^7.2.0",
"eslint-plugin-prettier": "^3.3.1",
"eslint-plugin-prettier": "^4.2.1",
"graphql-ws": "^5.8.2",
"husky": "^4.2.3",
"jest": "^27.5.1",
"json-schema": "^0.3.0",
"mocha": "^7.1.0",
"prettier": "^2.0.2",
"prettier": "^2.7.1",
"ts-jest": "^27.1.4",
"ts-node": "^10.8.1",
"ts-toolbelt": "^6.3.6",
Expand All @@ -68,10 +68,10 @@
"yargs": "^16.1.1"
},
"peerDependencies": {
"graphql-wq": ">=5"
"graphql-ws": ">=5"
},
"peerDependenciesMeta": {
"graphql-wq": {
"graphql-ws": {
"optional": true
}
},
Expand Down
2 changes: 1 addition & 1 deletion src/CLI/CLIClass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export class CLI {
env,
host,
esModule: !!args.esModule,
subscriptions: args.subscriptions == 'graphql-ws' ? 'graphql-ws' : 'legacy',
subscriptions: args.subscriptions === 'graphql-ws' ? 'graphql-ws' : 'legacy',
});
Object.keys(typeScriptDefinition).forEach((k) =>
writeFileRecursive(
Expand Down
11 changes: 9 additions & 2 deletions src/TreeToTS/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface ResolveOptions {
host?: string;
headers?: Record<string, string>;
esModule?: boolean;
subscriptions: 'legacy' | 'graphql-ws';
subscriptions?: 'legacy' | 'graphql-ws';
}

const disableLintersComments = ['eslint-disable'];
Expand Down Expand Up @@ -96,7 +96,14 @@ export class TreeToTS {
/**
* Generate typescript file
*/
static resolveTreeSplit({ tree, env = 'browser', host, esModule, headers, subscriptions }: ResolveOptions) {
static resolveTreeSplit({
tree,
env = 'browser',
host,
esModule,
headers,
subscriptions = 'legacy',
}: ResolveOptions) {
return {
indexImports: `import { AllTypesProps, ReturnTypes, Ops } from './const${esModule ? '.js' : ''}';`.concat(
env === 'node'
Expand Down
16 changes: 8 additions & 8 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2574,10 +2574,10 @@ eslint-config-prettier@^7.2.0:
resolved "https://registry.npmjs.org/eslint-config-prettier/-/eslint-config-prettier-7.2.0.tgz"
integrity sha512-rV4Qu0C3nfJKPOAhFujFxB7RMP+URFyQqqOZW9DMRD7ZDTFyjaIlETU3xzHELt++4ugC0+Jm084HQYkkJe+Ivg==

eslint-plugin-prettier@^3.3.1:
version "3.4.1"
resolved "https://registry.npmjs.org/eslint-plugin-prettier/-/eslint-plugin-prettier-3.4.1.tgz"
integrity sha512-htg25EUYUeIhKHXjOinK4BgCcDwtLHjqaxCDsMy5nbnUMkKFvIhMVCp+5GFUXQ4Nr8lBsPqtGAqBenbpFqAA2g==
eslint-plugin-prettier@^4.2.1:
version "4.2.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-prettier/-/eslint-plugin-prettier-4.2.1.tgz#651cbb88b1dab98bfd42f017a12fa6b2d993f94b"
integrity sha512-f/0rXLXUt0oFYs8ra4w49wYZBG5GKZpAYsJSm6rnYL5uVDjd+zowwMwVZHnAjf4edNrKpCDYfXDgmRE/Ak7QyQ==
dependencies:
prettier-linter-helpers "^1.0.0"

Expand Down Expand Up @@ -4777,10 +4777,10 @@ prettier-linter-helpers@^1.0.0:
dependencies:
fast-diff "^1.1.2"

prettier@^2.0.2:
version "2.6.2"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.6.2.tgz#e26d71a18a74c3d0f0597f55f01fb6c06c206032"
integrity sha512-PkUpF+qoXTqhOeWL9fu7As8LXsIUZ1WYaJiY/a7McAQzxjk82OF0tibkFXVCDImZtWxbvojFjerkiLb0/q8mew==
prettier@^2.7.1:
version "2.7.1"
resolved "https://registry.yarnpkg.com/prettier/-/prettier-2.7.1.tgz#e235806850d057f97bb08368a4f7d899f7760c64"
integrity sha512-ujppO+MkdPqoVINuDFDRLClm7D78qbDt0/NR+wp5FqEZOoTNAjPHWj17QRhu7geIHJfcNhRk1XVQmF8Bp3ye+g==

pretty-format@^27.0.0, pretty-format@^27.5.1:
version "27.5.1"
Expand Down

0 comments on commit 6be6e47

Please sign in to comment.