Skip to content

Commit 63bc010

Browse files
committed
Add support for appliedDirectives in extendSchema
1 parent c1e55cb commit 63bc010

File tree

1 file changed

+41
-2
lines changed

1 file changed

+41
-2
lines changed

src/utilities/extendSchema.js

+41-2
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
GraphQLScalarType,
2929
GraphQLEnumType,
3030
GraphQLInputObjectType,
31+
GraphQLAppliedDirectives,
3132
isInputType,
3233
isOutputType,
3334
} from '../type/definition';
@@ -82,6 +83,7 @@ import type {
8283

8384
import type {
8485
DocumentNode,
86+
DirectiveNode,
8587
InputValueDefinitionNode,
8688
TypeNode,
8789
NamedTypeNode,
@@ -95,6 +97,7 @@ import type {
9597
DirectiveDefinitionNode,
9698
} from '../language/ast';
9799

100+
import { getArgumentValues } from '../execution/values';
98101

99102
/**
100103
* Produces a new schema given an existing schema and a document which may
@@ -245,15 +248,37 @@ export function extendSchema(
245248
types.push(getTypeFromAST(typeDefinitionMap[typeName]));
246249
});
247250

251+
const directives = getMergedDirectives();
252+
const innerDirectivesMap = keyValMap(
253+
directives,
254+
directive => directive.name,
255+
directive => directive
256+
);
257+
248258
// Then produce and return a Schema with these types.
249259
return new GraphQLSchema({
250260
query: queryType,
251261
mutation: mutationType,
252262
subscription: subscriptionType,
253263
types,
254-
directives: getMergedDirectives(),
264+
directives,
255265
});
256266

267+
function makeAppliedDirectives(appliedDirectives: ?Array<DirectiveNode>) {
268+
return appliedDirectives && new GraphQLAppliedDirectives(keyValMap(
269+
appliedDirectives,
270+
directive => directive.name.value,
271+
directive => (() => {
272+
const directiveName = directive.name.value;
273+
if (!innerDirectivesMap[directiveName]) {
274+
throw new Error(
275+
`Directive "${directiveName}" not found in document.`);
276+
}
277+
return getArgumentValues(innerDirectivesMap[directiveName], directive);
278+
})
279+
));
280+
}
281+
257282
// Below are functions used for producing this schema that have closed over
258283
// this scope and have access to the schema, cache, and newly defined types.
259284

@@ -353,6 +378,7 @@ export function extendSchema(
353378
description: type.description,
354379
interfaces: () => extendImplementedInterfaces(type),
355380
fields: () => extendFieldMap(type),
381+
appliedDirectives: type.appliedDirectives,
356382
isTypeOf: type.isTypeOf,
357383
});
358384
}
@@ -364,6 +390,7 @@ export function extendSchema(
364390
name: type.name,
365391
description: type.description,
366392
fields: () => extendFieldMap(type),
393+
appliedDirectives: type.appliedDirectives,
367394
resolveType: type.resolveType,
368395
});
369396
}
@@ -373,6 +400,7 @@ export function extendSchema(
373400
name: type.name,
374401
description: type.description,
375402
types: type.getTypes().map(getTypeFromDef),
403+
appliedDirectives: type.appliedDirectives,
376404
resolveType: type.resolveType,
377405
});
378406
}
@@ -413,6 +441,7 @@ export function extendSchema(
413441
deprecationReason: field.deprecationReason,
414442
type: extendFieldType(field.type),
415443
args: keyMap(field.args, arg => arg.name),
444+
appliedDirectives: field.appliedDirectives,
416445
resolve: field.resolve,
417446
};
418447
});
@@ -435,6 +464,7 @@ export function extendSchema(
435464
type: buildOutputFieldType(field.type),
436465
args: buildInputValues(field.arguments),
437466
deprecationReason: getDeprecationReason(field.directives),
467+
appliedDirectives: makeAppliedDirectives(field.directives),
438468
};
439469
});
440470
});
@@ -471,6 +501,7 @@ export function extendSchema(
471501
description: getDescription(typeNode),
472502
interfaces: () => buildImplementedInterfaces(typeNode),
473503
fields: () => buildFieldMap(typeNode),
504+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
474505
});
475506
}
476507

@@ -479,6 +510,7 @@ export function extendSchema(
479510
name: typeNode.name.value,
480511
description: getDescription(typeNode),
481512
fields: () => buildFieldMap(typeNode),
513+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
482514
resolveType: cannotExecuteExtendedSchema,
483515
});
484516
}
@@ -488,6 +520,7 @@ export function extendSchema(
488520
name: typeNode.name.value,
489521
description: getDescription(typeNode),
490522
types: typeNode.types.map(getObjectTypeFromAST),
523+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
491524
resolveType: cannotExecuteExtendedSchema,
492525
});
493526
}
@@ -496,6 +529,7 @@ export function extendSchema(
496529
return new GraphQLScalarType({
497530
name: typeNode.name.value,
498531
description: getDescription(typeNode),
532+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
499533
serialize: id => id,
500534
// Note: validation calls the parse functions to determine if a
501535
// literal value is correct. Returning null would cause use of custom
@@ -516,8 +550,10 @@ export function extendSchema(
516550
enumValue => ({
517551
description: getDescription(enumValue),
518552
deprecationReason: getDeprecationReason(enumValue.directives),
553+
appliedDirectives: makeAppliedDirectives(enumValue.directives),
519554
}),
520555
),
556+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
521557
});
522558
}
523559

@@ -526,6 +562,7 @@ export function extendSchema(
526562
name: typeNode.name.value,
527563
description: getDescription(typeNode),
528564
fields: () => buildInputValues(typeNode.fields),
565+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
529566
});
530567
}
531568

@@ -556,6 +593,7 @@ export function extendSchema(
556593
description: getDescription(field),
557594
args: buildInputValues(field.arguments),
558595
deprecationReason: getDeprecationReason(field.directives),
596+
appliedDirectives: makeAppliedDirectives(field.directives),
559597
})
560598
);
561599
}
@@ -569,7 +607,8 @@ export function extendSchema(
569607
return {
570608
type,
571609
description: getDescription(value),
572-
defaultValue: valueFromAST(value.defaultValue, type)
610+
defaultValue: valueFromAST(value.defaultValue, type),
611+
appliedDirectives: makeAppliedDirectives(value.directives),
573612
};
574613
}
575614
);

0 commit comments

Comments
 (0)