@@ -99,7 +99,12 @@ export const topologicalSortAST = (schema: GraphQLSchema, ast: DocumentNode): Do
9999
100100 // Create a map of definitions for quick access, using the definition's name as the key.
101101 const definitionsMap : Map < string , DefinitionNode > = new Map ( ) ;
102- ast . definitions . forEach ( definition => {
102+
103+ // SCHEMA_DEFINITION does not have name.
104+ // https://spec.graphql.org/October2021/#sec-Schema
105+ const astDefinitions = ast . definitions . filter ( def => def . kind !== 'SchemaDefinition' ) ;
106+
107+ astDefinitions . forEach ( definition => {
103108 if ( hasNameField ( definition ) && definition . name ) {
104109 definitionsMap . set ( definition . name . value , definition ) ;
105110 }
@@ -122,17 +127,17 @@ export const topologicalSortAST = (schema: GraphQLSchema, ast: DocumentNode): Do
122127 // Add them to notSortedDefinitions.
123128 definitionsMap . forEach ( definition => notSortedDefinitions . push ( definition ) ) ;
124129
125- const definitions = [ ...sortedDefinitions , ...notSortedDefinitions ] ;
130+ const newDefinitions = [ ...sortedDefinitions , ...notSortedDefinitions ] ;
126131
127- if ( definitions . length !== ast . definitions . length ) {
132+ if ( newDefinitions . length !== astDefinitions . length ) {
128133 throw new Error (
129- `unexpected definition length after sorted: want ${ ast . definitions . length } but got ${ definitions . length } `
134+ `unexpected definition length after sorted: want ${ astDefinitions . length } but got ${ newDefinitions . length } `
130135 ) ;
131136 }
132137
133138 return {
134139 ...ast ,
135- definitions : definitions as ReadonlyArray < DefinitionNode > ,
140+ definitions : newDefinitions as ReadonlyArray < DefinitionNode > ,
136141 } ;
137142} ;
138143
0 commit comments