This repository has been archived by the owner on Mar 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 538
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Motivation: use one example to run query/mutation/subscription Also don't use unnessary 3rd-party packages in examples
- Loading branch information
1 parent
2061b09
commit ef117a2
Showing
6 changed files
with
55 additions
and
160 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
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`); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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