Skip to content

Commit cc66527

Browse files
committed
Add support for appliedDirectives in extendSchema
1 parent f4843f8 commit cc66527

File tree

2 files changed

+46
-13
lines changed

2 files changed

+46
-13
lines changed

src/utilities/__tests__/extendSchema-test.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -213,17 +213,10 @@ union SomeUnion = Foo | Biz
213213
expect(deprecatedFieldDef.isDeprecated).to.equal(true);
214214
expect(deprecatedFieldDef.deprecationReason).to.equal('not used anymore');
215215

216-
const deprecatedEnumDef = extendedSchema
217-
.getType('EnumWithDeprecatedValue');
218-
expect(deprecatedEnumDef.getValues()).to.deep.equal([
219-
{
220-
name: 'DEPRECATED',
221-
description: '',
222-
isDeprecated: true,
223-
deprecationReason: 'do not use',
224-
value: 'DEPRECATED'
225-
}
226-
]);
216+
const deprecatedEnumValueDef = extendedSchema
217+
.getType('EnumWithDeprecatedValue').getValues()[0];
218+
expect(deprecatedEnumValueDef.isDeprecated).to.equal(true);
219+
expect(deprecatedEnumValueDef.deprecationReason).to.equal('do not use');
227220
});
228221

229222
it('extends objects with deprecated fields', () => {

src/utilities/extendSchema.js

+42-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,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

@@ -353,6 +379,7 @@ export function extendSchema(
353379
description: type.description,
354380
interfaces: () => extendImplementedInterfaces(type),
355381
fields: () => extendFieldMap(type),
382+
appliedDirectives: type.appliedDirectives,
356383
isTypeOf: type.isTypeOf,
357384
});
358385
}
@@ -364,6 +391,7 @@ export function extendSchema(
364391
name: type.name,
365392
description: type.description,
366393
fields: () => extendFieldMap(type),
394+
appliedDirectives: type.appliedDirectives,
367395
resolveType: type.resolveType,
368396
});
369397
}
@@ -373,6 +401,7 @@ export function extendSchema(
373401
name: type.name,
374402
description: type.description,
375403
types: type.getTypes().map(getTypeFromDef),
404+
appliedDirectives: type.appliedDirectives,
376405
resolveType: type.resolveType,
377406
});
378407
}
@@ -413,6 +442,7 @@ export function extendSchema(
413442
deprecationReason: field.deprecationReason,
414443
type: extendFieldType(field.type),
415444
args: keyMap(field.args, arg => arg.name),
445+
appliedDirectives: field.appliedDirectives,
416446
resolve: field.resolve,
417447
};
418448
});
@@ -435,6 +465,7 @@ export function extendSchema(
435465
type: buildOutputFieldType(field.type),
436466
args: buildInputValues(field.arguments),
437467
deprecationReason: getDeprecationReason(field.directives),
468+
appliedDirectives: makeAppliedDirectives(field.directives),
438469
};
439470
});
440471
});
@@ -471,6 +502,7 @@ export function extendSchema(
471502
description: getDescription(typeNode),
472503
interfaces: () => buildImplementedInterfaces(typeNode),
473504
fields: () => buildFieldMap(typeNode),
505+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
474506
});
475507
}
476508

@@ -479,6 +511,7 @@ export function extendSchema(
479511
name: typeNode.name.value,
480512
description: getDescription(typeNode),
481513
fields: () => buildFieldMap(typeNode),
514+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
482515
resolveType: cannotExecuteExtendedSchema,
483516
});
484517
}
@@ -488,6 +521,7 @@ export function extendSchema(
488521
name: typeNode.name.value,
489522
description: getDescription(typeNode),
490523
types: typeNode.types.map(getObjectTypeFromAST),
524+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
491525
resolveType: cannotExecuteExtendedSchema,
492526
});
493527
}
@@ -496,6 +530,7 @@ export function extendSchema(
496530
return new GraphQLScalarType({
497531
name: typeNode.name.value,
498532
description: getDescription(typeNode),
533+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
499534
serialize: id => id,
500535
// Note: validation calls the parse functions to determine if a
501536
// literal value is correct. Returning null would cause use of custom
@@ -516,8 +551,10 @@ export function extendSchema(
516551
enumValue => ({
517552
description: getDescription(enumValue),
518553
deprecationReason: getDeprecationReason(enumValue.directives),
554+
appliedDirectives: makeAppliedDirectives(enumValue.directives),
519555
}),
520556
),
557+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
521558
});
522559
}
523560

@@ -526,6 +563,7 @@ export function extendSchema(
526563
name: typeNode.name.value,
527564
description: getDescription(typeNode),
528565
fields: () => buildInputValues(typeNode.fields),
566+
appliedDirectives: makeAppliedDirectives(typeNode.directives),
529567
});
530568
}
531569

@@ -556,6 +594,7 @@ export function extendSchema(
556594
description: getDescription(field),
557595
args: buildInputValues(field.arguments),
558596
deprecationReason: getDeprecationReason(field.directives),
597+
appliedDirectives: makeAppliedDirectives(field.directives),
559598
})
560599
);
561600
}
@@ -569,7 +608,8 @@ export function extendSchema(
569608
return {
570609
type,
571610
description: getDescription(value),
572-
defaultValue: valueFromAST(value.defaultValue, type)
611+
defaultValue: valueFromAST(value.defaultValue, type),
612+
appliedDirectives: makeAppliedDirectives(value.directives),
573613
};
574614
}
575615
);

0 commit comments

Comments
 (0)