Skip to content

Commit

Permalink
fix(utils): print specifiedBy directive definitions correctly (#2616)
Browse files Browse the repository at this point in the history
* changeset

* fix(utils): print specifiedBy directive definitions correctly
  • Loading branch information
ardatan committed Feb 18, 2021
1 parent 181a853 commit 98134f9
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 13 deletions.
5 changes: 5 additions & 0 deletions .changeset/heavy-peaches-search.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': patch
---

fix(utils): fix missing default value of input object type field
5 changes: 5 additions & 0 deletions .changeset/mighty-tips-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': patch
---

fix(utils): print specifiedBy directive definitions correctly
5 changes: 5 additions & 0 deletions .changeset/sweet-humans-draw.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@graphql-tools/utils': minor
---

enhance(utils): Extract getDocumentNodeFromSchema from printSchemaWithDirectives
7 changes: 7 additions & 0 deletions .changeset/young-chairs-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
'@graphql-tools/load': patch
'@graphql-tools/module-loader': patch
'@graphql-tools/merge': patch
---

enhance(load/module-loader/merge): use getDocumentNodeFromSchema instead of parse and printSchemaWithDirectives together
55 changes: 42 additions & 13 deletions packages/utils/src/print-schema-with-directives.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import {
InputValueDefinitionNode,
GraphQLArgument,
EnumValueDefinitionNode,
isSpecifiedDirective,
GraphQLDirective,
DirectiveDefinitionNode,
astFromValue,
Expand Down Expand Up @@ -93,10 +92,6 @@ export function getDocumentNodeFromSchema(

const directives = schema.getDirectives();
for (const directive of directives) {
if (isSpecifiedDirective(directive)) {
continue;
}

definitions.push(astFromDirective(directive, schema, pathToDirectivesInExtensions));
}

Expand Down Expand Up @@ -242,7 +237,7 @@ export function getDirectiveNodes(

let directives: Array<DirectiveNode>;
if (directivesInExtensions != null) {
directives = makeDirectives(schema, directivesInExtensions);
directives = makeDirectiveNodes(schema, directivesInExtensions);
} else {
directives = [].concat(...nodes.filter(node => node.directives != null).map(node => node.directives));
}
Expand All @@ -262,7 +257,7 @@ export function getDeprecatableDirectiveNodes(

let directives: ReadonlyArray<DirectiveNode>;
if (directivesInExtensions != null) {
directives = makeDirectives(schema, directivesInExtensions);
directives = makeDirectiveNodes(schema, directivesInExtensions);
} else {
directives = entity.astNode?.directives;
}
Expand Down Expand Up @@ -449,6 +444,36 @@ export function astFromScalarType(
schema: GraphQLSchema,
pathToDirectivesInExtensions: Array<string>
): ScalarTypeDefinitionNode {
let directiveNodesBesidesSpecifiedBy: Array<DirectiveNode> = [];
let specifiedByDirectiveNode: DirectiveNode;

const directivesInExtensions = getDirectivesInExtensions(type, pathToDirectivesInExtensions);

let allDirectives: ReadonlyArray<DirectiveNode>;
if (directivesInExtensions != null) {
allDirectives = makeDirectiveNodes(schema, directivesInExtensions);
} else {
allDirectives = type.astNode?.directives;
}

if (allDirectives != null) {
directiveNodesBesidesSpecifiedBy = allDirectives.filter(directive => directive.name.value !== 'specifiedBy');
if (((type as unknown) as { specifiedByUrl: string }).specifiedByUrl != null) {
specifiedByDirectiveNode = allDirectives.filter(directive => directive.name.value === 'specifiedBy')?.[0];
}
}

if (((type as unknown) as { specifiedByUrl: string }).specifiedByUrl != null && specifiedByDirectiveNode == null) {
specifiedByDirectiveNode = makeDirectiveNode('specifiedBy', {
url: ((type as unknown) as { specifiedByUrl: string }).specifiedByUrl,
});
}

const directives =
specifiedByDirectiveNode == null
? directiveNodesBesidesSpecifiedBy
: [specifiedByDirectiveNode].concat(directiveNodesBesidesSpecifiedBy);

return {
kind: Kind.SCALAR_TYPE_DEFINITION,
description:
Expand All @@ -463,7 +488,7 @@ export function astFromScalarType(
kind: Kind.NAME,
value: type.name,
},
directives: getDirectiveNodes(type, schema, pathToDirectivesInExtensions),
directives,
};
}

Expand Down Expand Up @@ -541,10 +566,14 @@ export function astFromEnumValue(
}

export function makeDeprecatedDirective(deprecationReason: string): DirectiveNode {
return makeDirective('deprecated', { reason: deprecationReason }, GraphQLDeprecatedDirective);
return makeDirectiveNode('deprecated', { reason: deprecationReason }, GraphQLDeprecatedDirective);
}

export function makeDirective(name: string, args: Record<string, any>, directive: GraphQLDirective): DirectiveNode {
export function makeDirectiveNode(
name: string,
args: Record<string, any>,
directive?: GraphQLDirective
): DirectiveNode {
const directiveArguments: Array<ArgumentNode> = [];

if (directive != null) {
Expand Down Expand Up @@ -585,16 +614,16 @@ export function makeDirective(name: string, args: Record<string, any>, directive
};
}

export function makeDirectives(schema: GraphQLSchema, directiveValues: Record<string, any>): Array<DirectiveNode> {
export function makeDirectiveNodes(schema: GraphQLSchema, directiveValues: Record<string, any>): Array<DirectiveNode> {
const directiveNodes: Array<DirectiveNode> = [];
Object.entries(directiveValues).forEach(([directiveName, arrayOrSingleValue]) => {
const directive = schema.getDirective(directiveName);
if (Array.isArray(arrayOrSingleValue)) {
arrayOrSingleValue.forEach(value => {
directiveNodes.push(makeDirective(directiveName, value, directive));
directiveNodes.push(makeDirectiveNode(directiveName, value, directive));
});
} else {
directiveNodes.push(makeDirective(directiveName, arrayOrSingleValue, directive));
directiveNodes.push(makeDirectiveNode(directiveName, arrayOrSingleValue, directive));
}
});
return directiveNodes;
Expand Down
3 changes: 3 additions & 0 deletions packages/utils/tests/print-schema-with-directives.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,11 +297,14 @@ describe('printSchemaWithDirectives', () => {
directive @INPUT_OBJECT on INPUT_OBJECT
directive @INPUT_FIELD_DEFINITION on INPUT_FIELD_DEFINITION
directive @specifiedBy(url: String!) on SCALAR
schema @SCHEMA {
query: SomeType
}
scalar DateTime @SCALAR
scalar JSONObject @specifiedBy(url: "http://www.ecma-international.org/publications/files/ECMA-ST/ECMA-404.pdf")
type SomeType @OBJECT {
someField(someArg: Int! @ARGUMENT_DEFINITION): String @FIELD_DEFINITION
Expand Down

0 comments on commit 98134f9

Please sign in to comment.