Skip to content
This repository was archived by the owner on Mar 20, 2023. It is now read-only.
Closed
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
48 changes: 48 additions & 0 deletions examples/index_subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import { createServer } from 'http';

import express from 'express';
import { execute, subscribe } from 'graphql';
import ws from 'ws';
import { useServer } from 'graphql-ws/lib/use/ws';

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

import { schema, roots, rootValue } from './schema';

const PORT = 4000;
const subscriptionEndpoint = `ws://localhost:${PORT}/subscriptions`;

const app = express();
app.use(
'/graphql',
graphqlHTTP({
schema,
rootValue,
graphiql: {
subscriptionEndpoint,
websocketClient: 'v1',
},
}),
);

const server = createServer(app);

const wsServer = new ws.Server({
server,
path: '/subscriptions',
});

server.listen(PORT, () => {
useServer(
{
schema,
roots,
execute,
subscribe,
},
wsServer,
);
console.info(
`Running a GraphQL API server with subscriptions at http://localhost:${PORT}/graphql`,
);
});
53 changes: 53 additions & 0 deletions examples/index_subscription_legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { createServer } from 'http';

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

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

import { schema, rootValue } from './schema';

const PORT = 4000;
const subscriptionEndpoint = `ws://localhost:${PORT}/subscriptions`;

const app = express();
app.use(
'/graphql',
graphqlHTTP({
schema,
rootValue,
graphiql: { subscriptionEndpoint },
}),
);

const ws = createServer(app);

ws.listen(PORT, () => {
console.log(
`Running a GraphQL API server with subscriptions at http://localhost:${PORT}/graphql`,
);
});

const onConnect = (_: any, __: any) => {
console.log('connecting ....');
};

const onDisconnect = (_: any) => {
console.log('disconnecting ...');
};

SubscriptionServer.create(
{
schema,
rootValue,
execute,
subscribe,
onConnect,
onDisconnect,
},
{
server: ws,
path: '/subscriptions',
},
);
37 changes: 37 additions & 0 deletions examples/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { buildSchema } from 'graphql';

function sleep(ms: number) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}

export const schema = buildSchema(`
type Query {
hello: String
}
type Subscription {
countDown: Int
}
`);

export const roots = {
Query: {
hello: () => 'Hello World!',
},
subscription: {
/* eslint no-await-in-loop: "off" */

countDown: async function* fiveToOne() {
for (const number of [5, 4, 3, 2, 1]) {
await sleep(1000); // slow down a bit so user can see the count down on GraphiQL
yield { countDown: number };
}
},
},
};

export const rootValue = {
hello: roots.Query.hello,
countDown: roots.subscription.countDown,
};
Loading