File tree Expand file tree Collapse file tree 5 files changed +127
-0
lines changed Expand file tree Collapse file tree 5 files changed +127
-0
lines changed Original file line number Diff line number Diff line change
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
+ } )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
1
+ import { createServer } from 'http'
2
+ import { yoga } from './yoga'
3
+
4
+ const server = createServer ( yoga )
5
+ server . listen ( 4000 )
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments