Skip to content

Commit 276ed06

Browse files
committed
Add support for appliedDirectives in extendSchema
1 parent 4c3186c commit 276ed06

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/utilities/extendSchema.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
GraphQLScalarType,
2929
GraphQLEnumType,
3030
GraphQLInputObjectType,
31+
GraphQLAppliedDirectives,
3132
assertInputType,
3233
assertOutputType,
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,38 @@ 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,
265+
appliedDirectives: schema.getAppliedDirectives(),
255266
});
256267

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

@@ -349,6 +375,7 @@ export function extendSchema(
349375
description: type.description,
350376
interfaces: () => extendImplementedInterfaces(type),
351377
fields: () => extendFieldMap(type),
378+
appliedDirectives: type.appliedDirectives,
352379
isTypeOf: type.isTypeOf,
353380
});
354381
}
@@ -360,6 +387,7 @@ export function extendSchema(
360387
name: type.name,
361388
description: type.description,
362389
fields: () => extendFieldMap(type),
390+
appliedDirectives: type.appliedDirectives,
363391
resolveType: type.resolveType,
364392
});
365393
}
@@ -369,6 +397,7 @@ export function extendSchema(
369397
name: type.name,
370398
description: type.description,
371399
types: type.getTypes().map(getTypeFromDef),
400+
appliedDirectives: type.appliedDirectives,
372401
resolveType: type.resolveType,
373402
});
374403
}
@@ -409,6 +438,7 @@ export function extendSchema(
409438
deprecationReason: field.deprecationReason,
410439
type: extendFieldType(field.type),
411440
args: keyMap(field.args, arg => arg.name),
441+
appliedDirectives: field.appliedDirectives,
412442
resolve: field.resolve,
413443
};
414444
});
@@ -431,6 +461,7 @@ export function extendSchema(
431461
type: buildOutputFieldType(field.type),
432462
args: buildInputValues(field.arguments),
433463
deprecationReason: getDeprecationReason(field.directives),
464+
appliedDirectives: makeAppliedDirectives(field.directives),
434465
};
435466
});
436467
});
@@ -467,6 +498,7 @@ export function extendSchema(
467498
description: getDescription(typeNode),
468499
interfaces: () => buildImplementedInterfaces(typeNode),
469500
fields: () => buildFieldMap(typeNode),
501+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
470502
});
471503
}
472504

@@ -475,6 +507,7 @@ export function extendSchema(
475507
name: typeNode.name.value,
476508
description: getDescription(typeNode),
477509
fields: () => buildFieldMap(typeNode),
510+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
478511
resolveType: cannotExecuteExtendedSchema,
479512
});
480513
}
@@ -484,6 +517,7 @@ export function extendSchema(
484517
name: typeNode.name.value,
485518
description: getDescription(typeNode),
486519
types: typeNode.types.map(getObjectTypeFromAST),
520+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
487521
resolveType: cannotExecuteExtendedSchema,
488522
});
489523
}
@@ -492,6 +526,7 @@ export function extendSchema(
492526
return new GraphQLScalarType({
493527
name: typeNode.name.value,
494528
description: getDescription(typeNode),
529+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
495530
serialize: id => id,
496531
// Note: validation calls the parse functions to determine if a
497532
// literal value is correct. Returning null would cause use of custom
@@ -512,8 +547,10 @@ export function extendSchema(
512547
enumValue => ({
513548
description: getDescription(enumValue),
514549
deprecationReason: getDeprecationReason(enumValue.directives),
550+
appliedDirectives: makeAppliedDirectives(enumValue.directives),
515551
}),
516552
),
553+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
517554
});
518555
}
519556

@@ -522,6 +559,7 @@ export function extendSchema(
522559
name: typeNode.name.value,
523560
description: getDescription(typeNode),
524561
fields: () => buildInputValues(typeNode.fields),
562+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
525563
});
526564
}
527565

@@ -552,6 +590,7 @@ export function extendSchema(
552590
description: getDescription(field),
553591
args: buildInputValues(field.arguments),
554592
deprecationReason: getDeprecationReason(field.directives),
593+
appliedDirectives: makeAppliedDirectives(field.directives),
555594
})
556595
);
557596
}
@@ -565,7 +604,8 @@ export function extendSchema(
565604
return {
566605
type,
567606
description: getDescription(value),
568-
defaultValue: valueFromAST(value.defaultValue, type)
607+
defaultValue: valueFromAST(value.defaultValue, type),
608+
appliedDirectives: makeAppliedDirectives(value.directives),
569609
};
570610
}
571611
);

0 commit comments

Comments
 (0)