Skip to content

Commit 188f033

Browse files
BufferUnderflowerdavimacedo
authored andcommitted
GraphQL classConfig query alias (#6257)
* adds alias option * added tests
1 parent abcc5fd commit 188f033

File tree

3 files changed

+122
-5
lines changed

3 files changed

+122
-5
lines changed

spec/ParseGraphQLSchema.spec.js

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,4 +564,102 @@ 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 () => {
570+
const parseGraphQLSchema = new ParseGraphQLSchema({
571+
databaseController,
572+
parseGraphQLController,
573+
log: defaultLogger,
574+
appId,
575+
});
576+
577+
await parseGraphQLSchema.parseGraphQLController.updateGraphQLConfig({
578+
classConfigs: [
579+
{
580+
className: 'Data',
581+
query: {
582+
get: true,
583+
getAlias: 'precious_data',
584+
find: true,
585+
findAlias: 'data_results',
586+
},
587+
},
588+
],
589+
});
590+
591+
const data = new Parse.Object('Data');
592+
593+
await data.save();
594+
595+
await parseGraphQLSchema.databaseController.schemaCache.clear();
596+
await parseGraphQLSchema.load();
597+
598+
const queries1 = parseGraphQLSchema.graphQLQueries;
599+
600+
expect(Object.keys(queries1)).toContain('data_results');
601+
expect(Object.keys(queries1)).toContain('precious_data');
602+
});
603+
604+
it('should fail if query alias is not a string', async () => {
605+
const parseGraphQLSchema = new ParseGraphQLSchema({
606+
databaseController,
607+
parseGraphQLController,
608+
log: defaultLogger,
609+
appId,
610+
});
611+
612+
const classConfigsBoolFindAlias = {
613+
classConfigs: [
614+
{
615+
className: 'Data',
616+
query: {
617+
get: true,
618+
getAlias: 'valid',
619+
find: true,
620+
findAlias: true,
621+
},
622+
},
623+
],
624+
};
625+
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+
};
639+
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+
}
663+
});
664+
});
567665
});

src/Controllers/ParseGraphQLController.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,13 @@ class ParseGraphQLController {
266266
}
267267
if (query !== null) {
268268
if (isValidSimpleObject(query)) {
269-
const { find = null, get = null, ...invalidKeys } = query;
269+
const {
270+
find = null,
271+
get = null,
272+
findAlias = null,
273+
getAlias = null,
274+
...invalidKeys
275+
} = query;
270276
if (Object.keys(invalidKeys).length) {
271277
return `"query" contains invalid keys, [${Object.keys(
272278
invalidKeys
@@ -275,6 +281,10 @@ class ParseGraphQLController {
275281
return `"query.find" must be a boolean`;
276282
} else if (get !== null && typeof get !== 'boolean') {
277283
return `"query.get" must be a boolean`;
284+
} else if (findAlias !== null && typeof findAlias !== 'string') {
285+
return `"query.findAlias" must be a string`;
286+
} else if (getAlias !== null && typeof getAlias !== 'string') {
287+
return `"query.getAlias" must be a string`;
278288
}
279289
} else {
280290
return `"query" must be a valid object`;
@@ -361,6 +371,8 @@ export interface ParseGraphQLClassConfig {
361371
query: ?{
362372
get: ?boolean,
363373
find: ?boolean,
374+
findAlias: ?String,
375+
getAlias: ?String,
364376
};
365377
/* The `mutation` object contains options for which class mutations are generated */
366378
mutation: ?{

src/GraphQL/loaders/parseClassQueries.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ const load = function(
5252
const {
5353
get: isGetEnabled = true,
5454
find: isFindEnabled = true,
55+
getAlias: getAlias = '',
56+
findAlias: findAlias = '',
5557
} = getParseClassQueryConfig(parseClassConfig);
5658

5759
const {
@@ -61,8 +63,11 @@ const load = function(
6163
} = parseGraphQLSchema.parseClassTypes[className];
6264

6365
if (isGetEnabled) {
64-
const getGraphQLQueryName =
66+
const lowerCaseClassName =
6567
graphQLClassName.charAt(0).toLowerCase() + graphQLClassName.slice(1);
68+
69+
const getGraphQLQueryName = getAlias || lowerCaseClassName;
70+
6671
parseGraphQLSchema.addGraphQLQuery(getGraphQLQueryName, {
6772
description: `The ${getGraphQLQueryName} query can be used to get an object of the ${graphQLClassName} class by its id.`,
6873
args: {
@@ -83,9 +88,11 @@ const load = function(
8388
}
8489

8590
if (isFindEnabled) {
86-
const findGraphQLQueryName = pluralize(
87-
graphQLClassName.charAt(0).toLowerCase() + graphQLClassName.slice(1)
88-
);
91+
const lowerCaseClassName =
92+
graphQLClassName.charAt(0).toLowerCase() + graphQLClassName.slice(1);
93+
94+
const findGraphQLQueryName = findAlias || pluralize(lowerCaseClassName);
95+
8996
parseGraphQLSchema.addGraphQLQuery(findGraphQLQueryName, {
9097
description: `The ${findGraphQLQueryName} query can be used to find objects of the ${graphQLClassName} class.`,
9198
args: classGraphQLFindArgs,

0 commit comments

Comments
 (0)