-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use reference-equality when omitting validation rules during composit…
…ion. The previous technique for deciding which validation rules from `specifiedSDLRules` to by-pass during composition was leveraging a string-comparison against `Function.prototype.name`. As shown in #3335, that technique breaks down under minification, when function names are often munged to shorter alternatives. As an alternative, we can import the rules and check them for reference equality with greater success, since those will not be affected by minification. While the actual bug in #3335 was _not_ in this code, this code poses the same hazard and would likely be affected as well (eventually, at least).
- Loading branch information
Showing
1 changed file
with
48 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,27 +1,58 @@ | ||
import { specifiedSDLRules } from 'graphql/validation/specifiedRules'; | ||
|
||
/** | ||
* Note that, because this module has overlapping names from the two modules | ||
* (graphql-js and our own, local validation rules) which we are importing from | ||
* we intentionally are very explicit about the suffixes of imported members | ||
* here, in order to make the intention clear. | ||
* | ||
* First, we'll import validation rules from graphql-js which we'll omit and | ||
* replace with our own validation rules. Note that we'll take alias each of | ||
* these to the same name with 'FromGraphqlJs' suffixes. | ||
*/ | ||
import { | ||
UniqueTypeNamesWithFields, | ||
MatchingEnums, | ||
PossibleTypeExtensions, | ||
UniqueFieldDefinitionNames, | ||
UniqueUnionTypes, | ||
} from './validate/sdl'; | ||
UniqueDirectivesPerLocation as UniqueDirectivesPerLocationFromGraphqlJs, | ||
} from 'graphql/validation/rules/UniqueDirectivesPerLocation'; | ||
import { | ||
UniqueTypeNames as UniqueTypeNamesFromGraphqlJs, | ||
} from 'graphql/validation/rules/UniqueTypeNames'; | ||
import { | ||
UniqueEnumValueNames as UniqueEnumValueNamesFromGraphqlJs, | ||
} from 'graphql/validation/rules/UniqueEnumValueNames'; | ||
import { | ||
PossibleTypeExtensions as PossibleTypeExtensionsFromGraphqlJs, | ||
} from 'graphql/validation/rules/PossibleTypeExtensions'; | ||
import { | ||
UniqueFieldDefinitionNames as UniqueFieldDefinitionNamesFromGraphqlJs, | ||
} from 'graphql/validation/rules/UniqueFieldDefinitionNames'; | ||
|
||
/** | ||
* Then, we'll import our own validation rules to take the place of those that | ||
* we'll be customizing, taking care to alias them all to the same name with | ||
* "FromComposition" suffixes. | ||
*/ | ||
import { | ||
UniqueTypeNamesWithFields as UniqueTypeNamesWithFieldsFromComposition, | ||
MatchingEnums as MatchingEnumsFromComposition, | ||
PossibleTypeExtensions as PossibleTypeExtensionsFromComposition, | ||
UniqueFieldDefinitionNames as UniqueFieldDefinitionsNamesFromComposition, | ||
UniqueUnionTypes as UniqueUnionTypesFromComposition, | ||
} from './validate/sdl'; | ||
|
||
const omit = [ | ||
'UniqueDirectivesPerLocation', | ||
'UniqueTypeNames', | ||
'UniqueEnumValueNames', | ||
'PossibleTypeExtensions', | ||
'UniqueFieldDefinitionNames', | ||
UniqueDirectivesPerLocationFromGraphqlJs, | ||
UniqueTypeNamesFromGraphqlJs, | ||
UniqueEnumValueNamesFromGraphqlJs, | ||
PossibleTypeExtensionsFromGraphqlJs, | ||
UniqueFieldDefinitionNamesFromGraphqlJs, | ||
]; | ||
|
||
export const compositionRules = specifiedSDLRules | ||
.filter(rule => !omit.includes(rule.name)) | ||
.filter(rule => !omit.includes(rule)) | ||
.concat([ | ||
UniqueFieldDefinitionNames, | ||
UniqueTypeNamesWithFields, | ||
MatchingEnums, | ||
UniqueUnionTypes, | ||
PossibleTypeExtensions, | ||
UniqueFieldDefinitionsNamesFromComposition, | ||
UniqueTypeNamesWithFieldsFromComposition, | ||
MatchingEnumsFromComposition, | ||
UniqueUnionTypesFromComposition, | ||
PossibleTypeExtensionsFromComposition, | ||
]); |