-
-
Notifications
You must be signed in to change notification settings - Fork 4.8k
Expand file tree
/
Copy pathfunctionsMutations.js
More file actions
74 lines (69 loc) · 2.41 KB
/
functionsMutations.js
File metadata and controls
74 lines (69 loc) · 2.41 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
import { GraphQLNonNull, GraphQLEnumType } from 'graphql';
import { mutationWithClientMutationId } from 'graphql-relay';
import { FunctionsRouter } from '../../Routers/FunctionsRouter';
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
const load = parseGraphQLSchema => {
if (parseGraphQLSchema.functionNames.length > 0) {
const cloudCodeFunctionEnum = parseGraphQLSchema.addGraphQLType(
new GraphQLEnumType({
name: 'CloudCodeFunction',
description:
'The CloudCodeFunction enum type contains a list of all available cloud code functions.',
values: parseGraphQLSchema.functionNames.reduce(
(values, functionName) => ({
...values,
[functionName]: { value: functionName },
}),
{}
),
}),
true,
true
);
const callCloudCodeMutation = mutationWithClientMutationId({
name: 'CallCloudCode',
description: 'The callCloudCode mutation can be used to invoke a cloud code function.',
inputFields: {
functionName: {
description: 'This is the function to be called.',
type: new GraphQLNonNull(cloudCodeFunctionEnum),
},
params: {
description: 'These are the params to be passed to the function.',
type: defaultGraphQLTypes.OBJECT,
},
},
outputFields: {
result: {
description: 'This is the result value of the cloud code function execution.',
type: defaultGraphQLTypes.ANY,
},
},
mutateAndGetPayload: async (args, context) => {
try {
const { functionName, params } = structuredClone(args);
const { config, auth, info } = context;
return {
result: (
await FunctionsRouter.handleCloudFunction({
params: {
functionName,
},
config,
auth,
info,
body: params,
})
).response.result,
};
} catch (e) {
parseGraphQLSchema.handleError(e);
}
},
});
parseGraphQLSchema.addGraphQLType(callCloudCodeMutation.args.input.type.ofType, true, true);
parseGraphQLSchema.addGraphQLType(callCloudCodeMutation.type, true, true);
parseGraphQLSchema.addGraphQLMutation('callCloudCode', callCloudCodeMutation, true, true);
}
};
export { load };