Improve callCloudCode mutation to receive a CloudCodeFunction enum instead of a String (#6029)
* Add listing test * Improvements * Fixinf package.json * Fix package.json * Fix tests
This commit is contained in:
committed by
GitHub
parent
33d2b16476
commit
a754b883b2
@@ -1,46 +1,65 @@
|
||||
import { GraphQLNonNull, GraphQLString } from 'graphql';
|
||||
import { GraphQLNonNull, GraphQLEnumType } from 'graphql';
|
||||
import { FunctionsRouter } from '../../Routers/FunctionsRouter';
|
||||
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
||||
|
||||
const load = parseGraphQLSchema => {
|
||||
parseGraphQLSchema.addGraphQLMutation(
|
||||
'callCloudCode',
|
||||
{
|
||||
description:
|
||||
'The call mutation can be used to invoke a cloud code function.',
|
||||
args: {
|
||||
functionName: {
|
||||
description: 'This is the name of the function to be called.',
|
||||
type: new GraphQLNonNull(GraphQLString),
|
||||
},
|
||||
params: {
|
||||
description: 'These are the params to be passed to the function.',
|
||||
type: defaultGraphQLTypes.OBJECT,
|
||||
},
|
||||
},
|
||||
type: defaultGraphQLTypes.ANY,
|
||||
async resolve(_source, args, context) {
|
||||
try {
|
||||
const { functionName, params } = args;
|
||||
const { config, auth, info } = context;
|
||||
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
|
||||
);
|
||||
|
||||
return (await FunctionsRouter.handleCloudFunction({
|
||||
params: {
|
||||
functionName,
|
||||
},
|
||||
config,
|
||||
auth,
|
||||
info,
|
||||
body: params,
|
||||
})).response.result;
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
parseGraphQLSchema.addGraphQLMutation(
|
||||
'callCloudCode',
|
||||
{
|
||||
description:
|
||||
'The call mutation can be used to invoke a cloud code function.',
|
||||
args: {
|
||||
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,
|
||||
},
|
||||
},
|
||||
type: defaultGraphQLTypes.ANY,
|
||||
async resolve(_source, args, context) {
|
||||
try {
|
||||
const { functionName, params } = args;
|
||||
const { config, auth, info } = context;
|
||||
|
||||
return (await FunctionsRouter.handleCloudFunction({
|
||||
params: {
|
||||
functionName,
|
||||
},
|
||||
config,
|
||||
auth,
|
||||
info,
|
||||
body: params,
|
||||
})).response.result;
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
},
|
||||
},
|
||||
},
|
||||
true,
|
||||
true
|
||||
);
|
||||
true,
|
||||
true
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
export { load };
|
||||
|
||||
Reference in New Issue
Block a user