Repository navigation
Expand file tree
/
Copy pathderiveAction.js
More file actions
258 lines (225 loc) · 7.15 KB
/
Copy pathderiveAction.js
File metadata and controls
258 lines (225 loc) · 7.15 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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
import { camelize } from 'inflection';
import {
buildClientSchema,
isScalarType,
isEnumType,
isInputObjectType,
parse as sdlParse,
validate,
} from 'graphql';
import { wrapTypename, getAstTypeMetadata } from './wrappingTypeUtils';
import { inbuiltTypes } from './hasuraCustomTypeUtils';
import {
getTypeFields,
getUnderlyingType,
getOperationType,
} from './graphqlSchemaUtils';
import { isValidOperationName } from './sdlUtils';
export const validateOperation = (operationString, clientSchema) => {
// parse operation string
let operationAst;
try {
operationAst = sdlParse(operationString);
} catch (e) {
throw Error('this seems to be an invalid GraphQL query');
}
const schemaValidationErrors = validate(clientSchema, operationAst);
if (schemaValidationErrors.length) {
throw Error(
'this is not a valid GraphQL query as per the current GraphQL schema'
);
}
if (operationAst.definitions.some(d => d.kind === 'FragmentDefinition')) {
throw Error('fragments are not supported');
}
if (operationAst.definitions.some(d => !isValidOperationName(d.operation))) {
throw Error('subscriptions cannot be derived into actions');
}
operationAst.definitions = operationAst.definitions.filter(d =>
isValidOperationName(d.operation)
);
// throw error if the AST is empty
if (!operationAst.definitions.length) {
throw Error('could not find any mutation operations');
}
if (operationAst.definitions.length !== 1) {
throw Error('you can derive action from only one operation');
}
if (operationAst.definitions[0].kind !== 'OperationDefinition') {
throw Error('could not find any operation in the given query');
}
// filter schema specific fields from the operation
operationAst.definitions[0].selectionSet.selections = operationAst.definitions[0].selectionSet.selections.filter(
s => {
return s.name.value.indexOf('__') !== 0;
}
);
// throw error if no operation is being made
if (!operationAst.definitions[0].selectionSet.selections.length) {
throw Error('the given operation must ask for at least one root field');
}
return operationAst;
};
const deriveAction = (
operationString,
introspectionSchema,
actionName = null
) => {
const clientSchema = introspectionSchema.__schema
? buildClientSchema(introspectionSchema)
: introspectionSchema;
let operationAst;
try {
operationAst = validateOperation(operationString, clientSchema);
} catch (e) {
throw e;
}
const operation = operationAst.definitions[0].operation;
const variables = operationAst.definitions[0].variableDefinitions;
// get operation name
const rootFields = operationAst.definitions[0].selectionSet.selections;
const operationDefinition = rootFields[0];
const operationName = operationDefinition.name.value;
const selectedFields = operationDefinition.selectionSet.selections.map(s => {
return s.name.value;
});
// get action name if not provided
if (!actionName) {
actionName = operationAst.definitions[0].name
? operationAst.definitions[0].name.value
: camelize(`${operationName}_derived`);
}
// function to prefix typename with the action name
const prefixTypename = typename => {
return camelize(`${actionName}_${typename}`);
};
const allHasuraTypes = clientSchema._typeMap;
const operationType = getOperationType(clientSchema, operation);
const isHasuraScalar = name => {
return isScalarType(allHasuraTypes[name]);
};
const actionArguments = [];
const newTypes = {};
const handleType = (type, typename) => {
if (newTypes[typename]) {
return;
}
const newType = {};
newType.name = typename;
if (isScalarType(type)) {
if (!inbuiltTypes[type.name] && !allHasuraTypes[type.name]) {
newType.kind = 'scalar';
newTypes[typename] = newType;
}
return;
}
if (isEnumType(type)) {
newType.kind = 'enum';
newType.values = type._values.map(v => ({
value: v.value,
description: v.description,
}));
newTypes[typename] = newType;
return;
}
if (isInputObjectType(type)) {
newType.kind = 'input_object';
newType.fields = [];
const typeFields = getTypeFields(type);
newTypes[typename] = true;
Object.values(typeFields).forEach(tf => {
const _tf = { name: tf.name };
const {
type: underLyingType,
wraps: fieldTypeWraps,
} = getUnderlyingType(tf.type);
if (
inbuiltTypes[underLyingType.name] ||
isHasuraScalar(underLyingType.name)
) {
_tf.type = wrapTypename(underLyingType.name, fieldTypeWraps);
} else {
_tf.type = wrapTypename(
prefixTypename(underLyingType.name),
fieldTypeWraps
);
}
handleType(underLyingType, prefixTypename(underLyingType.name));
newType.fields.push(_tf);
});
newTypes[typename] = newType;
return;
}
};
variables.forEach(v => {
const generatedArg = {
name: v.variable.name.value,
};
const argTypeMetadata = getAstTypeMetadata(v.type);
if (
!inbuiltTypes[argTypeMetadata.typename] &&
!isHasuraScalar(argTypeMetadata.typename)
) {
const argTypename = prefixTypename(argTypeMetadata.typename);
generatedArg.type = wrapTypename(argTypename, argTypeMetadata.stack);
const typeInSchema = allHasuraTypes[argTypeMetadata.typename];
handleType(typeInSchema, argTypename);
} else {
generatedArg.type = wrapTypename(
argTypeMetadata.typename,
argTypeMetadata.stack
);
}
actionArguments.push(generatedArg);
});
const actionOutputTypename = prefixTypename('output');
const actionOutputType = {
name: actionOutputTypename,
kind: 'object',
fields: [],
};
const outputTypeFields = {};
rootFields.forEach(f => {
const rfName = f.name.value;
const refOperationOutputType = getUnderlyingType(
getTypeFields(operationType)[rfName].type
).type;
Object.values(getTypeFields(refOperationOutputType)).forEach(
outputTypeField => {
const fieldTypeMetadata = getUnderlyingType(outputTypeField.type);
if (
isScalarType(fieldTypeMetadata.type) &&
selectedFields.includes(outputTypeField.name)
) {
outputTypeFields[outputTypeField.name] = wrapTypename(
fieldTypeMetadata.type.name,
fieldTypeMetadata.wraps
);
}
}
);
});
if (!Object.keys(outputTypeFields).length) {
throw new Error(
`no scalar found in the selection set of your operation; only scalar fields of the operation get mapped onto the output type of the derived action`
);
}
Object.keys(outputTypeFields).forEach(fieldName => {
actionOutputType.fields.push({
name: fieldName,
type: outputTypeFields[fieldName],
});
});
newTypes[actionOutputTypename] = actionOutputType;
return {
types: Object.values(newTypes),
action: {
name: actionName,
type: operation,
arguments: actionArguments,
output_type: actionOutputTypename,
},
variables,
};
};
export default deriveAction;