Skip to content

Commit 0bad61d

Browse files
authored
Use reference-equality when omitting validation rules during composition. (#3338)
* Use reference-equality when omitting validation rules during composition. 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). * Add changelog entry
1 parent 7132f6b commit 0bad61d

2 files changed

Lines changed: 51 additions & 17 deletions

File tree

packages/apollo-federation/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
> The changes noted within this `vNEXT` section have not been released yet. New PRs and commits which introduce changes should include an entry in this `vNEXT` section as part of their development. When a release is being prepared, a new header will be (manually) created below and the the appropriate changes within that release will be moved into the new section.
66
7+
- Use reference-equality when omitting validation rules during composition. [#3338](https://github.com/apollographql/apollo-server/pull/3338)
8+
79
### v0.10.0
810

911
> [See complete versioning details.](https://github.com/apollographql/apollo-server/commit/6100fb5e0797cd1f578ded7cb77b60fac47e58e3)
Lines changed: 49 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,59 @@
11
import { specifiedSDLRules } from 'graphql/validation/specifiedRules';
22

3+
/**
4+
* Since this module has overlapping names in the two modules (graphql-js and
5+
* our own, local validation rules) which we are importing from, we
6+
* intentionally are very explicit about the suffixes of imported members here,
7+
* so that the intention is clear.
8+
*
9+
* First, we'll import validation rules from graphql-js which we'll omit and
10+
* replace with our own validation rules. As noted above, we'll use aliases
11+
* with 'FromGraphqlJs' suffixes for clarity.
12+
*/
13+
14+
import {
15+
UniqueDirectivesPerLocation as UniqueDirectivesPerLocationFromGraphqlJs,
16+
} from 'graphql/validation/rules/UniqueDirectivesPerLocation';
17+
import {
18+
UniqueTypeNames as UniqueTypeNamesFromGraphqlJs,
19+
} from 'graphql/validation/rules/UniqueTypeNames';
20+
import {
21+
UniqueEnumValueNames as UniqueEnumValueNamesFromGraphqlJs,
22+
} from 'graphql/validation/rules/UniqueEnumValueNames';
23+
import {
24+
PossibleTypeExtensions as PossibleTypeExtensionsFromGraphqlJs,
25+
} from 'graphql/validation/rules/PossibleTypeExtensions';
26+
import {
27+
UniqueFieldDefinitionNames as UniqueFieldDefinitionNamesFromGraphqlJs,
28+
} from 'graphql/validation/rules/UniqueFieldDefinitionNames';
29+
30+
/**
31+
* Then, we'll import our own validation rules to take the place of those that
32+
* we'll be customizing, taking care to alias them all to the same name with
33+
* "FromComposition" suffixes.
34+
*/
335
import {
4-
UniqueTypeNamesWithFields,
5-
MatchingEnums,
6-
PossibleTypeExtensions,
7-
UniqueFieldDefinitionNames,
8-
UniqueUnionTypes,
9-
} from './validate/sdl';
36+
UniqueTypeNamesWithFields as UniqueTypeNamesWithFieldsFromComposition,
37+
MatchingEnums as MatchingEnumsFromComposition,
38+
PossibleTypeExtensions as PossibleTypeExtensionsFromComposition,
39+
UniqueFieldDefinitionNames as UniqueFieldDefinitionsNamesFromComposition,
40+
UniqueUnionTypes as UniqueUnionTypesFromComposition,
41+
} from './validate/sdl';
1042

1143
const omit = [
12-
'UniqueDirectivesPerLocation',
13-
'UniqueTypeNames',
14-
'UniqueEnumValueNames',
15-
'PossibleTypeExtensions',
16-
'UniqueFieldDefinitionNames',
44+
UniqueDirectivesPerLocationFromGraphqlJs,
45+
UniqueTypeNamesFromGraphqlJs,
46+
UniqueEnumValueNamesFromGraphqlJs,
47+
PossibleTypeExtensionsFromGraphqlJs,
48+
UniqueFieldDefinitionNamesFromGraphqlJs,
1749
];
1850

1951
export const compositionRules = specifiedSDLRules
20-
.filter(rule => !omit.includes(rule.name))
52+
.filter(rule => !omit.includes(rule))
2153
.concat([
22-
UniqueFieldDefinitionNames,
23-
UniqueTypeNamesWithFields,
24-
MatchingEnums,
25-
UniqueUnionTypes,
26-
PossibleTypeExtensions,
54+
UniqueFieldDefinitionsNamesFromComposition,
55+
UniqueTypeNamesWithFieldsFromComposition,
56+
MatchingEnumsFromComposition,
57+
UniqueUnionTypesFromComposition,
58+
PossibleTypeExtensionsFromComposition,
2759
]);

0 commit comments

Comments
 (0)