Skip to content

Commit 8f331aa

Browse files
authoredFeb 18, 2021
Small refactor and fixes in printSchemaWithDirectives and improvements in other packages (#2611)
* somework * changeset
1 parent 25f9a85 commit 8f331aa

13 files changed

+66
-32
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/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/load/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"graphql-type-json": "0.3.2"
2727
},
2828
"dependencies": {
29-
"@graphql-tools/utils": "^7.0.0",
29+
"@graphql-tools/utils": "^7.3.0",
3030
"@graphql-tools/merge": "^6.2.5",
3131
"globby": "11.0.2",
3232
"import-from": "3.0.0",

‎packages/load/src/load-typedefs/collect-sources.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { Source, isDocumentString, parseGraphQLSDL, asArray, printSchemaWithDirectives } from '@graphql-tools/utils';
2-
import { isSchema, Kind, parse } from 'graphql';
1+
import { Source, isDocumentString, parseGraphQLSDL, asArray, getDocumentNodeFromSchema } from '@graphql-tools/utils';
2+
import { isSchema, Kind } from 'graphql';
33
import isGlob from 'is-glob';
44
import { LoadTypedefsOptions } from '../load-typedefs';
55
import { loadFile, loadFileSync } from './load-file';
@@ -296,7 +296,7 @@ function addResultOfCustomLoader({
296296
source: {
297297
location: pointer,
298298
schema: result,
299-
document: parse(printSchemaWithDirectives(result)),
299+
document: getDocumentNodeFromSchema(result),
300300
},
301301
pointer,
302302
noCache: true,

‎packages/loaders/module/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
"graphql": "^14.0.0 || ^15.0.0"
2121
},
2222
"dependencies": {
23-
"@graphql-tools/utils": "^7.0.0",
23+
"@graphql-tools/utils": "^7.3.0",
2424
"tslib": "~2.1.0"
2525
},
2626
"publishConfig": {

‎packages/loaders/module/src/index.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { parse, isSchema } from 'graphql';
22
import {
33
UniversalLoader,
44
fixSchemaAst,
5-
printSchemaWithDirectives,
5+
getDocumentNodeFromSchema,
66
SingleFileOptions,
77
Source,
88
} from '@graphql-tools/utils';
@@ -86,7 +86,7 @@ export class ModuleLoader implements UniversalLoader {
8686
return {
8787
schema,
8888
get document() {
89-
return parse(printSchemaWithDirectives(schema, options));
89+
return getDocumentNodeFromSchema(schema);
9090
},
9191
location: pointer,
9292
};

‎packages/merge/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
},
2525
"dependencies": {
2626
"@graphql-tools/schema": "^7.0.0",
27-
"@graphql-tools/utils": "^7.0.0",
27+
"@graphql-tools/utils": "^7.3.0",
2828
"tslib": "~2.1.0"
2929
},
3030
"publishConfig": {

‎packages/merge/src/typedefs-mergers/merge-typedefs.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { DefinitionNode, DocumentNode, GraphQLSchema, parse, Source, Kind, isSch
22
import { isSourceTypes, isStringTypes, isSchemaDefinition } from './utils';
33
import { MergedResultMap, mergeGraphQLNodes } from './merge-nodes';
44
import { resetComments, printWithComments } from './comments';
5-
import { createSchemaDefinition, printSchemaWithDirectives } from '@graphql-tools/utils';
5+
import { createSchemaDefinition, getDocumentNodeFromSchema } from '@graphql-tools/utils';
66

77
type Omit<T, K extends keyof any> = Pick<T, Exclude<keyof T, K>>;
88
type CompareFn<T> = (a: T, b: T) => number;
@@ -115,7 +115,7 @@ export function mergeGraphQLTypes(
115115
type = mergeTypeDefs(type);
116116
}
117117
if (isSchema(type)) {
118-
return parse(printSchemaWithDirectives(type));
118+
return getDocumentNodeFromSchema(type);
119119
} else if (isStringTypes(type) || isSourceTypes(type)) {
120120
return parse(type);
121121
}

‎packages/utils/src/isDocumentNode.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { ASTNode, DocumentNode } from 'graphql';
1+
import { DocumentNode, Kind } from 'graphql';
22

33
export function isDocumentNode(object: any): object is DocumentNode {
4-
return (object as ASTNode).kind !== undefined;
4+
return object && typeof object === 'object' && 'kind' in object && object.kind === Kind.DOCUMENT;
55
}

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

+33-19
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,25 @@ import {
4545
GraphQLScalarType,
4646
ScalarTypeDefinitionNode,
4747
StringValueNode,
48+
DefinitionNode,
49+
DocumentNode,
4850
} from 'graphql';
49-
import { PrintSchemaWithDirectivesOptions } from './types';
51+
import { GetDocumentNodeFromSchemaOptions, PrintSchemaWithDirectivesOptions } from './types';
5052

5153
import { astFromType } from './astFromType';
5254
import { getDirectivesInExtensions } from './get-directives';
5355
import { astFromValueUntyped } from './astFromValueUntyped';
5456

55-
// this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
56-
// currently does not allow customization of printSchema options having to do with comments.
57-
export function printSchemaWithDirectives(
57+
export function getDocumentNodeFromSchema(
5858
schema: GraphQLSchema,
59-
options: PrintSchemaWithDirectivesOptions = {}
60-
): string {
59+
options: GetDocumentNodeFromSchemaOptions = {}
60+
): DocumentNode {
6161
const pathToDirectivesInExtensions = options.pathToDirectivesInExtensions;
6262

6363
const typesMap = schema.getTypeMap();
6464

6565
const schemaNode = astFromSchema(schema, pathToDirectivesInExtensions);
66-
const result: Array<string> = schemaNode != null ? [print(schemaNode)] : [];
66+
const definitions: Array<DefinitionNode> = schemaNode != null ? [schemaNode] : [];
6767

6868
for (const typeName in typesMap) {
6969
const type = typesMap[typeName];
@@ -75,17 +75,17 @@ export function printSchemaWithDirectives(
7575
}
7676

7777
if (isObjectType(type)) {
78-
result.push(print(astFromObjectType(type, schema, pathToDirectivesInExtensions)));
78+
definitions.push(astFromObjectType(type, schema, pathToDirectivesInExtensions));
7979
} else if (isInterfaceType(type)) {
80-
result.push(print(astFromInterfaceType(type, schema, pathToDirectivesInExtensions)));
80+
definitions.push(astFromInterfaceType(type, schema, pathToDirectivesInExtensions));
8181
} else if (isUnionType(type)) {
82-
result.push(print(astFromUnionType(type, schema, pathToDirectivesInExtensions)));
82+
definitions.push(astFromUnionType(type, schema, pathToDirectivesInExtensions));
8383
} else if (isInputObjectType(type)) {
84-
result.push(print(astFromInputObjectType(type, schema, pathToDirectivesInExtensions)));
84+
definitions.push(astFromInputObjectType(type, schema, pathToDirectivesInExtensions));
8585
} else if (isEnumType(type)) {
86-
result.push(print(astFromEnumType(type, schema, pathToDirectivesInExtensions)));
86+
definitions.push(astFromEnumType(type, schema, pathToDirectivesInExtensions));
8787
} else if (isScalarType(type)) {
88-
result.push(print(astFromScalarType(type, schema, pathToDirectivesInExtensions)));
88+
definitions.push(astFromScalarType(type, schema, pathToDirectivesInExtensions));
8989
} else {
9090
throw new Error(`Unknown type ${type}.`);
9191
}
@@ -97,10 +97,23 @@ export function printSchemaWithDirectives(
9797
continue;
9898
}
9999

100-
result.push(print(astFromDirective(directive, schema, pathToDirectivesInExtensions)));
100+
definitions.push(astFromDirective(directive, schema, pathToDirectivesInExtensions));
101101
}
102102

103-
return result.join('\n');
103+
return {
104+
kind: Kind.DOCUMENT,
105+
definitions,
106+
};
107+
}
108+
109+
// this approach uses the default schema printer rather than a custom solution, so may be more backwards compatible
110+
// currently does not allow customization of printSchema options having to do with comments.
111+
export function printSchemaWithDirectives(
112+
schema: GraphQLSchema,
113+
options: PrintSchemaWithDirectivesOptions = {}
114+
): string {
115+
const documentNode = getDocumentNodeFromSchema(schema, options);
116+
return print(documentNode);
104117
}
105118

106119
export function astFromSchema(
@@ -431,7 +444,7 @@ export function astFromEnumType(
431444
};
432445
}
433446

434-
function astFromScalarType(
447+
export function astFromScalarType(
435448
type: GraphQLScalarType,
436449
schema: GraphQLSchema,
437450
pathToDirectivesInExtensions: Array<string>
@@ -454,7 +467,7 @@ function astFromScalarType(
454467
};
455468
}
456469

457-
function astFromField(
470+
export function astFromField(
458471
field: GraphQLField<any, any>,
459472
schema: GraphQLSchema,
460473
pathToDirectivesInExtensions: Array<string>
@@ -479,7 +492,7 @@ function astFromField(
479492
};
480493
}
481494

482-
function astFromInputField(
495+
export function astFromInputField(
483496
field: GraphQLInputField,
484497
schema: GraphQLSchema,
485498
pathToDirectivesInExtensions: Array<string>
@@ -500,10 +513,11 @@ function astFromInputField(
500513
},
501514
type: astFromType(field.type),
502515
directives: getDeprecatableDirectiveNodes(field, schema, pathToDirectivesInExtensions),
516+
defaultValue: astFromValue(field.defaultValue, field.type),
503517
};
504518
}
505519

506-
function astFromEnumValue(
520+
export function astFromEnumValue(
507521
value: GraphQLEnumValue,
508522
schema: GraphQLSchema,
509523
pathToDirectivesInExtensions: Array<string>

‎packages/utils/src/types.ts

+3-1
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,12 @@ export interface SchemaPrintOptions {
1212
commentDescriptions?: boolean;
1313
}
1414

15-
export interface PrintSchemaWithDirectivesOptions extends SchemaPrintOptions {
15+
export interface GetDocumentNodeFromSchemaOptions {
1616
pathToDirectivesInExtensions?: Array<string>;
1717
}
1818

19+
export type PrintSchemaWithDirectivesOptions = SchemaPrintOptions & GetDocumentNodeFromSchemaOptions;
20+
1921
export type Maybe<T> = null | undefined | T;
2022

2123
export type Constructor<T> = new (...args: any[]) => T;

‎packages/utils/tests/print-schema-with-directives.spec.ts

+1
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,7 @@ describe('printSchemaWithDirectives', () => {
319319
320320
input SomeInputType @INPUT_OBJECT {
321321
someInputField: String @INPUT_FIELD_DEFINITION
322+
someOtherInputField: Int = 1
322323
}
323324
`;
324325
const schema = buildSchema(typeDefs);

0 commit comments

Comments
 (0)
Please sign in to comment.