-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support graphql-ws client for GraphiQL IDE (#152)
- Loading branch information
1 parent
be1feec
commit 5391a14
Showing
9 changed files
with
368 additions
and
55 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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`, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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', | ||
}, | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}; |
Oops, something went wrong.