Skip to content

Commit 0c32174

Browse files
committed
keep directives in their own map (#714)
prevents name-collision with types
1 parent f7f5245 commit 0c32174

1 file changed

Lines changed: 23 additions & 5 deletions

File tree

packages/apollo-tools/src/buildServiceDefinition.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,17 @@ export function buildServiceDefinition(
3535
const errors: GraphQLError[] = [];
3636

3737
const typeDefinitionsMap: {
38-
[name: string]: (TypeDefinitionNode | DirectiveDefinitionNode)[];
38+
[name: string]: TypeDefinitionNode[];
3939
} = Object.create(null);
4040

4141
const typeExtensionsMap: {
4242
[name: string]: TypeExtensionNode[];
4343
} = Object.create(null);
4444

45+
const directivesMap: {
46+
[name: string]: DirectiveDefinitionNode[];
47+
} = Object.create(null);
48+
4549
const schemaDefinitions: SchemaDefinitionNode[] = [];
4650
const schemaExtensions: SchemaExtensionNode[] = [];
4751

@@ -50,10 +54,7 @@ export function buildServiceDefinition(
5054
module = { typeDefs: module };
5155
}
5256
for (const definition of module.typeDefs.definitions) {
53-
if (
54-
isTypeDefinitionNode(definition) ||
55-
definition.kind === Kind.DIRECTIVE_DEFINITION
56-
) {
57+
if (isTypeDefinitionNode(definition)) {
5758
const typeName = definition.name.value;
5859

5960
if (typeDefinitionsMap[typeName]) {
@@ -69,6 +70,14 @@ export function buildServiceDefinition(
6970
} else {
7071
typeExtensionsMap[typeName] = [definition];
7172
}
73+
} else if (definition.kind === Kind.DIRECTIVE_DEFINITION) {
74+
const typeName = definition.name.value;
75+
76+
if (directivesMap[typeName]) {
77+
directivesMap[typeName].push(definition);
78+
} else {
79+
directivesMap[typeName] = [definition];
80+
}
7281
} else if (definition.kind === Kind.SCHEMA_DEFINITION) {
7382
schemaDefinitions.push(definition);
7483
} else if (definition.kind === Kind.SCHEMA_EXTENSION) {
@@ -174,6 +183,15 @@ export function buildServiceDefinition(
174183
});
175184
}
176185

186+
const directiveExtensions = Object.values(directivesMap).flat();
187+
188+
if (directiveExtensions.length > 0) {
189+
schema = extendSchema(schema, {
190+
kind: Kind.DOCUMENT,
191+
definitions: directiveExtensions
192+
});
193+
}
194+
177195
for (const module of modules) {
178196
if (!module.resolvers) continue;
179197

0 commit comments

Comments
 (0)