Skip to content

Commit

Permalink
Merge pull request #351 from ExpressLRS/subscriptions-bugfix
Browse files Browse the repository at this point in the history
Subscriptions bugfix
  • Loading branch information
jurgelenas authored Jun 29, 2022
2 parents 15c9d70 + cfc227e commit 6395a97
Show file tree
Hide file tree
Showing 6 changed files with 1,155 additions and 1,245 deletions.
85 changes: 43 additions & 42 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -189,33 +189,33 @@
"node-abi": "3.22.0"
},
"devDependencies": {
"@babel/core": "^7.18.2",
"@babel/core": "^7.18.6",
"@babel/eslint-parser": "^7.18.2",
"@babel/plugin-proposal-class-properties": "^7.17.12",
"@babel/plugin-proposal-decorators": "^7.18.2",
"@babel/plugin-proposal-do-expressions": "^7.16.7",
"@babel/plugin-proposal-export-default-from": "^7.17.12",
"@babel/plugin-proposal-export-namespace-from": "^7.17.12",
"@babel/plugin-proposal-function-bind": "^7.16.7",
"@babel/plugin-proposal-function-sent": "^7.18.2",
"@babel/plugin-proposal-json-strings": "^7.17.12",
"@babel/plugin-proposal-logical-assignment-operators": "^7.17.12",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.17.12",
"@babel/plugin-proposal-numeric-separator": "^7.16.7",
"@babel/plugin-proposal-optional-chaining": "^7.17.12",
"@babel/plugin-proposal-pipeline-operator": "^7.18.2",
"@babel/plugin-proposal-private-methods": "^7.17.12",
"@babel/plugin-proposal-private-property-in-object": "^7.17.12",
"@babel/plugin-proposal-throw-expressions": "^7.16.7",
"@babel/plugin-proposal-class-properties": "^7.18.6",
"@babel/plugin-proposal-decorators": "^7.18.6",
"@babel/plugin-proposal-do-expressions": "^7.18.6",
"@babel/plugin-proposal-export-default-from": "^7.18.6",
"@babel/plugin-proposal-export-namespace-from": "^7.18.6",
"@babel/plugin-proposal-function-bind": "^7.18.6",
"@babel/plugin-proposal-function-sent": "^7.18.6",
"@babel/plugin-proposal-json-strings": "^7.18.6",
"@babel/plugin-proposal-logical-assignment-operators": "^7.18.6",
"@babel/plugin-proposal-nullish-coalescing-operator": "^7.18.6",
"@babel/plugin-proposal-numeric-separator": "^7.18.6",
"@babel/plugin-proposal-optional-chaining": "^7.18.6",
"@babel/plugin-proposal-pipeline-operator": "^7.18.6",
"@babel/plugin-proposal-private-methods": "^7.18.6",
"@babel/plugin-proposal-private-property-in-object": "^7.18.6",
"@babel/plugin-proposal-throw-expressions": "^7.18.6",
"@babel/plugin-syntax-dynamic-import": "^7.8.3",
"@babel/plugin-syntax-import-meta": "^7.10.4",
"@babel/plugin-transform-react-constant-elements": "^7.17.12",
"@babel/plugin-transform-react-inline-elements": "^7.16.7",
"@babel/plugin-transform-runtime": "^7.18.2",
"@babel/preset-env": "^7.18.2",
"@babel/preset-react": "^7.17.12",
"@babel/preset-typescript": "^7.17.12",
"@babel/register": "^7.17.7",
"@babel/plugin-transform-react-constant-elements": "^7.18.6",
"@babel/plugin-transform-react-inline-elements": "^7.18.6",
"@babel/plugin-transform-runtime": "^7.18.6",
"@babel/preset-env": "^7.18.6",
"@babel/preset-react": "^7.18.6",
"@babel/preset-typescript": "^7.18.6",
"@babel/register": "^7.18.6",
"@graphql-codegen/cli": "2.6.2",
"@graphql-codegen/introspection": "2.1.1",
"@graphql-codegen/typescript": "2.5.1",
Expand All @@ -234,30 +234,30 @@
"@types/mkdirp": "^1.0.2",
"@types/multicast-dns": "^7.2.1",
"@types/node": "16.11.7",
"@types/react": "^18.0.12",
"@types/react": "^18.0.14",
"@types/react-dom": "^18.0.5",
"@types/react-router-dom": "^5.3.3",
"@types/rimraf": "^3.0.2",
"@types/serialport": "^8.0.2",
"@types/uuid": "^8.3.4",
"@types/webpack-env": "^1.17.0",
"@typescript-eslint/eslint-plugin": "^5.27.1",
"@typescript-eslint/parser": "^5.27.1",
"@typescript-eslint/eslint-plugin": "^5.30.0",
"@typescript-eslint/parser": "^5.30.0",
"babel-jest": "^28.1.1",
"babel-loader": "^8.2.5",
"babel-plugin-dev-expression": "^0.2.3",
"babel-plugin-transform-react-remove-prop-types": "^0.4.24",
"babel-plugin-transform-typescript-metadata": "^0.3.2",
"browserslist-config-erb": "^0.0.3",
"chalk": "^4.1.0",
"concurrently": "^7.2.1",
"core-js": "^3.22.8",
"concurrently": "^7.2.2",
"core-js": "^3.23.3",
"cross-env": "^7.0.3",
"css-loader": "^6.7.1",
"css-minimizer-webpack-plugin": "^4.0.0",
"detect-port": "^1.3.0",
"electron": "^19.0.4",
"electron-builder": "^23.0.3",
"electron": "^19.0.6",
"electron-builder": "^23.1.0",
"electron-devtools-installer": "^3.2.0",
"electron-notarize": "^1.2.1",
"electron-rebuild": "^3.2.7",
Expand Down Expand Up @@ -288,7 +288,7 @@
"sass-loader": "^13.0.0",
"style-loader": "^3.3.1",
"terser-webpack-plugin": "^5.3.3",
"typescript": "^4.7.3",
"typescript": "^4.7.4",
"url-loader": "^4.1.0",
"webpack": "^5.73.0",
"webpack-bundle-analyzer": "^4.5.0",
Expand All @@ -298,26 +298,27 @@
"yarn-deduplicate": "^5.0.0"
},
"dependencies": {
"@apollo/client": "^3.6.7",
"@apollo/client": "^3.6.9",
"@emotion/react": "^11.9.0",
"@emotion/styled": "^11.8.1",
"@fontsource/roboto": "^4.5.7",
"@hapi/joi": "^17.1.1",
"@mui/icons-material": "^5.8.3",
"@mui/material": "^5.8.3",
"@mui/icons-material": "^5.8.4",
"@mui/material": "^5.8.6",
"@octokit/rest": "^18.12.0",
"apollo-server-express": "^3.8.2",
"apollo-server-express": "^3.9.0",
"autosuggest-highlight": "^3.2.1",
"class-validator": "^0.13.2",
"cross-spawn": "^7.0.3",
"electron-debug": "^3.2.0",
"electron-log": "^4.4.7",
"electron-updater": "^5.0.1",
"electron-log": "^4.4.8",
"electron-updater": "^5.0.5",
"express": "^4.17.1",
"extract-zip": "^2.0.1",
"get-port": "^5.1.1",
"graphql": "15.5.0",
"graphql-subscriptions": "^2.0.0",
"graphql-ws": "^5.9.0",
"history": "^5.3.0",
"http": "^0.0.1-security",
"lodash": "^4.17.21",
Expand All @@ -326,18 +327,18 @@
"multicast-dns": "^7.2.5",
"node-fetch": "^2.6.1",
"quick-score": "0.2.0",
"react": "^18.1.0",
"react-dom": "^18.1.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-router-dom": "^6.3.0",
"reflect-metadata": "^0.1.13",
"regenerator-runtime": "^0.13.5",
"semver": "^7.3.7",
"simple-git": "^3.7.1",
"source-map-support": "^0.5.21",
"subscriptions-transport-ws": "^0.11.0",
"type-graphql": "^1.1.1",
"typedi": "^0.10.0",
"winston": "^3.7.2"
"winston": "^3.7.2",
"ws": "^8.8.0"
},
"devEngines": {
"node": ">=16.x",
Expand Down
34 changes: 16 additions & 18 deletions src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createServer } from 'http';
import { ApolloServer } from 'apollo-server-express';
import { SubscriptionServer } from 'subscriptions-transport-ws';
import { PubSub } from 'graphql-subscriptions';
import { execute, subscribe } from 'graphql';
import { WebSocketServer } from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';
import express, { Express } from 'express';
import * as http from 'http';
import getPort from 'get-port';
Expand Down Expand Up @@ -156,11 +156,16 @@ export default class ApiServer {

this.httpServer = createServer(this.app);
/*
I know, crazy. It is 45 minutes, but on slower network connection it might take a while to download
and install all Platformio dependencies and build firmware.
*/
I know, crazy. It is 45 minutes, but on slower network connection it might take a while to download
and install all Platformio dependencies and build firmware.
*/
this.httpServer.setTimeout(45 * 60 * 1000);

const wsServer = new WebSocketServer({
server: this.httpServer,
path: '/graphql',
});

const schema = await buildSchema({
resolvers: [
FirmwareResolver,
Expand All @@ -173,7 +178,11 @@ export default class ApiServer {
container: Container,
pubSub: Container.get<PubSub>(PubSubToken),
});
let subscriptionServer: SubscriptionServer | undefined;

// not a React component
// eslint-disable-next-line react-hooks/rules-of-hooks
const serverCleanup = useServer({ schema }, wsServer);

const server = new ApolloServer({
schema,
introspection: true,
Expand All @@ -182,24 +191,13 @@ export default class ApiServer {
async serverWillStart() {
return {
async drainServer() {
subscriptionServer?.close();
serverCleanup?.dispose();
},
};
},
},
],
});
subscriptionServer = SubscriptionServer.create(
{
schema,
execute,
subscribe,
},
{
server: this.httpServer,
path: server.graphqlPath,
}
);

// You must `await server.start()` before calling `server.applyMiddleware()
await server.start();
Expand Down
6 changes: 5 additions & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ import App from './ui/App';

const container = document.getElementById('root');
const root = createRoot(container!);
root.render(<App />);
root.render(
<React.StrictMode>
<App />
</React.StrictMode>
);
15 changes: 7 additions & 8 deletions src/ui/gql/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ApolloClient, HttpLink, InMemoryCache, split } from '@apollo/client';
import { getMainDefinition } from '@apollo/client/utilities';
import { WebSocketLink } from '@apollo/client/link/ws';
import { GraphQLWsLink } from '@apollo/client/link/subscriptions';
import { createClient } from 'graphql-ws';

const searchParams = new URLSearchParams(window.location.search.slice(1));
const httpLink = new HttpLink({
Expand All @@ -11,13 +12,11 @@ const httpLink = new HttpLink({
}),
});

const wsLink = new WebSocketLink({
uri: searchParams.get('subscriptions_url') ?? 'ws://localhost:3500/graphql',
options: {
reconnect: true,
lazy: true,
},
});
const wsLink = new GraphQLWsLink(
createClient({
url: searchParams.get('subscriptions_url') ?? 'ws://localhost:3500/graphql',
})
);

// Send query request based on the type definition
const link = split(
Expand Down
1 change: 0 additions & 1 deletion src/ui/views/ConfiguratorView/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,6 @@ const ConfiguratorView: FunctionComponent<ConfiguratorViewProps> = (props) => {
});
}, []);
useBuildLogUpdatesSubscription({
fetchPolicy: 'network-only',
onSubscriptionData: (options) => {
const args = options.subscriptionData.data?.buildLogUpdates.data;
if (args !== undefined && eventsBatcherRef.current !== null) {
Expand Down
Loading

0 comments on commit 6395a97

Please sign in to comment.