-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
executable file
·165 lines (152 loc) · 4.57 KB
/
index.js
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
153
154
155
156
157
158
159
160
161
162
163
164
165
import type {
FieldNode,
FragmentDefinitionNode,
SelectionNode,
SelectionSetNode,
} from 'graphql/language/ast';
import type {
GraphQLNamedType,
GraphQLOutputType,
GraphQLResolveInfo,
} from 'graphql/type';
import {
getNamedType,
} from 'graphql/type';
import { SchemaDirectiveVisitor } from 'graphql-tools';
export type Projection = {
[id:string]: Projection
};
/**
* Attempt to grab the type name from a GraphQL return type. E.g., if it's a null or a list
* we only care about the inner guts.
*/
function getTypeName(returnType: GraphQLOutputType): string {
let namedType: ?GraphQLNamedType = getNamedType(returnType);
if (namedType) {
while (namedType.ofType) {
namedType = namedType.ofType;
}
return namedType;
}
throw new Error('Could not detect return type');
}
function replaceFragments(
selectionSet: ?SelectionSetNode,
fragments: { [fragmentName: string]: FragmentDefinitionNode },
): Array<FieldNode> {
if (!selectionSet) {
return [];
}
return selectionSet.selections.reduce((
accumulator: Array<FieldNode>,
selection: SelectionNode,
): Array<FieldNode> => {
if (selection.kind === 'Field') {
return [...accumulator, selection];
}
if(selection.kind === 'InlineFragment'){
return [...accumulator, ...replaceFragments(selection.selectionSet, fragments)]
}
if (selection.kind !== 'FragmentSpread') {
// todo: Figure out what we need to do to handle this case
throw new Error(`Unable to handle SelectionNode of type '${selection.kind}'`);
}
const fragment: ?FragmentDefinitionNode = fragments[selection.name.value];
if (!fragment) {
throw new Error(`Unable to find fragment for selection '${selection.name.value}'`);
}
// Step through this fragment recursively in case it is also made up of fragments
const fragmentSelections = replaceFragments(fragment.selectionSet, fragments);
return [
...accumulator,
...fragmentSelections,
];
}, []);
}
export default function projector(
{ fieldNodes, returnType, fragments, schema }: GraphQLResolveInfo,
): Projection {
const projection: Projection = {};
const typeName: string = getTypeName(returnType).name;
fieldNodes.forEach((fieldNode: FieldNode): Projection => {
// Ensure all of the selection sets through the tree aren't fragments
const selections: Array<FieldNode> = replaceFragments(fieldNode.selectionSet, fragments);
selections.forEach((selection: FieldNode): Projection => {
let fieldName: string = selection.name.value;
/* eslint-disable no-underscore-dangle */
const type: ?GraphQLNamedType = schema._typeMap[typeName];
const field: ?Object = type.getFields()[fieldName];
if (!field) {
projection[fieldName] = 1;
return;
}
let customProjection = field.projection;
if (customProjection) {
if (!Array.isArray(customProjection)) {
customProjection = [customProjection];
}
customProjection.forEach((fname) => {
projection[fname] = 1;
});
return;
}
if (field.nameInDB) {
fieldName = field.nameInDB;
}
// if complex type
if (selection.selectionSet) {
projection[fieldName] = {};
const innerResult: Projection = projector({
fieldNodes: [selection],
returnType: field.type,
fragments,
schema });
// eslint-disable-next-line
for (const key: string in innerResult) {
projection[fieldName][key] = innerResult[key];
}
} else {
projection[fieldName] = 1;
}
});
});
return projection;
}
function toMongoProjection(projection) {
const result = {};
// eslint-disable-next-line
for (const key in projection) {
if (projection[key] === 1) {
result[key] = 1;
} else {
// eslint-disable-next-line
for (const inKey in toMongoProjection(projection[key])) {
result[`${key}.${inKey}`] = 1;
}
}
}
return result;
}
class ApolloProjector extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
const { projection, projections, nameInDB } = this.args;
field.projection = projection || projections; // eslint-disable-line
field.nameInDB = nameInDB; // eslint-disable-line
}
}
class IncludeAll extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
field.projection = field.name;
}
}
class IgnoreField extends SchemaDirectiveVisitor {
visitFieldDefinition(field) {
field.projection = [];
}
}
export {
toMongoProjection,
ApolloProjector,
IncludeAll,
IgnoreField,
};