@@ -2,15 +2,14 @@ import type { AST } from 'eslint';
2
2
import type { JSONSchema4 } from 'json-schema' ;
3
3
import {
4
4
Kind ,
5
- TypeInfo ,
6
5
DocumentNode ,
7
6
GraphQLSchema ,
8
7
ValidationRule ,
9
8
FragmentDefinitionNode ,
10
9
OperationDefinitionNode ,
11
10
visit ,
12
11
validate ,
13
- visitWithTypeInfo ,
12
+ ASTVisitor ,
14
13
} from 'graphql' ;
15
14
import { validateSDL } from 'graphql/validation/validate' ;
16
15
import type { GraphQLESLintRule , GraphQLESLintRuleContext } from '../types' ;
@@ -65,59 +64,47 @@ function validateDocument(
65
64
}
66
65
}
67
66
68
- type FragmentInfo = `${string } :${string } `;
67
+ type GetFragmentDefsAndFragmentSpreads = {
68
+ fragmentDefs : Set < string > ;
69
+ fragmentSpreads : Set < string > ;
70
+ } ;
69
71
70
- const getFragmentDefsAndFragmentSpreads = (
71
- schema : GraphQLSchema ,
72
- node : DocumentNode
73
- ) : {
74
- fragmentDefs : Set < FragmentInfo > ;
75
- fragmentSpreads : Set < FragmentInfo > ;
76
- } => {
77
- const typeInfo = new TypeInfo ( schema ) ;
78
- const fragmentDefs = new Set < FragmentInfo > ( ) ;
79
- const fragmentSpreads = new Set < FragmentInfo > ( ) ;
72
+ const getFragmentDefsAndFragmentSpreads = ( node : DocumentNode ) : GetFragmentDefsAndFragmentSpreads => {
73
+ const fragmentDefs = new Set < string > ( ) ;
74
+ const fragmentSpreads = new Set < string > ( ) ;
80
75
81
- const visitor = visitWithTypeInfo ( typeInfo , {
76
+ const visitor : ASTVisitor = {
82
77
FragmentDefinition ( node ) {
83
- fragmentDefs . add ( ` ${ node . name . value } : ${ node . typeCondition . name . value } ` ) ;
78
+ fragmentDefs . add ( node . name . value ) ;
84
79
} ,
85
80
FragmentSpread ( node ) {
86
- const parentType = typeInfo . getParentType ( ) ;
87
- if ( parentType ) {
88
- fragmentSpreads . add ( `${ node . name . value } :${ parentType . name } ` ) ;
89
- }
81
+ fragmentSpreads . add ( node . name . value ) ;
90
82
} ,
91
- } ) ;
83
+ } ;
92
84
93
85
visit ( node , visitor ) ;
94
86
return { fragmentDefs, fragmentSpreads } ;
95
87
} ;
96
88
97
- const getMissingFragments = ( schema : GraphQLSchema , node : DocumentNode ) : FragmentInfo [ ] => {
98
- const { fragmentDefs, fragmentSpreads } = getFragmentDefsAndFragmentSpreads ( schema , node ) ;
89
+ const getMissingFragments = ( node : DocumentNode ) : string [ ] => {
90
+ const { fragmentDefs, fragmentSpreads } = getFragmentDefsAndFragmentSpreads ( node ) ;
99
91
return [ ...fragmentSpreads ] . filter ( name => ! fragmentDefs . has ( name ) ) ;
100
92
} ;
101
93
102
94
type GetDocumentNode = ( props : {
103
95
ruleId : string ;
104
96
context : GraphQLESLintRuleContext ;
105
- schema : GraphQLSchema ;
106
97
node : DocumentNode ;
107
98
} ) => DocumentNode ;
108
99
109
- const handleMissingFragments : GetDocumentNode = ( { ruleId, context, schema , node } ) => {
110
- const missingFragments = getMissingFragments ( schema , node ) ;
100
+ const handleMissingFragments : GetDocumentNode = ( { ruleId, context, node } ) => {
101
+ const missingFragments = getMissingFragments ( node ) ;
111
102
if ( missingFragments . length > 0 ) {
112
103
const siblings = requireSiblingsOperations ( ruleId , context ) ;
113
104
const fragmentsToAdd : FragmentDefinitionNode [ ] = [ ] ;
114
105
115
- for ( const missingFragment of missingFragments ) {
116
- const [ fragmentName , fragmentTypeName ] = missingFragment . split ( ':' ) ;
117
- const [ foundFragment ] = siblings
118
- . getFragment ( fragmentName )
119
- . map ( source => source . document )
120
- . filter ( fragment => fragment . typeCondition . name . value === fragmentTypeName ) ;
106
+ for ( const fragmentName of missingFragments ) {
107
+ const [ foundFragment ] = siblings . getFragment ( fragmentName ) . map ( source => source . document ) ;
121
108
if ( foundFragment ) {
122
109
fragmentsToAdd . push ( foundFragment ) ;
123
110
}
@@ -128,7 +115,6 @@ const handleMissingFragments: GetDocumentNode = ({ ruleId, context, schema, node
128
115
return handleMissingFragments ( {
129
116
ruleId,
130
117
context,
131
- schema,
132
118
node : {
133
119
kind : Kind . DOCUMENT ,
134
120
definitions : [ ...node . definitions , ...fragmentsToAdd ] ,
@@ -182,7 +168,7 @@ const validationToRule = (
182
168
const schema = docs . requiresSchema ? requireGraphQLSchemaFromContext ( ruleId , context ) : null ;
183
169
184
170
const documentNode = getDocumentNode
185
- ? getDocumentNode ( { ruleId, context, schema , node : node . rawNode ( ) } )
171
+ ? getDocumentNode ( { ruleId, context, node : node . rawNode ( ) } )
186
172
: node . rawNode ( ) ;
187
173
188
174
validateDocument ( context , schema , documentNode , ruleFn ) ;
@@ -360,7 +346,7 @@ export const GRAPHQL_JS_VALIDATIONS: Record<string, GraphQLESLintRule> = Object.
360
346
requiresSchema : true ,
361
347
requiresSiblings : true ,
362
348
} ,
363
- ( { ruleId, context, schema , node } ) => {
349
+ ( { ruleId, context, node } ) => {
364
350
const siblings = requireSiblingsOperations ( ruleId , context ) ;
365
351
const FilePathToDocumentsMap = [ ...siblings . getOperations ( ) , ...siblings . getFragments ( ) ] . reduce <
366
352
Record < string , ( OperationDefinitionNode | FragmentDefinitionNode ) [ ] >
@@ -371,15 +357,15 @@ export const GRAPHQL_JS_VALIDATIONS: Record<string, GraphQLESLintRule> = Object.
371
357
} , Object . create ( null ) ) ;
372
358
373
359
const getParentNode = ( currentFilePath : string , node : DocumentNode ) : DocumentNode => {
374
- const { fragmentDefs } = getFragmentDefsAndFragmentSpreads ( schema , node ) ;
360
+ const { fragmentDefs } = getFragmentDefsAndFragmentSpreads ( node ) ;
375
361
if ( fragmentDefs . size === 0 ) {
376
362
return node ;
377
363
}
378
364
// skip iteration over documents for current filepath
379
365
delete FilePathToDocumentsMap [ currentFilePath ] ;
380
366
381
367
for ( const [ filePath , documents ] of Object . entries ( FilePathToDocumentsMap ) ) {
382
- const missingFragments = getMissingFragments ( schema , {
368
+ const missingFragments = getMissingFragments ( {
383
369
kind : Kind . DOCUMENT ,
384
370
definitions : documents ,
385
371
} ) ;
0 commit comments