Skip to content

Commit 58b9dd9

Browse files
committed
Client mutation id on callCloudCode mutation
1 parent dd0d451 commit 58b9dd9

File tree

4 files changed

+120
-31
lines changed

4 files changed

+120
-31
lines changed

spec/ParseGraphQLServer.spec.js

Lines changed: 70 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -820,6 +820,53 @@ describe('ParseGraphQLServer', () => {
820820
'fileInfo',
821821
]);
822822
});
823+
824+
it('should have clientMutationId in call function input', async () => {
825+
Parse.Cloud.define('hello', () => {});
826+
827+
const callFunctionInputFields = (await apolloClient.query({
828+
query: gql`
829+
query {
830+
__type(name: "CallCloudCodeInput") {
831+
inputFields {
832+
name
833+
}
834+
}
835+
}
836+
`,
837+
})).data['__type'].inputFields
838+
.map(field => field.name)
839+
.sort();
840+
841+
expect(callFunctionInputFields).toEqual([
842+
'clientMutationId',
843+
'functionName',
844+
'params',
845+
]);
846+
});
847+
848+
it('should have clientMutationId in call function payload', async () => {
849+
Parse.Cloud.define('hello', () => {});
850+
851+
const callFunctionPayloadFields = (await apolloClient.query({
852+
query: gql`
853+
query {
854+
__type(name: "CallCloudCodePayload") {
855+
fields {
856+
name
857+
}
858+
}
859+
}
860+
`,
861+
})).data['__type'].fields
862+
.map(field => field.name)
863+
.sort();
864+
865+
expect(callFunctionPayloadFields).toEqual([
866+
'clientMutationId',
867+
'result',
868+
]);
869+
});
823870
});
824871

