-
Notifications
You must be signed in to change notification settings - Fork 242
/
FieldSet.ts
152 lines (133 loc) 路 3.53 KB
/
FieldSet.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
import {
FieldNode,
getNamedType,
GraphQLCompositeType,
GraphQLField,
isCompositeType,
Kind,
SelectionNode,
SelectionSetNode,
GraphQLObjectType,
} from 'graphql';
import { getResponseName } from './utilities/graphql';
export interface Field<
TParent extends GraphQLCompositeType = GraphQLCompositeType
> {
scope: Scope<TParent>;
fieldNode: FieldNode;
fieldDef: GraphQLField<any, any>;
}
export interface Scope<TParent extends GraphQLCompositeType> {
parentType: TParent;
possibleTypes: ReadonlyArray<GraphQLObjectType>;
enclosingScope?: Scope<GraphQLCompositeType>;
}
export type FieldSet = Field[];
export function printFields(fields?: FieldSet) {
if (!fields) return '[]';
return (
'[' +
fields
.map(field => `"${field.scope.parentType.name}.${field.fieldDef.name}"`)
.join(', ') +
']'
);
}
export function matchesField(field: Field) {
// TODO: Compare parent type and arguments
return (otherField: Field) => {
return field.fieldDef.name === otherField.fieldDef.name;
};
}
function groupBy<T, U>(keyFunction: (element: T) => U) {
return (iterable: Iterable<T>) => {
const result = new Map<U, T[]>();
for (const element of iterable) {
const key = keyFunction(element);
const group = result.get(key);
if (group) {
group.push(element);
} else {
result.set(key, [element]);
}
}
return result;
};
}
export const groupByResponseName = groupBy<Field, string>(field =>
getResponseName(field.fieldNode)
);
export const groupByParentType = groupBy<Field, GraphQLCompositeType>(
field => field.scope.parentType,
);
export function selectionSetFromFieldSet(
fields: FieldSet,
parentType?: GraphQLCompositeType,
): SelectionSetNode {
return {
kind: Kind.SELECTION_SET,
selections: Array.from(groupByParentType(fields)).flatMap(
([typeCondition, fieldsByParentType]: [GraphQLCompositeType, FieldSet]) =>
wrapInInlineFragmentIfNeeded(
Array.from(groupByResponseName(fieldsByParentType).values()).map(
fieldsByResponseName => {
return combineFields(fieldsByResponseName)
.fieldNode;
},
),
typeCondition,
parentType,
),
),
};
}
function wrapInInlineFragmentIfNeeded(
selections: SelectionNode[],
typeCondition: GraphQLCompositeType,
parentType?: GraphQLCompositeType,
): SelectionNode[] {
return typeCondition === parentType
? selections
: [
{
kind: Kind.INLINE_FRAGMENT,
typeCondition: {
kind: Kind.NAMED_TYPE,
name: {
kind: Kind.NAME,
value: typeCondition.name,
},
},
selectionSet: { kind: Kind.SELECTION_SET, selections },
},
];
}
function combineFields(
fields: FieldSet,
): Field {
const { scope, fieldNode, fieldDef } = fields[0];
const returnType = getNamedType(fieldDef.type);
if (isCompositeType(returnType)) {
return {
scope,
fieldNode: {
...fieldNode,
selectionSet: mergeSelectionSets(fields.map(field => field.fieldNode)),
},
fieldDef,
};
} else {
return { scope, fieldNode, fieldDef };
}
}
function mergeSelectionSets(fieldNodes: FieldNode[]): SelectionSetNode {
const selections: SelectionNode[] = [];
for (const fieldNode of fieldNodes) {
if (!fieldNode.selectionSet) continue;
selections.push(...fieldNode.selectionSet.selections);
}
return {
kind: 'SelectionSet',
selections,
};
}