Skip to content

Commit 3e2003e

Browse files
BufferUnderflowerdavimacedo
authored andcommitted
GraphQL alias for mutations in classConfigs (parse-community#6258)
* mutations * removed duplicate tests
1 parent 2e8a613 commit 3e2003e

File tree

4 files changed

+146
-51
lines changed

4 files changed

+146
-51
lines changed

spec/ParseGraphQLController.spec.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -970,4 +970,102 @@ describe('ParseGraphQLController', () => {
970970
expect(cacheAfterValue).toBeNull();
971971
});
972972
});
973+
974+
describe('alias', () => {
975+
it('should fail if query alias is not a string', async () => {
976+
const parseGraphQLController = new ParseGraphQLController({
977+
databaseController,
978+
});
979+
980+
const className = 'Bar';
981+
982+
expectAsync(
983+
parseGraphQLController.updateGraphQLConfig({
984+
classConfigs: [
985+
{
986+
className,
987+
query: {
988+
get: true,
989+
getAlias: 1,
990+
},
991+
},
992+
],
993+
})
994+
).toBeRejected(
995+
`Invalid graphQLConfig: classConfig:${className} is invalid because "query.getAlias" must be a string`
996+
);
997+
998+
expectAsync(
999+
parseGraphQLController.updateGraphQLConfig({
1000+
classConfigs: [
1001+
{
1002+
className,
1003+
query: {
1004+
find: true,
1005+
findAlias: { not: 'valid' },
1006+
},
1007+
},
1008+
],
1009+
})
1010+
).toBeRejected(
1011+
`Invalid graphQLConfig: classConfig:${className} is invalid because "query.findAlias" must be a string`
1012+
);
1013+
});
1014+
1015+
it('should fail if mutation alias is not a string', async () => {
1016+
const parseGraphQLController = new ParseGraphQLController({
1017+
databaseController,
1018+
});
1019+
1020+
const className = 'Bar';
1021+
1022+
expectAsync(
1023+
parseGraphQLController.updateGraphQLConfig({
1024+
classConfigs: [
1025+
{
1026+
className,
1027+
mutation: {
1028+
create: true,
1029+
createAlias: true,
1030+
},
1031+
},
1032+
],
1033+
})
1034+
).toBeRejected(
1035+
`Invalid graphQLConfig: classConfig:${className} is invalid because "mutation.createAlias" must be a string`
1036+
);
1037+
1038+
expectAsync(
1039+
parseGraphQLController.updateGraphQLConfig({
1040+
classConfigs: [
1041+
{
1042+
className,
1043+
mutation: {
1044+
update: true,
1045+
updateAlias: 1,
1046+
},
1047+
},
1048+
],
1049+
})
1050+
).toBeRejected(
1051+
`Invalid graphQLConfig: classConfig:${className} is invalid because "mutation.updateAlias" must be a string`
1052+
);
1053+
1054+
expectAsync(
1055+
parseGraphQLController.updateGraphQLConfig({
1056+
classConfigs: [
1057+
{
1058+
className,
1059+
mutation: {
1060+
destroy: true,
1061+
destroyAlias: { not: 'valid' },
1062+
},
1063+
},
1064+
],
1065+
})
1066+
).toBeRejected(
1067+
`Invalid graphQLConfig: classConfig:${className} is invalid because "mutation.destroyAlias" must be a string`
1068+
);
1069+
});
1070+
});
9731071
});

spec/ParseGraphQLSchema.spec.js

