Skip to content

Commit

Permalink
feat: support graphql-ws client for GraphiQL IDE (#152)
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin authored Nov 28, 2021
1 parent be1feec commit 5391a14
Show file tree
Hide file tree
Showing 9 changed files with 368 additions and 55 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,8 @@ The `graphqlHTTP` function accepts the following options:

- **`subscriptionEndpoint`**: An optional GraphQL 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

- **`shouldPersistHeaders`**

- **`editorTheme`**: By passing an object you may change the theme of GraphiQL.
Expand Down
1 change: 0 additions & 1 deletion examples/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import Koa from 'koa';
import mount from 'koa-mount';

import { buildSchema } from 'graphql';

import { graphqlHTTP } from '../src/index';
Expand Down
51 changes: 51 additions & 0 deletions examples/index_subscription.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { createServer } from 'http';

import Koa from 'koa';
import mount from 'koa-mount';
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 = new Koa();
app.use(
mount(
'/graphql',
graphqlHTTP({
schema,
rootValue,
graphiql: {
subscriptionEndpoint,
websocketClient: 'v1',
},
}),
),
);

const server = createServer(app.callback());

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`,
);
});
56 changes: 56 additions & 0 deletions examples/index_subscription_legacy.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { createServer } from 'http';

import Koa from 'koa';
import mount from 'koa-mount';
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 = new Koa();
app.use(
mount(
'/graphql',
graphqlHTTP({
schema,
rootValue,
graphiql: { subscriptionEndpoint },
}),
),
);

const ws = createServer(app.callback());

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

0 comments on commit 5391a14

Please sign in to comment.