1- import { getOperationRootType } from 'graphql'
1+ import { GraphQLError } from 'graphql'
22import {
33 buildResolveInfo ,
4- collectFields ,
54 ExecutionContext ,
65 getFieldDef ,
76} from 'graphql/execution/execute'
7+ import { collectFields } from 'graphql/execution/collectFields'
88import { getArgumentValues } from 'graphql/execution/values'
99import { addPath } from 'graphql/jsutils/Path'
10- import { ServerClosure } from '../types'
1110
1211interface ResolverAndArgs {
1312 field : ReturnType < typeof getFieldDef >
@@ -17,44 +16,52 @@ interface ResolverAndArgs {
1716 info : ReturnType < typeof buildResolveInfo >
1817}
1918
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 ,
2937 )
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+ )
4047 }
4148
49+ const path = addPath ( undefined , responseName , rootType . name )
4250 const info = buildResolveInfo (
4351 execContext ,
44- field ,
52+ fieldDef ,
4553 fieldNodes ,
46- type ,
54+ rootType ,
4755 path ,
4856 )
4957
50- const args = getArgumentValues ( field , fieldNode , execContext . variableValues )
51- const context = execContext . contextValue
58+ const args = getArgumentValues ( fieldDef , fieldNodes [ 0 ] , variableValues )
5259
5360 return {
54- field,
61+ field : fieldDef ,
5562 root : null ,
5663 args,
57- context,
64+ context : contextValue ,
5865 info,
5966 }
6067}
0 commit comments