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

Simplify subscription example #811

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 5 additions & 13 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 @@ -142,9 +134,9 @@ The `graphqlHTTP` function accepts the following options:
- **`headers`**: An optional string of initial state for the header editor. Only makes sense if headerEditorEnabled is `true`.
Defaults to empty.

- **`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
- **`websocketClient`**: An optional string for websocket client used for subscription, `v0`: subscriptions-transport-ws, `v1`: graphql-ws. Defaults to `v0` if not provided

- **`rootValue`**: A value to pass as the `rootValue` to the `execute()`
function from [`GraphQL.js/src/execute.ts`](https://github.com/graphql/graphql-js/blob/main/src/execution/execute.ts#L148).
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