Lines changed: 24 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -564,9 +564,8 @@ describe('ParseGraphQLSchema', () => {
564564
).toEqual(Object.keys(mutations2).sort());
565565
});
566566
});
567-
568-
describe('query alias', () => {
569-
it('Should be able to define alias for find query', async () => {
567+
describe('alias', () => {
568+
it('Should be able to define alias for get and find query', async () => {
570569
const parseGraphQLSchema = new ParseGraphQLSchema({
571570
databaseController,
572571
parseGraphQLController,
@@ -601,65 +600,42 @@ describe('ParseGraphQLSchema', () => {
601600
expect(Object.keys(queries1)).toContain('precious_data');
602601
});
603602

604-
it('should fail if query alias is not a string', async () => {
603+
it('Should be able to define alias for mutation', async () => {
605604
const parseGraphQLSchema = new ParseGraphQLSchema({
606605
databaseController,
607606
parseGraphQLController,
608607
log: defaultLogger,
609608
appId,
610609
});
611610

612-
const classConfigsBoolFindAlias = {
611+
await parseGraphQLSchema.parseGraphQLController.updateGraphQLConfig({
613612
classConfigs: [
614613
{
615-
className: 'Data',
616-
query: {
617-
get: true,
618-
getAlias: 'valid',
619-
find: true,
620-
findAlias: true,
614+
className: 'Track',
615+
mutation: {
616+
create: true,
617+
createAlias: 'addTrack',
618+
update: true,
619+
updateAlias: 'modifyTrack',
620+
destroy: true,
621+
destroyAlias: 'eraseTrack',
621622
},
622623
},
623624
],
624-
};
625+
});
625626

626-
const classConfigsNumberGetAlias = {
627-
classConfigs: [
628-
{
629-
className: 'Pants',
630-
query: {
631-
get: true,
632-
getAlias: 1,
633-
find: true,
634-
findAlias: 'valid',
635-
},
636-
},
637-
],
638-
};
627+
const data = new Parse.Object('Track');
628+
629+
await data.save();
630+
631+
await parseGraphQLSchema.databaseController.schemaCache.clear();
632+
await parseGraphQLSchema.load();
633+
634+
const mutations = parseGraphQLSchema.graphQLMutations;
639635

640-
let className;
641-
642-
className = classConfigsBoolFindAlias.classConfigs[0].className;
643-
try {
644-
await parseGraphQLSchema.parseGraphQLController.updateGraphQLConfig(
645-
classConfigsBoolFindAlias
646-
);
647-
} catch (e) {
648-
expect(e).toMatch(
649-
`Invalid graphQLConfig: classConfig:${className} is invalid because "query.findAlias" must be a string`
650-
);
651-
}
652-
653-
className = classConfigsNumberGetAlias.classConfigs[0].className;
654-
try {
655-
await parseGraphQLSchema.parseGraphQLController.updateGraphQLConfig(
656-
classConfigsNumberGetAlias
657-
);
658-
} catch (e) {
659-
expect(e).toMatch(
660-
`Invalid graphQLConfig: classConfig:${className} is invalid because "query.getAlias" must be a string`
661-
);
662-
}
636+
expect(Object.keys(mutations)).toContain('addTrack');
637+
expect(Object.keys(mutations)).toContain('modifyTrack');
638+
expect(Object.keys(mutations)).toContain('eraseTrack');
663639
});
664640
});
665641
});

src/Controllers/ParseGraphQLController.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -296,6 +296,9 @@ class ParseGraphQLController {
296296
create = null,
297297
update = null,
298298
destroy = null,
299+
createAlias = null,
300+
updateAlias = null,
301+
destroyAlias = null,
299302
...invalidKeys
300303
} = mutation;
301304
if (Object.keys(invalidKeys).length) {
@@ -312,6 +315,15 @@ class ParseGraphQLController {
312315
if (destroy !== null && typeof destroy !== 'boolean') {
313316
return `"mutation.destroy" must be a boolean`;
314317
}
318+
if (createAlias !== null && typeof createAlias !== 'string') {
319+
return `"mutation.createAlias" must be a string`;
320+
}
321+
if (updateAlias !== null && typeof updateAlias !== 'string') {
322+
return `"mutation.updateAlias" must be a string`;
323+
}
324+
if (destroyAlias !== null && typeof destroyAlias !== 'string') {
325+
return `"mutation.destroyAlias" must be a string`;
326+
}
315327
} else {
316328
return `"mutation" must be a valid object`;
317329
}
@@ -380,6 +392,9 @@ export interface ParseGraphQLClassConfig {
380392
update: ?boolean,
381393
// delete is a reserved key word in js
382394
destroy: ?boolean,
395+
createAlias: ?String,
396+
updateAlias: ?String,
397+
destroyAlias: ?String,
383398
};
384399
}
385400

src/GraphQL/loaders/parseClassMutations.js

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@ const load = function(
5151
create: isCreateEnabled = true,
5252
update: isUpdateEnabled = true,
5353
destroy: isDestroyEnabled = true,
54+
createAlias: createAlias = '',
55+
updateAlias: updateAlias = '',
56+
destroyAlias: destroyAlias = '',
5457
} = getParseClassMutationConfig(parseClassConfig);
5558

5659
const {
@@ -60,7 +63,8 @@ const load = function(
6063
} = parseGraphQLSchema.parseClassTypes[className];
6164

6265
if (isCreateEnabled) {
63-
const createGraphQLMutationName = `create${graphQLClassName}`;
66+
const createGraphQLMutationName =
67+
createAlias || `create${graphQLClassName}`;
6468
const createGraphQLMutation = mutationWithClientMutationId({
6569
name: `Create${graphQLClassName}`,
6670
description: `The ${createGraphQLMutationName} mutation can be used to create a new object of the ${graphQLClassName} class.`,
@@ -150,7 +154,8 @@ const load = function(
150154
}
151155

152156
if (isUpdateEnabled) {
153-
const updateGraphQLMutationName = `update${graphQLClassName}`;
157+
const updateGraphQLMutationName =
158+
updateAlias || `update${graphQLClassName}`;
154159
const updateGraphQLMutation = mutationWithClientMutationId({
155160
name: `Update${graphQLClassName}`,
156161
description: `The ${updateGraphQLMutationName} mutation can be used to update an object of the ${graphQLClassName} class.`,
@@ -250,7 +255,8 @@ const load = function(
250255
}
251256

252257
if (isDestroyEnabled) {
253-
const deleteGraphQLMutationName = `delete${graphQLClassName}`;
258+
const deleteGraphQLMutationName =
259+
destroyAlias || `delete${graphQLClassName}`;
254260
const deleteGraphQLMutation = mutationWithClientMutationId({
255261
name: `Delete${graphQLClassName}`,
256262
description: `The ${deleteGraphQLMutationName} mutation can be used to delete an object of the ${graphQLClassName} class.`,

0 commit comments

Comments
 (0)