1
- import { getOperationRootType } from 'graphql'
1
+ import { GraphQLError } from 'graphql'
2
2
import {
3
3
buildResolveInfo ,
4
- collectFields ,
5
4
ExecutionContext ,
6
5
getFieldDef ,
7
6
} from 'graphql/execution/execute'
7
+ import { collectFields } from 'graphql/execution/collectFields'
8
8
import { getArgumentValues } from 'graphql/execution/values'
9
9
import { addPath } from 'graphql/jsutils/Path'
10
- import { ServerClosure } from '../types'
11
10
12
11
interface ResolverAndArgs {
13
12
field : ReturnType < typeof getFieldDef >
@@ -17,44 +16,52 @@ interface ResolverAndArgs {
17
16
info : ReturnType < typeof buildResolveInfo >
18
17
}
19
18
20
- export const getResolverAndArgs = ( { server, execContext } : { server : ServerClosure , execContext : ExecutionContext } ) : ResolverAndArgs => {
21
- // Taken from graphql-js - https://github.com/graphql/graphql-js/blob/main/src/subscription/subscribe.ts#L189
22
- const type = getOperationRootType ( server . schema , execContext . operation )
23
- const fields = collectFields (
24
- execContext ,
25
- type ,
26
- execContext . operation . selectionSet ,
27
- Object . create ( null ) ,
28
- Object . create ( null ) ,
19
+ export const getResolverAndArgs = ( { execContext } : { execContext : ExecutionContext } ) : ResolverAndArgs => {
20
+ // Taken from graphql-js executeSubscription - https://github.com/graphql/graphql-js/blob/main/src/execution/subscribe.ts#L187
21
+ const { schema, fragments, operation, variableValues, contextValue } = execContext
22
+
23
+ const rootType = schema . getSubscriptionType ( )
24
+ if ( rootType == null ) {
25
+ throw new GraphQLError (
26
+ 'Schema is not configured to execute subscription operation.' ,
27
+ operation ,
28
+ )
29
+ }
30
+
31
+ const rootFields = collectFields (
32
+ schema ,
33
+ fragments ,
34
+ variableValues ,
35
+ rootType ,
36
+ operation . selectionSet ,
29
37
)
30
- const responseNames = Object . keys ( fields )
31
- const responseName = responseNames [ 0 ]
32
- const fieldNodes = fields [ responseName ]
33
- const fieldNode = fieldNodes [ 0 ]
34
- const fieldName = fieldNode . name . value
35
- const field = getFieldDef ( server . schema , type , fieldName )
36
- const path = addPath ( undefined , responseName , type . name )
37
-
38
- if ( ! field ) {
39
- throw new Error ( 'invalid schema, unknown field definition' )
38
+ const [ responseName , fieldNodes ] = [ ...rootFields . entries ( ) ] [ 0 ]
39
+ const fieldDef = getFieldDef ( schema , rootType , fieldNodes [ 0 ] )
40
+
41
+ if ( ! fieldDef ) {
42
+ const fieldName = fieldNodes [ 0 ] . name . value
43
+ throw new GraphQLError (
44
+ `The subscription field "${ fieldName } " is not defined.` ,
45
+ fieldNodes ,
46
+ )
40
47
}
41
48
49
+ const path = addPath ( undefined , responseName , rootType . name )
42
50
const info = buildResolveInfo (
43
51
execContext ,
44
- field ,
52
+ fieldDef ,
45
53
fieldNodes ,
46
- type ,
54
+ rootType ,
47
55
path ,
48
56
)
49
57
50
- const args = getArgumentValues ( field , fieldNode , execContext . variableValues )
51
- const context = execContext . contextValue
58
+ const args = getArgumentValues ( fieldDef , fieldNodes [ 0 ] , variableValues )
52
59
53
60
return {
54
- field,
61
+ field : fieldDef ,
55
62
root : null ,
56
63
args,
57
- context,
64
+ context : contextValue ,
58
65
info,
59
66
}
60
67
}
0 commit comments