This repository was archived by the owner on Oct 9, 2025. It is now read-only.
forked from jcoreio/apollo-magic-refetch
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgetSchemaTypes.js.flow
More file actions
97 lines (85 loc) · 2.1 KB
/
getSchemaTypes.js.flow
File metadata and controls
97 lines (85 loc) · 2.1 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
95
96
97
// @flow
import type { TypeMetadata } from './typesQuery'
export type RawField = {
name: string,
type: RawType,
}
export type RawType = {
name: ?string,
kind: string,
ofType?: ?RawType,
fields?: ?Array<RawField>,
}
export type Field = {
name: string,
type: Type,
parent: Type,
}
export type Type = {
name: ?string,
kind: string,
ofType?: ?Type,
fields?: ?{ [name: string]: Field },
parents?: Array<Field>,
}
export type Types = { [name: string]: Type }
function convertRawField({ name, type }: RawField): Field {
return ({ name, type: convertRawType(type) }: any)
}
function convertRawFields(fields: Array<RawField>): { [name: string]: Field } {
const convertedFields = {}
for (let field of fields) {
convertedFields[field.name] = (convertRawField(field): any)
}
return convertedFields
}
function convertRawType({ name, kind, ofType, fields }: RawType): Type {
return {
name,
kind,
ofType: ofType ? convertRawType(ofType) : null,
fields: fields ? convertRawFields(fields) : null,
}
}
export function linkTypes(rawTypes: Array<RawType>): Types {
const types: Types = {}
for (let rawType of rawTypes) {
const { name } = rawType
if (name) {
types[name] = convertRawType(rawType)
}
}
function resolveType(type: Type, parent: ?Field): Type {
const { name, ofType } = type
if (name && types[name]) type = types[name]
if (ofType) type.ofType = resolveType(ofType, parent)
if (parent) {
let { parents } = type
if (!parents) type.parents = parents = []
parents.push(parent)
}
return type
}
for (let name in types) {
const type = types[name]
const { fields } = type
if (fields) {
for (let name in fields) {
const field = fields[name]
field.type = resolveType(field.type, field)
field.parent = type
}
}
}
return types
}
export default async function getSchemaTypes(
fetchTypeMetadata: () => Promise<TypeMetadata>
): Promise<Types> {
const {
data: {
__schema: { types },
},
} = await fetchTypeMetadata()
return linkTypes((types: any))
}