GraphQL { functions { call } } generic mutation (#5818)

* Generic call function mutation
* Change function return type to any
* First passing test
* Testing errors
* Testing different data types
This commit is contained in:
Antonio Davi Macedo Coelho de Castro
2019-07-18 12:41:10 -07:00
committed by Douglas Muraoka
parent 6be15331a6
commit 0b86a86209
3 changed files with 202 additions and 0 deletions

View File

@@ -1,11 +1,13 @@
import * as objectsMutations from './objectsMutations';
import * as filesMutations from './filesMutations';
import * as usersMutations from './usersMutations';
import * as functionsMutations from './functionsMutations';
const load = parseGraphQLSchema => {
objectsMutations.load(parseGraphQLSchema);
filesMutations.load(parseGraphQLSchema);
usersMutations.load(parseGraphQLSchema);
functionsMutations.load(parseGraphQLSchema);
};
export { load };

View File

@@ -0,0 +1,57 @@
import { GraphQLObjectType, GraphQLNonNull, GraphQLString } from 'graphql';
import { FunctionsRouter } from '../../Routers/FunctionsRouter';
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
const load = parseGraphQLSchema => {
const fields = {};
fields.call = {
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;
return (await FunctionsRouter.handleCloudFunction({
params: {
functionName,
},
config,
auth,
info,
body: params,
})).response.result;
} catch (e) {
parseGraphQLSchema.handleError(e);
}
},
};
const functionsMutation = new GraphQLObjectType({
name: 'FunctionsMutation',
description:
'FunctionsMutation is the top level type for functions mutations.',
fields,
});
parseGraphQLSchema.graphQLTypes.push(functionsMutation);
parseGraphQLSchema.graphQLMutations.functions = {
description: 'This is the top level for functions mutations.',
type: functionsMutation,
resolve: () => new Object(),
};
};
export { load };