825872
describe('Parse Class Types', () => {
@@ -5923,19 +5970,33 @@ describe('ParseGraphQLServer', () => {
59235970
describe('Functions Mutations', () => {
59245971
it('can be called', async () => {
59255972
try {
5973+
const clientMutationId = uuidv4();
5974+
59265975
Parse.Cloud.define('hello', async () => {
59275976
return 'Hello world!';
59285977
});
59295978

59305979
const result = await apolloClient.mutate({
59315980
mutation: gql`
5932-
mutation CallFunction {
5933-
callCloudCode(functionName: hello)
5981+
mutation CallFunction($input: CallCloudCodeInput!) {
5982+
callCloudCode(input: $input) {
5983+
clientMutationId
5984+
result
5985+
}
59345986
}
59355987
`,
5988+
variables: {
5989+
input: {
5990+
clientMutationId,
5991+
functionName: 'hello',
5992+
},
5993+
},
59365994
});
59375995

5938-
expect(result.data.callCloudCode).toEqual('Hello world!');
5996+
expect(result.data.callCloudCode.clientMutationId).toEqual(
5997+
clientMutationId
5998+
);
5999+
expect(result.data.callCloudCode.result).toEqual('Hello world!');
59396000
} catch (e) {
59406001
handleError(e);
59416002
}
@@ -5950,7 +6011,9 @@ describe('ParseGraphQLServer', () => {
59506011
await apolloClient.mutate({
59516012
mutation: gql`
59526013
mutation CallFunction {
5953-
callCloudCode(functionName: hello)
6014+
callCloudCode(input: { functionName: hello }) {
6015+
result
6016+
}
59546017
}
59556018
`,
59566019
});
@@ -6051,7 +6114,9 @@ describe('ParseGraphQLServer', () => {
60516114
apolloClient.mutate({
60526115
mutation: gql`
60536116
mutation CallFunction($params: Object) {
6054-
callCloudCode(functionName: hello, params: $params)
6117+
callCloudCode(input: { functionName: hello, params: $params }) {
6118+
result
6119+
}
60556120
}
60566121
`,
60576122
variables: {

src/GraphQL/ParseGraphQLSchema.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ const RESERVED_GRAPHQL_TYPE_NAMES = [
3939
'SignUpFieldsInput',
4040
'LogInFieldsInput',
4141
'CloudCodeFunction',
42+
'CallCloudCodeInput',
43+
'CallCloudCodePayload',
4244
];
4345
const RESERVED_GRAPHQL_QUERY_NAMES = ['health', 'viewer', 'class', 'classes'];
4446
const RESERVED_GRAPHQL_MUTATION_NAMES = [

src/GraphQL/loaders/filesMutations.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,11 @@ const load = parseGraphQLSchema => {
8383
},
8484
});
8585

86-
parseGraphQLSchema.addGraphQLType(createMutation.args.input.type, true, true);
86+
parseGraphQLSchema.addGraphQLType(
87+
createMutation.args.input.type.ofType,
88+
true,
89+
true
90+
);
8791
parseGraphQLSchema.addGraphQLType(createMutation.type, true, true);
8892
parseGraphQLSchema.addGraphQLMutation(
8993
'createFile',

src/GraphQL/loaders/functionsMutations.js

Lines changed: 43 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { GraphQLNonNull, GraphQLEnumType } from 'graphql';
2+
import { mutationWithClientMutationId } from 'graphql-relay';
23
import { FunctionsRouter } from '../../Routers/FunctionsRouter';
34
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
45

@@ -21,41 +22,58 @@ const load = parseGraphQLSchema => {
2122
true
2223
);
2324

24-
parseGraphQLSchema.addGraphQLMutation(
25-
'callCloudCode',
26-
{
27-
description:
28-
'The call mutation can be used to invoke a cloud code function.',
29-
args: {
30-
functionName: {
31-
description: 'This is the function to be called.',
32-
type: new GraphQLNonNull(cloudCodeFunctionEnum),
33-
},
34-
params: {
35-
description: 'These are the params to be passed to the function.',
36-
type: defaultGraphQLTypes.OBJECT,
37-
},
25+
const callCloudCodeMutation = mutationWithClientMutationId({
26+
name: 'CallCloudCode',
27+
description:
28+
'The callCloudCode mutation can be used to invoke a cloud code function.',
29+
inputFields: {
30+
functionName: {
31+
description: 'This is the function to be called.',
32+
type: new GraphQLNonNull(cloudCodeFunctionEnum),
33+
},
34+
params: {
35+
description: 'These are the params to be passed to the function.',
36+
type: defaultGraphQLTypes.OBJECT,
3837
},
39-
type: defaultGraphQLTypes.ANY,
40-
async resolve(_source, args, context) {
41-
try {
42-
const { functionName, params } = args;
43-
const { config, auth, info } = context;
38+
},
39+
outputFields: {
40+
result: {
41+
description:
42+
'This is the result value of the cloud code function execution.',
43+
type: defaultGraphQLTypes.ANY,
44+
},
45+
},
46+
mutateAndGetPayload: async (args, context) => {
47+
try {
48+
const { functionName, params } = args;
49+
const { config, auth, info } = context;
4450

45-
return (await FunctionsRouter.handleCloudFunction({
51+
return {
52+
result: (await FunctionsRouter.handleCloudFunction({
4653
params: {
4754
functionName,
4855
},
4956
config,
5057
auth,
5158
info,
5259
body: params,
53-
})).response.result;
54-
} catch (e) {
55-
parseGraphQLSchema.handleError(e);
56-
}
57-
},
60+
})).response.result,
61+
};
62+
} catch (e) {
63+
parseGraphQLSchema.handleError(e);
64+
}
5865
},
66+
});
67+
68+
parseGraphQLSchema.addGraphQLType(
69+
callCloudCodeMutation.args.input.type.ofType,
70+
true,
71+
true
72+
);
73+
parseGraphQLSchema.addGraphQLType(callCloudCodeMutation.type, true, true);
74+
parseGraphQLSchema.addGraphQLMutation(
75+
'callCloudCode',
76+
callCloudCodeMutation,
5977
true,
6078
true
6179
);

0 commit comments

Comments
 (0)