Skip to content

Commit 219ed39

Browse files
committedFeb 18, 2021
fix(utils): print specifiedBy directive definitions correctly (#2616)
* changeset * fix(utils): print specifiedBy directive definitions correctly
1 parent 181a853 commit 219ed39

5 files changed

+64
-8
lines changed
 

‎.changeset/heavy-peaches-search.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/utils': patch
3+
---
4+
5+
fix(utils): fix missing default value of input object type field

‎.changeset/mighty-tips-notice.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/utils': patch
3+
---
4+
5+
fix(utils): print specifiedBy directive definitions correctly

‎.changeset/sweet-humans-draw.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@graphql-tools/utils': minor
3+
---
4+
5+
enhance(utils): Extract getDocumentNodeFromSchema from printSchemaWithDirectives

‎.changeset/young-chairs-float.md

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
'@graphql-tools/load': patch
3+
'@graphql-tools/module-loader': patch
4+
'@graphql-tools/merge': patch
5+
---
6+
7+
enhance(load/module-loader/merge): use getDocumentNodeFromSchema instead of parse and printSchemaWithDirectives together

‎packages/utils/src/print-schema-with-directives.ts

+42-8
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ export function getDirectiveNodes(
242242

243243
let directives: Array<DirectiveNode>;
244244
if (directivesInExtensions != null) {
245-
directives = makeDirectives(schema, directivesInExtensions);
245+
directives = makeDirectiveNodes(schema, directivesInExtensions);
246246
} else {
247247
directives = [].concat(...nodes.filter(node => node.directives != null).map(node => node.directives));
248248
}
@@ -262,7 +262,7 @@ export function getDeprecatableDirectiveNodes(
262262

263263
let directives: ReadonlyArray<DirectiveNode>;
264264
if (directivesInExtensions != null) {
265-
directives = makeDirectives(schema, directivesInExtensions);
265+
directives = makeDirectiveNodes(schema, directivesInExtensions);
266266
} else {
267267
directives = entity.astNode?.directives;
268268
}
@@ -449,6 +449,36 @@ export function astFromScalarType(
449449
schema: GraphQLSchema,
450450
pathToDirectivesInExtensions: Array<string>
451451
): ScalarTypeDefinitionNode {
452+
let directiveNodesBesidesSpecifiedBy: Array<DirectiveNode> = [];
453+
let specifiedByDirectiveNode: DirectiveNode;
454+
455+
const directivesInExtensions = getDirectivesInExtensions(type, pathToDirectivesInExtensions);
456+
457+
let allDirectives: ReadonlyArray<DirectiveNode>;
458+
if (directivesInExtensions != null) {
459+
allDirectives = makeDirectiveNodes(schema, directivesInExtensions);
460+
} else {
461+
allDirectives = type.astNode?.directives;
462+
}
463+
464+
if (allDirectives != null) {
465+
directiveNodesBesidesSpecifiedBy = allDirectives.filter(directive => directive.name.value !== 'specifiedBy');
466+
if (((type as unknown) as { specifiedByUrl: string }).specifiedByUrl != null) {
467+
specifiedByDirectiveNode = allDirectives.filter(directive => directive.name.value === 'specifiedBy')?.[0];
468+
}
469+
}
470+
471+
if (((type as unknown) as { specifiedByUrl: string }).specifiedByUrl != null && specifiedByDirectiveNode == null) {
472+
specifiedByDirectiveNode = makeDirectiveNode('specifiedBy', {
473+
url: ((type as unknown) as { specifiedByUrl: string }).specifiedByUrl,
474+
});
475+
}
476+
477+
const directives =
478+
specifiedByDirectiveNode == null
479+
? directiveNodesBesidesSpecifiedBy
480+
: [specifiedByDirectiveNode].concat(directiveNodesBesidesSpecifiedBy);
481+
452482
return {
453483
kind: Kind.SCALAR_TYPE_DEFINITION,
454484
description:
@@ -463,7 +493,7 @@ export function astFromScalarType(
463493
kind: Kind.NAME,
464494
value: type.name,
465495
},
466-
directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
496+
directives,
467497
};
468498
}
469499

@@ -541,10 +571,14 @@ export function astFromEnumValue(
541571
}
542572

543573
export function makeDeprecatedDirective(deprecationReason: string): DirectiveNode {
544-
return makeDirective('deprecated', { reason: deprecationReason }, GraphQLDeprecatedDirective);
574+
return makeDirectiveNode('deprecated', { reason: deprecationReason }, GraphQLDeprecatedDirective);
545575
}
546576

547-
export function makeDirective(name: string, args: Record<string, any>, directive: GraphQLDirective): DirectiveNode {
577+
export function makeDirectiveNode(
578+
name: string,
579+
args: Record<string, any>,
580+
directive?: GraphQLDirective
581+
): DirectiveNode {
548582
const directiveArguments: Array<ArgumentNode> = [];
549583

550584
if (directive != null) {
@@ -585,16 +619,16 @@ export function makeDirective(name: string, args: Record<string, any>, directive
585619
};
586620
}
587621

588-
export function makeDirectives(schema: GraphQLSchema, directiveValues: Record<string, any>): Array<DirectiveNode> {
622+
export function makeDirectiveNodes(schema: GraphQLSchema, directiveValues: Record<string, any>): Array<DirectiveNode> {
589623
const directiveNodes: Array<DirectiveNode> = [];
590624
Object.entries(directiveValues).forEach(([directiveName, arrayOrSingleValue]) => {
591625
const directive = schema.getDirective(directiveName);
592626
if (Array.isArray(arrayOrSingleValue)) {
593627
arrayOrSingleValue.forEach(value => {
594-
directiveNodes.push(makeDirective(directiveName, value, directive));
628+
directiveNodes.push(makeDirectiveNode(directiveName, value, directive));
595629
});
596630
} else {
597-
directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
631+
directiveNodes.push(makeDirectiveNode(directiveName, arrayOrSingleValue, directive));
598632
}
599633
});
600634
return directiveNodes;

0 commit comments

Comments
 (0)
Please sign in to comment.