Skip to content

Commit

Permalink
fix: use patched graphql-subscriptions
Browse files Browse the repository at this point in the history
Patch graphql-subscriptions with changes from the [release 3.0 PR](apollographql/graphql-subscriptions#250) until it's merged
  • Loading branch information
tim-stasse committed Feb 22, 2022
1 parent 8d8cc34 commit d7c103f
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 27 deletions.
8 changes: 2 additions & 6 deletions backend/src/graphql/modules/game.configuration/resolvers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,8 @@ const resolvers: Resolvers = {
Subscription: {
gameConfig: {
resolve: () => gameConfig,
subscribe: async () => ({
[Symbol.asyncIterator]() {
return pubsub.asyncIterator('GAME_STATE_UPDATED');
}
})
}
subscribe: async () => pubsub.asyncIterableIterator('GAME_STATE_UPDATED'),
},
},
};

Expand Down
51 changes: 31 additions & 20 deletions backend/src/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import {
import { healthCheck } from './plugins/health';
import { AddressInfo } from 'net';


export default async function startApolloServer(schema: GraphQLSchema) {
// TODO: replace fastify logging with something appropriate
const app = express();
Expand All @@ -46,34 +45,46 @@ export default async function startApolloServer(schema: GraphQLSchema) {
path: GRAPHQL_ENDPOINT,
});

wsServer.on('listening', function() {
wsServer.on('listening', function () {
const { port, family, address } = wsServer.address() as AddressInfo;
console.log(`🚀 Subscriptions ready at ws://${address}:${port}${GRAPHQL_ENDPOINT} - ${family}`);
})
console.log(
`🚀 Subscriptions ready at ws://${address}:${port}${GRAPHQL_ENDPOINT} - ${family}`
);
});

app.get('/health', healthCheck(wsServer));
app.get('/graphql', (_, res) => {
res.sendFile(path.join(__dirname, 'views', 'graphiql', 'index.html'))
})

useServer({ schema,
// season to taste...
onConnect: (ctx) => console.log('Connected', ctx),
onSubscribe: (ctx, msg) => console.log('Subscribe', { ctx, msg }),
onNext: (ctx, msg, args, result) => console.debug('Next', { ctx, msg, args, result }),
onError: (ctx, msg, errors) => console.error('Error', { ctx, msg, errors }),
onComplete: (ctx, msg) => console.log('Completed!', { ctx, msg }),
onDisconnect: (ctx, msg, args) => console.log('Disconnected!', ctx, msg, args),
}, wsServer);
res.sendFile(path.join(__dirname, 'views', 'graphiql', 'index.html'));
});

useServer(
{
schema,
// season to taste...
onConnect: (ctx) => console.log('Connected', ctx),
onSubscribe: (ctx, msg) => console.log('Subscribe', { ctx, msg }),
onNext: (ctx, msg, args, result) =>
console.debug('Next', { ctx, msg, args, result }),
onError: (ctx, msg, errors) =>
console.error('Error', { ctx, msg, errors }),
onComplete: (ctx, msg) => console.log('Completed!', { ctx, msg }),
onDisconnect: (ctx, msg, args) =>
console.log('Disconnected!', ctx, msg, args),
},
wsServer
);

await server.start();

server.applyMiddleware({ app });

await new Promise<void>(resolve => httpServer.listen(HTTP_PORT, HTTP_ADDRESS, resolve));
await new Promise<void>((resolve) =>
httpServer.listen(HTTP_PORT, HTTP_ADDRESS, resolve)
);

console.log(`🚀 HTTP Server ready at http://${HTTP_ADDRESS}:${HTTP_PORT}${server.graphqlPath}`);
console.log(
`🚀 HTTP Server ready at http://${HTTP_ADDRESS}:${HTTP_PORT}${server.graphqlPath}`
);

return app
return app;
}

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,8 @@
"rollup-plugin-lit-css": "^2.x",
"ts-node": "^10.5.0",
"typescript": "^4.5.5"
},
"resolutions": {
"graphql-subscriptions": "patch:graphql-subscriptions@npm:2.0.0#.yarn/patches/graphql-subscriptions-npm-2.0.0-53564d1c84"
}
}
13 changes: 12 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -6337,7 +6337,7 @@ __metadata:
languageName: node
linkType: hard

"graphql-subscriptions@npm:^2.0.0":
"graphql-subscriptions@npm:2.0.0":
version: 2.0.0
resolution: "graphql-subscriptions@npm:2.0.0"
dependencies:
Expand All @@ -6348,6 +6348,17 @@ __metadata:
languageName: node
linkType: hard

"graphql-subscriptions@patch:graphql-subscriptions@npm:2.0.0#.yarn/patches/graphql-subscriptions-npm-2.0.0-53564d1c84::locator=2022-game-app%40workspace%3A.":
version: 2.0.0
resolution: "graphql-subscriptions@patch:graphql-subscriptions@npm%3A2.0.0#.yarn/patches/graphql-subscriptions-npm-2.0.0-53564d1c84::version=2.0.0&hash=fa51e0&locator=2022-game-app%40workspace%3A."
dependencies:
iterall: ^1.3.0
peerDependencies:
graphql: ^15.7.2 || ^16.0.0
checksum: fb92d898e64b40badb0dc46ec7ef8c41f4a4c6e28b9dd71b8ff3b214173ffd9a5ffd4f80916bce4b7caef7e21521163cc5381147be08ba21ebdfa9fb6b6d653f
languageName: node
linkType: hard

"graphql-tag@npm:^2.11.0, graphql-tag@npm:^2.12.3":
version: 2.12.6
resolution: "graphql-tag@npm:2.12.6"
Expand Down

0 comments on commit d7c103f

Please sign in to comment.