/
print-schema-with-directives.ts
137 lines (124 loc) · 4.58 KB
/
print-schema-with-directives.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import {
GraphQLSchema,
print,
printType,
GraphQLNamedType,
Kind,
ObjectTypeExtensionNode,
isSpecifiedScalarType,
isIntrospectionType,
isScalarType,
parse,
TypeDefinitionNode,
DirectiveNode,
FieldDefinitionNode,
InputValueDefinitionNode,
EnumValueDefinitionNode,
} from 'graphql';
import { SchemaPrintOptions } from './types';
import { createSchemaDefinition } from './create-schema-definition';
export function printSchemaWithDirectives(schema: GraphQLSchema, _options: SchemaPrintOptions = {}): string {
const typesMap = schema.getTypeMap();
const result: string[] = [getSchemaDefinition(schema)];
for (const typeName in typesMap) {
const type = typesMap[typeName];
const isPredefinedScalar = isScalarType(type) && isSpecifiedScalarType(type);
const isIntrospection = isIntrospectionType(type);
if (isPredefinedScalar || isIntrospection) {
continue;
}
// KAMIL: we might want to turn on descriptions in future
result.push(print(correctType(typeName, typesMap)?.astNode));
}
const directives = schema.getDirectives();
for (const directive of directives) {
if (directive.astNode) {
result.push(print(directive.astNode));
}
}
return result.join('\n');
}
function extendDefinition(type: GraphQLNamedType): GraphQLNamedType['astNode'] {
switch (type.astNode.kind) {
case Kind.OBJECT_TYPE_DEFINITION:
return {
...type.astNode,
fields: type.astNode.fields.concat(
(type.extensionASTNodes as ReadonlyArray<ObjectTypeExtensionNode>).reduce(
(fields, node) => fields.concat(node.fields),
[]
)
),
};
case Kind.INPUT_OBJECT_TYPE_DEFINITION:
return {
...type.astNode,
fields: type.astNode.fields.concat(
(type.extensionASTNodes as ReadonlyArray<ObjectTypeExtensionNode>).reduce(
(fields, node) => fields.concat(node.fields),
[]
)
),
};
default:
return type.astNode;
}
}
function correctType<TMap extends { [key: string]: GraphQLNamedType }, TName extends keyof TMap>(
typeName: TName,
typesMap: TMap
): TMap[TName] {
const type = typesMap[typeName];
type.name = typeName.toString();
if (type.astNode && type.extensionASTNodes) {
type.astNode = type.extensionASTNodes ? extendDefinition(type) : type.astNode;
}
const doc = parse(printType(type));
const fixedAstNode = doc.definitions[0] as TypeDefinitionNode;
const originalAstNode = type?.astNode;
if (originalAstNode) {
(fixedAstNode.directives as DirectiveNode[]) = originalAstNode?.directives as DirectiveNode[];
if (fixedAstNode && 'fields' in fixedAstNode && originalAstNode && 'fields' in originalAstNode) {
for (const fieldDefinitionNode of fixedAstNode.fields) {
const originalFieldDefinitionNode = (originalAstNode.fields as (
| InputValueDefinitionNode
| FieldDefinitionNode
)[]).find(field => field.name.value === fieldDefinitionNode.name.value);
(fieldDefinitionNode.directives as DirectiveNode[]) = originalFieldDefinitionNode?.directives as DirectiveNode[];
if (
fieldDefinitionNode &&
'arguments' in fieldDefinitionNode &&
originalFieldDefinitionNode &&
'arguments' in originalFieldDefinitionNode
) {
for (const argument of fieldDefinitionNode.arguments) {
const originalArgumentNode = (originalFieldDefinitionNode as FieldDefinitionNode).arguments?.find(
arg => arg.name.value === argument.name.value
);
(argument.directives as DirectiveNode[]) = originalArgumentNode.directives as DirectiveNode[];
}
}
}
} else if (fixedAstNode && 'values' in fixedAstNode && originalAstNode && 'values' in originalAstNode) {
for (const valueDefinitionNode of fixedAstNode.values) {
const originalValueDefinitionNode = (originalAstNode.values as EnumValueDefinitionNode[]).find(
valueNode => valueNode.name.value === valueDefinitionNode.name.value
);
(valueDefinitionNode.directives as DirectiveNode[]) = originalValueDefinitionNode?.directives as DirectiveNode[];
}
}
}
type.astNode = fixedAstNode;
return type;
}
function getSchemaDefinition(schema: GraphQLSchema) {
if (!Object.getOwnPropertyDescriptor(schema, 'astNode').get && schema.astNode) {
return print(schema.astNode);
} else {
return createSchemaDefinition({
query: schema.getQueryType(),
mutation: schema.getMutationType(),
subscription: schema.getSubscriptionType(),
});
}
}