Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve callCloudCode mutation to receive a CloudCodeFunction enum instead of a String #6029

Merged
merged 6 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 48 additions & 13 deletions spec/ParseGraphQLServer.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5177,19 +5177,23 @@ describe('ParseGraphQLServer', () => {

describe('Functions Mutations', () => {
it('can be called', async () => {
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});
try {
Parse.Cloud.define('hello', async () => {
return 'Hello world!';
});

const result = await apolloClient.mutate({
mutation: gql`
mutation CallFunction {
callCloudCode(functionName: "hello")
}
`,
});
const result = await apolloClient.mutate({
mutation: gql`
mutation CallFunction {
callCloudCode(functionName: hello)
}
`,
});

expect(result.data.callCloudCode).toEqual('Hello world!');
expect(result.data.callCloudCode).toEqual('Hello world!');
} catch (e) {
handleError(e);
}
});

it('can throw errors', async () => {
Expand All @@ -5201,7 +5205,7 @@ describe('ParseGraphQLServer', () => {
await apolloClient.mutate({
mutation: gql`
mutation CallFunction {
callCloudCode(functionName: "hello")
callCloudCode(functionName: hello)
}
`,
});
Expand Down Expand Up @@ -5302,14 +5306,45 @@ describe('ParseGraphQLServer', () => {
apolloClient.mutate({
mutation: gql`
mutation CallFunction($params: Object) {
callCloudCode(functionName: "hello", params: $params)
callCloudCode(functionName: hello, params: $params)
}
`,
variables: {
params,
},
});
});

it('should list all functions in the enum type', async () => {
try {
Parse.Cloud.define('a', async () => {
return 'hello a';
});

Parse.Cloud.define('b', async () => {
return 'hello b';
});

const functionEnum = (await apolloClient.query({
query: gql`
query ObjectType {
__type(name: "CloudCodeFunction") {
kind
enumValues {
name
}
}
}
`,
})).data['__type'];
expect(functionEnum.kind).toEqual('ENUM');
expect(
functionEnum.enumValues.map(value => value.name).sort()
).toEqual(['a', 'b']);
} catch (e) {
handleError(e);
}
});
});

describe('Data Types', () => {
Expand Down
20 changes: 18 additions & 2 deletions src/GraphQL/ParseGraphQLSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import DatabaseController from '../Controllers/DatabaseController';
import { toGraphQLError } from './parseGraphQLUtils';
import * as schemaDirectives from './loaders/schemaDirectives';
import * as schemaTypes from './loaders/schemaTypes';
import { getFunctionNames } from '../triggers';

const RESERVED_GRAPHQL_TYPE_NAMES = [
'String',
Expand All @@ -29,6 +30,7 @@ const RESERVED_GRAPHQL_TYPE_NAMES = [
'Viewer',
'SignUpFieldsInput',
'LogInFieldsInput',
'CloudCodeFunction',
];
const RESERVED_GRAPHQL_QUERY_NAMES = ['health', 'viewer', 'class', 'classes'];
const RESERVED_GRAPHQL_MUTATION_NAMES = [
Expand All @@ -53,6 +55,7 @@ class ParseGraphQLSchema {
databaseController: DatabaseController,
parseGraphQLController: ParseGraphQLController,
log: any,
appId: String,
davimacedo marked this conversation as resolved.
Show resolved Hide resolved
} = {}
) {
this.parseGraphQLController =
Expand All @@ -64,20 +67,24 @@ class ParseGraphQLSchema {
this.log =
params.log || requiredParameter('You must provide a log instance!');
this.graphQLCustomTypeDefs = params.graphQLCustomTypeDefs;
this.appId = params.appId;
davimacedo marked this conversation as resolved.
Show resolved Hide resolved
}

async load() {
const { parseGraphQLConfig } = await this._initializeSchemaAndConfig();

const parseClasses = await this._getClassesForSchema(parseGraphQLConfig);
const parseClassesString = JSON.stringify(parseClasses);
const functionNames = await getFunctionNames(this.appId);
const functionNamesString = JSON.stringify(functionNames);

if (
this.graphQLSchema &&
!this._hasSchemaInputChanged({
parseClasses,
parseClassesString,
parseGraphQLConfig,
functionNamesString,
})
) {
return this.graphQLSchema;
Expand All @@ -86,6 +93,8 @@ class ParseGraphQLSchema {
this.parseClasses = parseClasses;
this.parseClassesString = parseClassesString;
this.parseGraphQLConfig = parseGraphQLConfig;
this.functionNames = functionNames;
this.functionNamesString = functionNamesString;
this.parseClassTypes = {};
this.viewerType = null;
this.graphQLAutoSchema = null;
Expand Down Expand Up @@ -370,12 +379,19 @@ class ParseGraphQLSchema {
parseClasses: any,
parseClassesString: string,
parseGraphQLConfig: ?ParseGraphQLConfig,
functionNamesString: string,
}): boolean {
const { parseClasses, parseClassesString, parseGraphQLConfig } = params;
const {
parseClasses,
parseClassesString,
parseGraphQLConfig,
functionNamesString,
} = params;

if (
JSON.stringify(this.parseGraphQLConfig) ===
JSON.stringify(parseGraphQLConfig)
JSON.stringify(parseGraphQLConfig) &&
this.functionNamesString === functionNamesString
) {
if (this.parseClasses === parseClasses) {
return false;
Expand Down
1 change: 1 addition & 0 deletions src/GraphQL/ParseGraphQLServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ class ParseGraphQLServer {
databaseController: this.parseServer.config.databaseController,
log: this.log,
graphQLCustomTypeDefs: this.config.graphQLCustomTypeDefs,
appId: this.parseServer.config.appId,
});
}

Expand Down
91 changes: 55 additions & 36 deletions src/GraphQL/loaders/functionsMutations.js
Original file line number Diff line number Diff line change
@@ -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,
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
);

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;
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);
}
return (await FunctionsRouter.handleCloudFunction({
params: {
functionName,
},
config,
auth,
info,
body: params,
})).response.result;
} catch (e) {
parseGraphQLSchema.handleError(e);
}
},
},
},
true,
true
);
true,
true
);
}
};

export { load };
23 changes: 23 additions & 0 deletions src/triggers.js
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,29 @@ export function getFunction(functionName, applicationId) {
return get(Category.Functions, functionName, applicationId);
}

export function getFunctionNames(applicationId) {
const store =
(_triggerStore[applicationId] &&
_triggerStore[applicationId][Category.Functions]) ||
{};
const functionNames = [];
const extractFunctionNames = (namespace, store) => {
Object.keys(store).forEach(name => {
const value = store[name];
if (namespace) {
name = `${namespace}.${name}`;
}
if (typeof value === 'function') {
functionNames.push(name);
} else {
extractFunctionNames(name, value);
}
});
};
extractFunctionNames(null, store);
return functionNames;
}

export function getJob(jobName, applicationId) {
return get(Category.Jobs, jobName, applicationId);
}
Expand Down