-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathGraphQLController.js
More file actions
168 lines (148 loc) · 4.89 KB
/
GraphQLController.js
File metadata and controls
168 lines (148 loc) · 4.89 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
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
166
167
168
import requiredParameter from '../../lib/requiredParameter';
import DatabaseController from './DatabaseController';
import CacheController from './CacheController';
const GraphQLConfigClass = '_GraphQLConfig';
const GraphQLConfigId = '1';
const GraphQLConfigKey = 'config';
class GraphQLController {
databaseController: DatabaseController;
cacheController: CacheController;
isEnabled: boolean;
constructor(params: {
databaseController: DatabaseController,
cacheController: CacheController,
mountGraphQL: boolean,
}) {
this.databaseController =
params.databaseController ||
requiredParameter(
`GraphQLController requires a "databaseController" to be instantiated.`
);
this.cacheController =
params.cacheController ||
requiredParameter(
`GraphQLController requires a "cacheController" to be instantiated.`
);
this.isEnabled = !!params.mountGraphQL;
}
async getGraphQLConfig(): Promise<ParseGraphQLConfig> {
const _cachedConfig = await this._getCachedGraphQLConfig();
if (_cachedConfig) {
return _cachedConfig;
}
const results = await this.databaseController.find(
GraphQLConfigClass,
{ objectId: GraphQLConfigId },
{ limit: 1 }
);
let graphQLConfig;
if (results.length != 1) {
// If there is no config in the database - return empty config.
return {};
} else {
graphQLConfig = results[0][GraphQLConfigKey];
}
await this._putCachedGraphQLConfig(graphQLConfig);
return graphQLConfig;
}
async updateGraphQLConfig(
graphQLConfig: ParseGraphQLConfig
): Promise<ParseGraphQLConfig> {
if(!this.isEnabled) {
throw new Error('GraphQL is not enabled on this application.');
}
// throws if invalid
this._validateGraphQLConfig(graphQLConfig);
// Transform in dot notation to make sure it works
const update = Object.keys(graphQLConfig).reduce((acc, key) => {
acc[`${GraphQLConfigKey}.${key}`] = graphQLConfig[key];
return acc;
}, {});
await this.databaseController.update(
GraphQLConfigClass,
{ objectId: GraphQLConfigId },
update,
{ upsert: true }
);
await this._putCachedGraphQLConfig(graphQLConfig);
return { response: { result: true } };
}
async _getCachedGraphQLConfig() {
return this.cacheController.graphQL.get(GraphQLConfigKey);
}
async _putCachedGraphQLConfig(graphQLConfig: ParseGraphQLConfig) {
return this.cacheController.graphQL.put(
GraphQLConfigKey,
graphQLConfig,
60000
);
}
_validateGraphQLConfig(graphQLConfig: ?ParseGraphQLConfig): void {
let errorMessage: string;
if (!graphQLConfig) {
errorMessage = 'cannot be undefined, null or empty.';
} else if (typeof graphQLConfig !== 'object') {
errorMessage = 'must be a valid object.';
} else {
const {
enabledForClasses,
disabledForClasses,
classConfigs,
...invalidKeys
} = graphQLConfig;
if (invalidKeys.length) {
errorMessage = `encountered invalid keys: ${invalidKeys}`;
}
// TODO use more rigirous structural validations
if (enabledForClasses && !Array.isArray(enabledForClasses)) {
errorMessage = `"enabledForClasses" is not a valid array.`;
}
if (disabledForClasses && !Array.isArray(disabledForClasses)) {
errorMessage = `"disabledForClasses" is not a valid array.`;
}
if (classConfigs && !Array.isArray(classConfigs)) {
errorMessage = `"classConfigs" is not a valid array.`;
}
}
if (errorMessage) {
throw new Error(`Invalid graphQLConfig: ${errorMessage}`);
}
}
}
export interface ParseGraphQLConfig {
enabledForClasses?: string[];
disabledForClasses?: string[];
classConfigs?: ParseGraphQLClassConfig[];
}
export interface ParseGraphQLClassConfig {
className: string;
/* The `type` object contains options for how the class types are generated */
type: ?{
/* Fields that are allowed when creating or updating an object. */
inputFields:
| ?(string[])
| ?{
/* Leave blank to allow all available fields in the schema. */
create?: string[],
update?: string[],
},
/* Fields on the edges that can be resolved from a query, i.e. the Result Type. */
outputFields: ?(string[]),
/* Fields by which a query can be filtered, i.e. the `where` object. */
constraintFields: ?(string[]),
/* Fields by which a query can be sorted; suffix with _ASC or _DESC to enforce direction. */
sortFields: ?(string[]),
};
/* The `query` object contains options for which class queries are generated */
query: ?{
get: ?boolean,
find: ?boolean,
};
/* The `mutation` object contains options for which class mutations are generated */
mutation: ?{
create: ?boolean,
update: ?boolean,
delete: ?boolean,
};
}
export default GraphQLController;