GraphQL alias for mutations in classConfigs (#6258)
* mutations * removed duplicate tests
This commit is contained in:
committed by
Antonio Davi Macedo Coelho de Castro
parent
917c6718fc
commit
6db040bdec
@@ -970,4 +970,102 @@ describe('ParseGraphQLController', () => {
|
|||||||
expect(cacheAfterValue).toBeNull();
|
expect(cacheAfterValue).toBeNull();
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('alias', () => {
|
||||||
|
it('should fail if query alias is not a string', async () => {
|
||||||
|
const parseGraphQLController = new ParseGraphQLController({
|
||||||
|
databaseController,
|
||||||
|
});
|
||||||
|
|
||||||
|
const className = 'Bar';
|
||||||
|
|
||||||
|
expectAsync(
|
||||||
|
parseGraphQLController.updateGraphQLConfig({
|
||||||
|
classConfigs: [
|
||||||
|
{
|
||||||
|
className,
|
||||||
|
query: {
|
||||||
|
get: true,
|
||||||
|
getAlias: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
).toBeRejected(
|
||||||
|
`Invalid graphQLConfig: classConfig:${className} is invalid because "query.getAlias" must be a string`
|
||||||
|
);
|
||||||
|
|
||||||
|
expectAsync(
|
||||||
|
parseGraphQLController.updateGraphQLConfig({
|
||||||
|
classConfigs: [
|
||||||
|
{
|
||||||
|
className,
|
||||||
|
query: {
|
||||||
|
find: true,
|
||||||
|
findAlias: { not: 'valid' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
).toBeRejected(
|
||||||
|
`Invalid graphQLConfig: classConfig:${className} is invalid because "query.findAlias" must be a string`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should fail if mutation alias is not a string', async () => {
|
||||||
|
const parseGraphQLController = new ParseGraphQLController({
|
||||||
|
databaseController,
|
||||||
|
});
|
||||||
|
|
||||||
|
const className = 'Bar';
|
||||||
|
|
||||||
|
expectAsync(
|
||||||
|
parseGraphQLController.updateGraphQLConfig({
|
||||||
|
classConfigs: [
|
||||||
|
{
|
||||||
|
className,
|
||||||
|
mutation: {
|
||||||
|
create: true,
|
||||||
|
createAlias: true,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
).toBeRejected(
|
||||||
|
`Invalid graphQLConfig: classConfig:${className} is invalid because "mutation.createAlias" must be a string`
|
||||||
|
);
|
||||||
|
|
||||||
|
expectAsync(
|
||||||
|
parseGraphQLController.updateGraphQLConfig({
|
||||||
|
classConfigs: [
|
||||||
|
{
|
||||||
|
className,
|
||||||
|
mutation: {
|
||||||
|
update: true,
|
||||||
|
updateAlias: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
).toBeRejected(
|
||||||
|
`Invalid graphQLConfig: classConfig:${className} is invalid because "mutation.updateAlias" must be a string`
|
||||||
|
);
|
||||||
|
|
||||||
|
expectAsync(
|
||||||
|
parseGraphQLController.updateGraphQLConfig({
|
||||||
|
classConfigs: [
|
||||||
|
{
|
||||||
|
className,
|
||||||
|
mutation: {
|
||||||
|
destroy: true,
|
||||||
|
destroyAlias: { not: 'valid' },
|
||||||
|
},
|
||||||
|
},
|
||||||
|
],
|
||||||
|
})
|
||||||
|
).toBeRejected(
|
||||||
|
`Invalid graphQLConfig: classConfig:${className} is invalid because "mutation.destroyAlias" must be a string`
|
||||||
|
);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -564,9 +564,8 @@ describe('ParseGraphQLSchema', () => {
|
|||||||
).toEqual(Object.keys(mutations2).sort());
|
).toEqual(Object.keys(mutations2).sort());
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
describe('alias', () => {
|
||||||
describe('query alias', () => {
|
it('Should be able to define alias for get and find query', async () => {
|
||||||
it('Should be able to define alias for find query', async () => {
|
|
||||||
const parseGraphQLSchema = new ParseGraphQLSchema({
|
const parseGraphQLSchema = new ParseGraphQLSchema({
|
||||||
databaseController,
|
databaseController,
|
||||||
parseGraphQLController,
|
parseGraphQLController,
|
||||||
@@ -601,7 +600,7 @@ describe('ParseGraphQLSchema', () => {
|
|||||||
expect(Object.keys(queries1)).toContain('precious_data');
|
expect(Object.keys(queries1)).toContain('precious_data');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should fail if query alias is not a string', async () => {
|
it('Should be able to define alias for mutation', async () => {
|
||||||
const parseGraphQLSchema = new ParseGraphQLSchema({
|
const parseGraphQLSchema = new ParseGraphQLSchema({
|
||||||
databaseController,
|
databaseController,
|
||||||
parseGraphQLController,
|
parseGraphQLController,
|
||||||
@@ -609,57 +608,34 @@ describe('ParseGraphQLSchema', () => {
|
|||||||
appId,
|
appId,
|
||||||
});
|
});
|
||||||
|
|
||||||
const classConfigsBoolFindAlias = {
|
await parseGraphQLSchema.parseGraphQLController.updateGraphQLConfig({
|
||||||
classConfigs: [
|
classConfigs: [
|
||||||
{
|
{
|
||||||
className: 'Data',
|
className: 'Track',
|
||||||
query: {
|
mutation: {
|
||||||
get: true,
|
create: true,
|
||||||
getAlias: 'valid',
|
createAlias: 'addTrack',
|
||||||
find: true,
|
update: true,
|
||||||
findAlias: true,
|
updateAlias: 'modifyTrack',
|
||||||
|
destroy: true,
|
||||||
|
destroyAlias: 'eraseTrack',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
};
|
});
|
||||||
|
|
||||||
const classConfigsNumberGetAlias = {
|
const data = new Parse.Object('Track');
|
||||||
classConfigs: [
|
|
||||||
{
|
|
||||||
className: 'Pants',
|
|
||||||
query: {
|
|
||||||
get: true,
|
|
||||||
getAlias: 1,
|
|
||||||
find: true,
|
|
||||||
findAlias: 'valid',
|
|
||||||
},
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
let className;
|
await data.save();
|
||||||
|
|
||||||
className = classConfigsBoolFindAlias.classConfigs[0].className;
|
await parseGraphQLSchema.databaseController.schemaCache.clear();
|
||||||
try {
|
await parseGraphQLSchema.load();
|
||||||
await parseGraphQLSchema.parseGraphQLController.updateGraphQLConfig(
|
|
||||||
classConfigsBoolFindAlias
|
|
||||||
);
|
|
||||||
} catch (e) {
|
|
||||||
expect(e).toMatch(
|
|
||||||
`Invalid graphQLConfig: classConfig:${className} is invalid because "query.findAlias" must be a string`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
className = classConfigsNumberGetAlias.classConfigs[0].className;
|
const mutations = parseGraphQLSchema.graphQLMutations;
|
||||||
try {
|
|
||||||
await parseGraphQLSchema.parseGraphQLController.updateGraphQLConfig(
|
expect(Object.keys(mutations)).toContain('addTrack');
|
||||||
classConfigsNumberGetAlias
|
expect(Object.keys(mutations)).toContain('modifyTrack');
|
||||||
);
|
expect(Object.keys(mutations)).toContain('eraseTrack');
|
||||||
} catch (e) {
|
|
||||||
expect(e).toMatch(
|
|
||||||
`Invalid graphQLConfig: classConfig:${className} is invalid because "query.getAlias" must be a string`
|
|
||||||
);
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -296,6 +296,9 @@ class ParseGraphQLController {
|
|||||||
create = null,
|
create = null,
|
||||||
update = null,
|
update = null,
|
||||||
destroy = null,
|
destroy = null,
|
||||||
|
createAlias = null,
|
||||||
|
updateAlias = null,
|
||||||
|
destroyAlias = null,
|
||||||
...invalidKeys
|
...invalidKeys
|
||||||
} = mutation;
|
} = mutation;
|
||||||
if (Object.keys(invalidKeys).length) {
|
if (Object.keys(invalidKeys).length) {
|
||||||
@@ -312,6 +315,15 @@ class ParseGraphQLController {
|
|||||||
if (destroy !== null && typeof destroy !== 'boolean') {
|
if (destroy !== null && typeof destroy !== 'boolean') {
|
||||||
return `"mutation.destroy" must be a boolean`;
|
return `"mutation.destroy" must be a boolean`;
|
||||||
}
|
}
|
||||||
|
if (createAlias !== null && typeof createAlias !== 'string') {
|
||||||
|
return `"mutation.createAlias" must be a string`;
|
||||||
|
}
|
||||||
|
if (updateAlias !== null && typeof updateAlias !== 'string') {
|
||||||
|
return `"mutation.updateAlias" must be a string`;
|
||||||
|
}
|
||||||
|
if (destroyAlias !== null && typeof destroyAlias !== 'string') {
|
||||||
|
return `"mutation.destroyAlias" must be a string`;
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
return `"mutation" must be a valid object`;
|
return `"mutation" must be a valid object`;
|
||||||
}
|
}
|
||||||
@@ -380,6 +392,9 @@ export interface ParseGraphQLClassConfig {
|
|||||||
update: ?boolean,
|
update: ?boolean,
|
||||||
// delete is a reserved key word in js
|
// delete is a reserved key word in js
|
||||||
destroy: ?boolean,
|
destroy: ?boolean,
|
||||||
|
createAlias: ?String,
|
||||||
|
updateAlias: ?String,
|
||||||
|
destroyAlias: ?String,
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,9 @@ const load = function(
|
|||||||
create: isCreateEnabled = true,
|
create: isCreateEnabled = true,
|
||||||
update: isUpdateEnabled = true,
|
update: isUpdateEnabled = true,
|
||||||
destroy: isDestroyEnabled = true,
|
destroy: isDestroyEnabled = true,
|
||||||
|
createAlias: createAlias = '',
|
||||||
|
updateAlias: updateAlias = '',
|
||||||
|
destroyAlias: destroyAlias = '',
|
||||||
} = getParseClassMutationConfig(parseClassConfig);
|
} = getParseClassMutationConfig(parseClassConfig);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
@@ -60,7 +63,8 @@ const load = function(
|
|||||||
} = parseGraphQLSchema.parseClassTypes[className];
|
} = parseGraphQLSchema.parseClassTypes[className];
|
||||||
|
|
||||||
if (isCreateEnabled) {
|
if (isCreateEnabled) {
|
||||||
const createGraphQLMutationName = `create${graphQLClassName}`;
|
const createGraphQLMutationName =
|
||||||
|
createAlias || `create${graphQLClassName}`;
|
||||||
const createGraphQLMutation = mutationWithClientMutationId({
|
const createGraphQLMutation = mutationWithClientMutationId({
|
||||||
name: `Create${graphQLClassName}`,
|
name: `Create${graphQLClassName}`,
|
||||||
description: `The ${createGraphQLMutationName} mutation can be used to create a new object of the ${graphQLClassName} class.`,
|
description: `The ${createGraphQLMutationName} mutation can be used to create a new object of the ${graphQLClassName} class.`,
|
||||||
@@ -150,7 +154,8 @@ const load = function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isUpdateEnabled) {
|
if (isUpdateEnabled) {
|
||||||
const updateGraphQLMutationName = `update${graphQLClassName}`;
|
const updateGraphQLMutationName =
|
||||||
|
updateAlias || `update${graphQLClassName}`;
|
||||||
const updateGraphQLMutation = mutationWithClientMutationId({
|
const updateGraphQLMutation = mutationWithClientMutationId({
|
||||||
name: `Update${graphQLClassName}`,
|
name: `Update${graphQLClassName}`,
|
||||||
description: `The ${updateGraphQLMutationName} mutation can be used to update an object of the ${graphQLClassName} class.`,
|
description: `The ${updateGraphQLMutationName} mutation can be used to update an object of the ${graphQLClassName} class.`,
|
||||||
@@ -250,7 +255,8 @@ const load = function(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (isDestroyEnabled) {
|
if (isDestroyEnabled) {
|
||||||
const deleteGraphQLMutationName = `delete${graphQLClassName}`;
|
const deleteGraphQLMutationName =
|
||||||
|
destroyAlias || `delete${graphQLClassName}`;
|
||||||
const deleteGraphQLMutation = mutationWithClientMutationId({
|
const deleteGraphQLMutation = mutationWithClientMutationId({
|
||||||
name: `Delete${graphQLClassName}`,
|
name: `Delete${graphQLClassName}`,
|
||||||
description: `The ${deleteGraphQLMutationName} mutation can be used to delete an object of the ${graphQLClassName} class.`,
|
description: `The ${deleteGraphQLMutationName} mutation can be used to delete an object of the ${graphQLClassName} class.`,
|
||||||
|
|||||||
Reference in New Issue
Block a user