Skip to content
This repository has been archived by the owner on Mar 20, 2023. It is now read-only.

Commit

Permalink
Simplify subscription example
Browse files Browse the repository at this point in the history
Motivation: use one example to run query/mutation/subscription
Also don't use unnessary 3rd-party packages in examples
  • Loading branch information
IvanGoncharov committed Nov 3, 2022
1 parent f4414b4 commit a71d3b7
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 159 deletions.
16 changes: 4 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,8 @@ app.listen(4000);
const express = require('express');
const { graphqlHTTP } = require('express-graphql');

const typeDefs = require('./schema');
const resolvers = require('./resolvers');
const { makeExecutableSchema } = require('graphql-tools');
const schema = makeExecutableSchema({
typeDefs: typeDefs,
resolvers: resolvers,
});

const { execute, subscribe } = require('graphql');
const { createServer } = require('http');
const { execute, subscribe } = require('graphql');
const { SubscriptionServer } = require('subscriptions-transport-ws');

const PORT = 4000;
Expand All @@ -93,7 +85,7 @@ var app = express();
app.use(
'/graphql',
graphqlHTTP({
schema: schema,
schema: MyGraphQLSchema,
graphiql: { subscriptionEndpoint: `ws://localhost:${PORT}/subscriptions` },
}),
);
Expand All @@ -102,7 +94,7 @@ const ws = createServer(app);

ws.listen(PORT, () => {
// Set up the WebSocket for handling GraphQL subscriptions.
new SubscriptionServer(
SubscriptionServer.create(
{
execute,
subscribe,
Expand Down Expand Up @@ -136,7 +128,7 @@ The `graphqlHTTP` function accepts the following options:
- **`headerEditorEnabled`**: An optional boolean which enables the header editor when `true`.
Defaults to `false`.

- **`subscriptionEndpoint`**: An optional GraphQL string contains the WebSocket server url for subscription.
- **`subscriptionEndpoint`**: An optional string contains the WebSocket server url for subscription.

- **`websocketClient`**: An optional GraphQL string for websocket client used for subscription, `v0`: subscriptions-transport-ws, `v1`: graphql-ws. Defaults to `v0` if not provided

Expand Down
54 changes: 49 additions & 5 deletions examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,72 @@
import { createServer } from 'http';

import express from 'express';
import { buildSchema } from 'graphql';
import { execute, subscribe, buildSchema } from 'graphql';
import { SubscriptionServer } from 'subscriptions-transport-ws';

import { graphqlHTTP } from '../src';

// Construct a schema, using GraphQL schema language
const schema = buildSchema(`
type Query {
hello: String
hello: String!
}
type Subscription {
currentTime: String!
}
`);

// The root provides a resolver function for each API endpoint
const rootValue = {
hello: () => 'Hello world!',
currentTime: () => currentTimeGenerator(),
};

async function* currentTimeGenerator() {
while (true) {
const currentTime = (new Date()).toLocaleTimeString();
console.log('Pushed current time over subscriptions: ' + currentTime);
yield { currentTime };

await later(1);
}
}

function later(delayInSeconds: number) {
return new Promise((resolve) => setTimeout(resolve, delayInSeconds * 1000));
}

const PORT = 4001;
const app = express();
app.use(
'/graphql',
graphqlHTTP({
schema,
rootValue,
graphiql: { headerEditorEnabled: true },
graphiql: {
headerEditorEnabled: true,
subscriptionEndpoint: `ws://localhost:${PORT}/subscriptions`,
},
}),
);
app.listen(4000);
console.log('Running a GraphQL API server at http://localhost:4000/graphql');

const ws = createServer(app);

ws.listen(PORT, () => {
// Set up the WebSocket for handling GraphQL subscriptions.
SubscriptionServer.create(
{
execute,
subscribe,
schema,
rootValue,
},
{
server: ws,
path: '/subscriptions',
},
);
});

console.log(`Running a GraphQL API server at http://localhost:${PORT}/graphql`);
48 changes: 0 additions & 48 deletions examples/index_subscription.ts

This file was deleted.

53 changes: 0 additions & 53 deletions examples/index_subscription_legacy.ts

This file was deleted.

37 changes: 0 additions & 37 deletions examples/schema.ts

This file was deleted.

5 changes: 1 addition & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,7 @@
"check:spelling": "cspell '**/*'",
"check:integrations": "mocha --full-trace integrationTests/*-test.js",
"build:npm": "node resources/build-npm.js",
"start": "node -r ./resources/register.js examples/index.ts",
"start:subscription": "node -r ./resources/register.js examples/index_subscription.ts",
"start:subscription_legacy": "node -r ./resources/register.js examples/index_subscription_legacy.ts"
"start": "node -r ./resources/register.js examples/index.ts"
},
"dependencies": {
"accepts": "^1.3.7",
Expand Down Expand Up @@ -100,7 +98,6 @@
"react-dom": "16.14.0",
"restify": "8.5.1",
"sinon": "9.2.1",
"subscriptions-transport-ws": "0.9.18",
"supertest": "6.0.1",
"ts-node": "9.0.0",
"typescript": "4.1.2",
Expand Down

0 comments on commit a71d3b7

Please sign in to comment.