Skip to content

Commit

Permalink
Subscription support for GraphiQL
Browse files Browse the repository at this point in the history
  • Loading branch information
chentsulin committed Nov 28, 2021
1 parent 9fed34d commit be1feec
Show file tree
Hide file tree
Showing 6 changed files with 445 additions and 4 deletions.
6 changes: 3 additions & 3 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copied from '.gitignore', please keep it in sync.
dist
node_modules
coverage
/node_modules
/coverage
/npmDist
48 changes: 48 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,52 @@ app.use(
);
```

## Setup with Subscription Support

```js
const Koa = require('koa');
const mount = require('koa-mount');
const { graphqlHTTP } = require('koa-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 { SubscriptionServer } = require('subscriptions-transport-ws');
const PORT = 4000;
const app = new Koa();
app.use(
mount(
'/graphql',
graphqlHTTP({
schema: schema,
graphiql: {
subscriptionEndpoint: `ws://localhost:${PORT}/subscriptions`,
},
}),
),
);
const ws = createServer(app.callback());
ws.listen(PORT, () => {
// Set up the WebSocket for handling GraphQL subscriptions.
new SubscriptionServer(
{
execute,
subscribe,
schema,
},
{
server: ws,
path: '/subscriptions',
},
);
});
```

## Options

The `graphqlHTTP` function accepts the following options:
Expand All @@ -102,6 +148,8 @@ 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.

- **`shouldPersistHeaders`**

- **`editorTheme`**: By passing an object you may change the theme of GraphiQL.
Expand Down
Loading

0 comments on commit be1feec

Please sign in to comment.