Skip to content

Commit a754b88

Browse files
authored
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
1 parent 33d2b16 commit a754b88

File tree

6 files changed

+244
-53
lines changed

6 files changed

+244
-53
lines changed

spec/ParseGraphQLSchema.spec.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ describe('ParseGraphQLSchema', () => {
77
let databaseController;
88
let parseGraphQLController;
99
let parseGraphQLSchema;
10+
const appId = 'test';
1011

1112
beforeAll(async () => {
1213
parseServer = await global.reconfigureServer({
@@ -18,11 +19,12 @@ describe('ParseGraphQLSchema', () => {
1819
databaseController,
1920
parseGraphQLController,
2021
log: defaultLogger,
22+
appId,
2123
});
2224
});
2325

2426
describe('constructor', () => {
25-
it('should require a parseGraphQLController, databaseController and a log instance', () => {
27+
it('should require a parseGraphQLController, databaseController, a log instance, and the appId', () => {
2628
expect(() => new ParseGraphQLSchema()).toThrow(
2729
'You must provide a parseGraphQLController instance!'
2830
);
@@ -36,6 +38,14 @@ describe('ParseGraphQLSchema', () => {
3638
databaseController: {},
3739
})
3840
).toThrow('You must provide a log instance!');
41+
expect(
42+
() =>
43+
new ParseGraphQLSchema({
44+
parseGraphQLController: {},
45+
databaseController: {},
46+
log: {},
47+
})
48+
).toThrow('You must provide the appId!');
3949
});
4050
});
4151

@@ -88,6 +98,7 @@ describe('ParseGraphQLSchema', () => {
8898
databaseController,
8999
parseGraphQLController,
90100
log: defaultLogger,
101+
appId,
91102
});
92103
await parseGraphQLSchema.load();
93104
const parseClasses = parseGraphQLSchema.parseClasses;
@@ -134,6 +145,7 @@ describe('ParseGraphQLSchema', () => {
134145
);
135146
},
136147
},
148+
appId,
137149
});
138150
await parseGraphQLSchema.load();
139151
const type = new GraphQLObjectType({ name: 'SomeClass' });
@@ -156,6 +168,7 @@ describe('ParseGraphQLSchema', () => {
156168
fail('Should not warn');
157169
},
158170
},
171+
appId,
159172
});
160173
await parseGraphQLSchema.load();
161174
const type = new GraphQLObjectType({ name: 'SomeClass' });
@@ -184,6 +197,7 @@ describe('ParseGraphQLSchema', () => {
184197
);
185198
},
186199
},
200+
appId,
187201
});
188202
await parseGraphQLSchema.load();
189203
expect(
@@ -203,6 +217,7 @@ describe('ParseGraphQLSchema', () => {
203217
fail('Should not warn');
204218
},
205219
},
220+
appId,
206221
});
207222
await parseGraphQLSchema.load();
208223
const type = new GraphQLObjectType({ name: 'String' });
@@ -225,6 +240,7 @@ describe('ParseGraphQLSchema', () => {
225240
);
226241
},
227242
},
243+
appId,
228244
});
229245
await parseGraphQLSchema.load();
230246
const field = {};
@@ -247,6 +263,7 @@ describe('ParseGraphQLSchema', () => {
247263
fail('Should not warn');
248264
},
249265
},
266+
appId,
250267
});
251268
await parseGraphQLSchema.load();
252269
const field = {};
@@ -274,6 +291,7 @@ describe('ParseGraphQLSchema', () => {
274291
);
275292
},
276293
},
294+
appId,
277295
});
278296
await parseGraphQLSchema.load();
279297
expect(parseGraphQLSchema.addGraphQLQuery('viewer', {})).toBeUndefined();
@@ -289,6 +307,7 @@ describe('ParseGraphQLSchema', () => {
289307
fail('Should not warn');
290308
},
291309
},
310+
appId,
292311
});
293312
await parseGraphQLSchema.load();
294313
delete parseGraphQLSchema.graphQLQueries.viewer;
@@ -314,6 +333,7 @@ describe('ParseGraphQLSchema', () => {
314333
);
315334
},
316335
},
336+
appId,
317337
});
318338
await parseGraphQLSchema.load();
319339
const field = {};
@@ -338,6 +358,7 @@ describe('ParseGraphQLSchema', () => {
338358
fail('Should not warn');
339359
},
340360
},
361+
appId,
341362
});
342363
await parseGraphQLSchema.load();
343364
const field = {};
@@ -367,6 +388,7 @@ describe('ParseGraphQLSchema', () => {
367388
);
368389
},
369390
},
391+
appId,
370392
});
371393
await parseGraphQLSchema.load();
372394
expect(
@@ -384,6 +406,7 @@ describe('ParseGraphQLSchema', () => {
384406
fail('Should not warn');
385407
},
386408
},
409+
appId,
387410
});
388411
await parseGraphQLSchema.load();
389412
delete parseGraphQLSchema.graphQLMutations.signUp;
@@ -405,6 +428,7 @@ describe('ParseGraphQLSchema', () => {
405428
fail('Should not warn');
406429
},
407430
},
431+
appId,
408432
});
409433
expect(
410434
parseGraphQLSchema
@@ -443,6 +467,7 @@ describe('ParseGraphQLSchema', () => {
443467
databaseController,
444468
parseGraphQLController,
445469
log: defaultLogger,
470+
appId,
446471
});
447472
await parseGraphQLSchema.databaseController.schemaCache.clear();
448473
const schema1 = await parseGraphQLSchema.load();
@@ -476,6 +501,7 @@ describe('ParseGraphQLSchema', () => {
476501
databaseController,
477502
parseGraphQLController,
478503
log: defaultLogger,
504+
appId,
479505
});
480506
const car1 = new Parse.Object('Car');
481507
await car1.save();
@@ -511,6 +537,7 @@ describe('ParseGraphQLSchema', () => {
511537
databaseController,
512538
parseGraphQLController,
513539
log: defaultLogger,
540+
appId,
514541
});
515542
const car = new Parse.Object('Car');
516543
await car.save();

spec/ParseGraphQLServer.spec.js

Lines changed: 105 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5177,19 +5177,23 @@ describe('ParseGraphQLServer', () => {
51775177

51785178
describe('Functions Mutations', () => {
51795179
it('can be called', async () => {
5180-
Parse.Cloud.define('hello', async () => {
5181-
return 'Hello world!';
5182-
});
5180+
try {
5181+
Parse.Cloud.define('hello', async () => {
5182+
return 'Hello world!';
5183+
});
51835184

5184-
const result = await apolloClient.mutate({
5185-
mutation: gql`
5186-
mutation CallFunction {
5187-
callCloudCode(functionName: "hello")
5188-
}
5189-
`,
5190-
});
5185+
const result = await apolloClient.mutate({
5186+
mutation: gql`
5187+
mutation CallFunction {
5188+
callCloudCode(functionName: hello)
5189+
}
5190+
`,
5191+
});
51915192

5192-
expect(result.data.callCloudCode).toEqual('Hello world!');
5193+
expect(result.data.callCloudCode).toEqual('Hello world!');
5194+
} catch (e) {
5195+
handleError(e);
5196+
}
51935197
});
51945198

51955199
it('can throw errors', async () => {
@@ -5201,7 +5205,7 @@ describe('ParseGraphQLServer', () => {
52015205
await apolloClient.mutate({
52025206
mutation: gql`
52035207
mutation CallFunction {
5204-
callCloudCode(functionName: "hello")
5208+
callCloudCode(functionName: hello)
52055209
}
52065210
`,
52075211
});
@@ -5302,14 +5306,102 @@ describe('ParseGraphQLServer', () => {
53025306
apolloClient.mutate({
53035307
mutation: gql`
53045308
mutation CallFunction($params: Object) {
5305-
callCloudCode(functionName: "hello", params: $params)
5309+
callCloudCode(functionName: hello, params: $params)
53065310
}
53075311
`,
53085312
variables: {
53095313
params,
53105314
},
53115315
});
53125316
});
5317+
5318+
it('should list all functions in the enum type', async () => {
5319+
try {
5320+
Parse.Cloud.define('a', async () => {
5321+
return 'hello a';
5322+
});
5323+
5324+
Parse.Cloud.define('b', async () => {
5325+
return 'hello b';
5326+
});
5327+
5328+
Parse.Cloud.define('_underscored', async () => {
5329+
return 'hello _underscored';
5330+
});
5331+
5332+
Parse.Cloud.define('contains1Number', async () => {
5333+
return 'hello contains1Number';
5334+
});
5335+
5336+
const functionEnum = (await apolloClient.query({
5337+
query: gql`
5338+
query ObjectType {
5339+
__type(name: "CloudCodeFunction") {
5340+
kind
5341+
enumValues {
5342+
name
5343+
}
5344+
}
5345+
}
5346+
`,
5347+
})).data['__type'];
5348+
expect(functionEnum.kind).toEqual('ENUM');
5349+
expect(
5350+
functionEnum.enumValues.map(value => value.name).sort()
5351+
).toEqual(['_underscored', 'a', 'b', 'contains1Number']);
5352+
} catch (e) {
5353+
handleError(e);
5354+
}
5355+
});
5356+
5357+
it('should warn functions not matching GraphQL allowed names', async () => {
5358+
try {
5359+
spyOn(
5360+
parseGraphQLServer.parseGraphQLSchema.log,
5361+
'warn'
5362+
).and.callThrough();
5363+
5364+
Parse.Cloud.define('a', async () => {
5365+
return 'hello a';
5366+
});
5367+
5368+
Parse.Cloud.define('double-barrelled', async () => {
5369+
return 'hello b';
5370+
});
5371+
5372+
Parse.Cloud.define('1NumberInTheBeggning', async () => {
5373+
return 'hello contains1Number';
5374+
});
5375+
5376+
const functionEnum = (await apolloClient.query({
5377+
query: gql`
5378+
query ObjectType {
5379+
__type(name: "CloudCodeFunction") {
5380+
kind
5381+
enumValues {
5382+
name
5383+
}
5384+
}
5385+
}
5386+
`,
5387+
})).data['__type'];
5388+
expect(functionEnum.kind).toEqual('ENUM');
5389+
expect(
5390+
functionEnum.enumValues.map(value => value.name).sort()
5391+
).toEqual(['a']);
5392+
expect(
5393+
parseGraphQLServer.parseGraphQLSchema.log.warn.calls
5394+
.all()
5395+
.map(call => call.args[0])
5396+
.sort()
5397+
).toEqual([
5398+
'Function 1NumberInTheBeggning could not be added to the auto schema because GraphQL names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/.',
5399+
'Function double-barrelled could not be added to the auto schema because GraphQL names must match /^[_a-zA-Z][_a-zA-Z0-9]*$/.',
5400+
]);
5401+
} catch (e) {
5402+
handleError(e);
5403+
}
5404+
});
53135405
});
53145406

53155407
describe('Data Types', () => {

0 commit comments

Comments
 (0)