Skip to content

Commit 9621b88

Browse files
authored
Simple Node with TypeScript example (#1774)
* ts example * use yoga.fetch and micro TS example
1 parent 44878a5 commit 9621b88

File tree

5 files changed

+127
-0
lines changed

5 files changed

+127
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import { yoga } from '../src/yoga'
2+
3+
describe('node-ts example integration', () => {
4+
it('should execute query', async () => {
5+
const response = await yoga.fetch('/graphql?query={hello}')
6+
7+
expect(response.status).toBe(200)
8+
expect(await response.text()).toMatchInlineSnapshot(
9+
`"{"data":{"hello":"world"}}"`,
10+
)
11+
})
12+
13+
it('should have subscriptions disabled', async () => {
14+
const response = await yoga.fetch(
15+
'/graphql?query=subscription{greetings}',
16+
{
17+
headers: {
18+
Accept: 'text/event-stream',
19+
},
20+
},
21+
)
22+
23+
expect(response.status).toBe(400)
24+
expect(await response.text()).toMatchInlineSnapshot(`
25+
"data: {"errors":[{"message":"Subscriptions have been disabled"}]}
26+
27+
"
28+
`)
29+
})
30+
})

examples/node-ts/package.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"name": "example-node-ts",
3+
"private": true,
4+
"version": "1.0.0",
5+
"scripts": {
6+
"start": "ts-node src/index.ts",
7+
"check": "tsc --pretty --noEmit"
8+
},
9+
"dependencies": {
10+
"graphql-yoga": "3.0.0-next.0",
11+
"graphql": "16.6.0"
12+
},
13+
"devDependencies": {
14+
"ts-node": "10.9.1",
15+
"typescript": "4.8.3"
16+
}
17+
}

examples/node-ts/src/index.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { createServer } from 'http'
2+
import { yoga } from './yoga'
3+
4+
const server = createServer(yoga)
5+
server.listen(4000)

examples/node-ts/src/yoga.ts

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
import { createYoga, Plugin, createSchema } from 'graphql-yoga'
2+
import { GraphQLError } from 'graphql'
3+
4+
// available when handling requests, needs to be provided by the implementor
5+
type ServerContext = {}
6+
7+
// available in GraphQL, during execution/subscription
8+
interface UserContext {
9+
disableSubscription: boolean
10+
}
11+
12+
export const yoga = createYoga<ServerContext, UserContext>({
13+
context: {
14+
disableSubscription: true,
15+
},
16+
schema: createSchema({
17+
typeDefs: /* GraphQL */ `
18+
type Query {
19+
hello: String!
20+
}
21+
type Subscription {
22+
greetings: String!
23+
}
24+
`,
25+
resolvers: {
26+
Query: {
27+
hello: () => 'world',
28+
},
29+
Subscription: {
30+
greetings: {
31+
async *subscribe() {
32+
yield { greetings: 'Hi' }
33+
},
34+
},
35+
},
36+
},
37+
}),
38+
plugins: [useDisableSubscription()],
39+
})
40+
41+
// context only relevant to the plugin
42+
type DisableSubscriptionPluginContext = {}
43+
44+
function useDisableSubscription(): Plugin<
45+
DisableSubscriptionPluginContext,
46+
ServerContext,
47+
UserContext
48+
> {
49+
return {
50+
onSubscribe({ args }) {
51+
if (args.contextValue.disableSubscription) {
52+
throw new GraphQLError('Subscriptions have been disabled', {
53+
extensions: {
54+
http: {
55+
status: 400, // report error with a 400
56+
},
57+
},
58+
})
59+
}
60+
},
61+
}
62+
}

examples/node-ts/tsconfig.json

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"compilerOptions": {
3+
"target": "esnext",
4+
"moduleResolution": "node",
5+
"module": "commonjs",
6+
"sourceMap": true,
7+
"lib": ["esnext", "DOM", "DOM.Iterable"],
8+
"allowSyntheticDefaultImports": true,
9+
"skipLibCheck": true
10+
},
11+
"include": ["src"],
12+
"exclude": ["node_modules", "dist", "test"]
13+
}

0 commit comments

Comments
 (0)