-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathintrospectionConverter.ts
More file actions
94 lines (90 loc) · 3.87 KB
/
introspectionConverter.ts
File metadata and controls
94 lines (90 loc) · 3.87 KB
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
import {
BirdseyeDataStructure,
Type as BirdseyeType,
Connection as BirdseyeConnection,
Field as BirdseyeField
} from '../dataStructure';
import {
IntrospectionSchema,
IntrospectionType,
IntrospectionObjectType,
IntrospectionInterfaceType,
IntrospectionOutputTypeRef,
IntrospectionOutputType,
IntrospectionListTypeRef,
IntrospectionNonNullTypeRef,
IntrospectionNamedTypeRef
} from './utilities/introspectionQuery';
import { mapToArray, arrayToMap } from '../utils';
type FilteredIntrospectionType = IntrospectionObjectType | IntrospectionInterfaceType;
export default class DataStructure extends BirdseyeDataStructure {
constructor(schema: IntrospectionSchema, config = {}) {
super(config);
this.typeMap = this.convert(schema.types);
}
convert(types: ReadonlyArray<IntrospectionType>) {
const typeMap = arrayToMap(types, (t) => t.name)
const filteredTypeMap = types
.filter(type => {
if (this.isBaseEntity(type)) {
return false;
}
return true;
})
.reduce((acc, type) => {
acc[type.name] = typeMap[type.name] as FilteredIntrospectionType;
return acc;
}, {} as { [key: string]: FilteredIntrospectionType })
const birdseyeTypeMap = mapToArray(filteredTypeMap).reduce((accumulator, type) => {
const birdseyeType = new BirdseyeType(type.name);
accumulator[type.name] = birdseyeType;
return accumulator;
}, {} as { [key: string]: BirdseyeType });
mapToArray(birdseyeTypeMap)
.map(birdseyeType => {
const fields = filteredTypeMap[birdseyeType.name].fields;
fields.map((field) => {
const birdseyeField = new BirdseyeField();
birdseyeField.name = field.name;
birdseyeField.typeLabel = this.getFieldLabel(field.type);
if (!birdseyeField.typeLabel || birdseyeField.typeLabel === 'undefined') {
birdseyeField.typeLabel = this.getFieldLabel(field.type)
}
const type = this.getNestedType(field.type).name;
if (!type) {
console.log(type, field.type)
}
const target = birdseyeTypeMap[type];
birdseyeField.type = target || type;
// if (target) {
// const birdseyeConnection = new BirdseyeConnection();
// birdseyeConnection.source = birdseyeType;
// birdseyeConnection.target = target;
// this.addConnection(birdseyeConnection);
// }
birdseyeType.addField(birdseyeField);
}, {});
})
return birdseyeTypeMap;
}
isBaseEntity(type: IntrospectionType) {
return ["SCALAR", "UNION", "ENUM", "INPUT_OBJECT"].includes(type.kind) || type.name.startsWith("__") || type.name === "Mutation"
}
getFieldLabel(type: IntrospectionOutputTypeRef): string {
if (type.kind === "LIST") {
return `[${this.getFieldLabel(type.ofType)}]`;
}
if (type.kind === "NON_NULL") {
return `${this.getFieldLabel(type.ofType)}!`;
}
return `${type.name}`;
}
getNestedType(outputType: IntrospectionOutputTypeRef): IntrospectionOutputType {
if (!Object.keys(outputType).includes("name") || !outputType["name"]) {
return this.getNestedType(
(outputType as IntrospectionListTypeRef<any> | IntrospectionNonNullTypeRef<IntrospectionNamedTypeRef<IntrospectionOutputType> | IntrospectionListTypeRef<any>>).ofType
);
}
return outputType as IntrospectionOutputType;
}
}