Skip to content

Commit

Permalink
Use reference-equality when omitting validation rules during composit…
Browse files Browse the repository at this point in the history
…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
abernix committed Sep 25, 2019
1 parent 7132f6b commit 706c3ee
Showing 1 changed file with 48 additions and 17 deletions.
65 changes: 48 additions & 17 deletions packages/apollo-federation/src/composition/rules.ts
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,
]);

0 comments on commit 706c3ee

Please sign in to comment.