Return specific Type on specific Mutation (#5893)
* Return specific Type on specific Mutation * Add Optimization on Mutation * Optimize SignUp
This commit is contained in:
committed by
Antonio Davi Macedo Coelho de Castro
parent
6f6210387e
commit
9031961e86
@@ -1,6 +1,9 @@
|
||||
import { GraphQLNonNull, GraphQLBoolean } from 'graphql';
|
||||
import { GraphQLNonNull } from 'graphql';
|
||||
import getFieldNames from 'graphql-list-fields';
|
||||
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
||||
import * as parseClassTypes from './parseClassTypes';
|
||||
import * as objectsMutations from './objectsMutations';
|
||||
import * as objectsQueries from './objectsQueries';
|
||||
import { ParseGraphQLClassConfig } from '../../Controllers/ParseGraphQLController';
|
||||
|
||||
const getParseClassMutationConfig = function(
|
||||
@@ -9,6 +12,28 @@ const getParseClassMutationConfig = function(
|
||||
return (parseClassConfig && parseClassConfig.mutation) || {};
|
||||
};
|
||||
|
||||
const getOnlyRequiredFields = (
|
||||
updatedFields,
|
||||
selectedFieldsString,
|
||||
includedFieldsString,
|
||||
nativeObjectFields
|
||||
) => {
|
||||
const includedFields = includedFieldsString.split(',');
|
||||
const selectedFields = selectedFieldsString.split(',');
|
||||
const missingFields = selectedFields
|
||||
.filter(
|
||||
field =>
|
||||
(!updatedFields[field] && !nativeObjectFields.includes(field)) ||
|
||||
includedFields.includes(field)
|
||||
)
|
||||
.join(',');
|
||||
if (!missingFields.length) {
|
||||
return { needGet: false, keys: '' };
|
||||
} else {
|
||||
return { needGet: true, keys: missingFields };
|
||||
}
|
||||
};
|
||||
|
||||
const load = function(
|
||||
parseGraphQLSchema,
|
||||
parseClass,
|
||||
@@ -24,6 +49,7 @@ const load = function(
|
||||
const {
|
||||
classGraphQLCreateType,
|
||||
classGraphQLUpdateType,
|
||||
classGraphQLOutputType,
|
||||
} = parseGraphQLSchema.parseClassTypes[className];
|
||||
|
||||
const createFields = {
|
||||
@@ -78,21 +104,50 @@ const load = function(
|
||||
args: {
|
||||
fields: createFields,
|
||||
},
|
||||
type: new GraphQLNonNull(defaultGraphQLTypes.CREATE_RESULT),
|
||||
async resolve(_source, args, context) {
|
||||
type: new GraphQLNonNull(classGraphQLOutputType),
|
||||
async resolve(_source, args, context, mutationInfo) {
|
||||
try {
|
||||
const { fields } = args;
|
||||
let { fields } = args;
|
||||
if (!fields) fields = {};
|
||||
const { config, auth, info } = context;
|
||||
|
||||
transformTypes('create', fields);
|
||||
|
||||
return await objectsMutations.createObject(
|
||||
const createdObject = await objectsMutations.createObject(
|
||||
className,
|
||||
fields,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
const selectedFields = getFieldNames(mutationInfo);
|
||||
const { keys, include } = parseClassTypes.extractKeysAndInclude(
|
||||
selectedFields
|
||||
);
|
||||
const { keys: requiredKeys, needGet } = getOnlyRequiredFields(
|
||||
fields,
|
||||
keys,
|
||||
include,
|
||||
['objectId', 'createdAt', 'updatedAt']
|
||||
);
|
||||
let optimizedObject = {};
|
||||
if (needGet) {
|
||||
optimizedObject = await objectsQueries.getObject(
|
||||
className,
|
||||
createdObject.objectId,
|
||||
requiredKeys,
|
||||
include,
|
||||
undefined,
|
||||
undefined,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
}
|
||||
return {
|
||||
...createdObject,
|
||||
updatedAt: createdObject.createdAt,
|
||||
...fields,
|
||||
...optimizedObject,
|
||||
};
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
@@ -108,15 +163,15 @@ const load = function(
|
||||
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
|
||||
fields: updateFields,
|
||||
},
|
||||
type: defaultGraphQLTypes.UPDATE_RESULT,
|
||||
async resolve(_source, args, context) {
|
||||
type: new GraphQLNonNull(classGraphQLOutputType),
|
||||
async resolve(_source, args, context, mutationInfo) {
|
||||
try {
|
||||
const { objectId, fields } = args;
|
||||
const { config, auth, info } = context;
|
||||
|
||||
transformTypes('update', fields);
|
||||
|
||||
return await objectsMutations.updateObject(
|
||||
const updatedObject = await objectsMutations.updateObject(
|
||||
className,
|
||||
objectId,
|
||||
fields,
|
||||
@@ -124,6 +179,32 @@ const load = function(
|
||||
auth,
|
||||
info
|
||||
);
|
||||
const selectedFields = getFieldNames(mutationInfo);
|
||||
const { keys, include } = parseClassTypes.extractKeysAndInclude(
|
||||
selectedFields
|
||||
);
|
||||
|
||||
const { keys: requiredKeys, needGet } = getOnlyRequiredFields(
|
||||
fields,
|
||||
keys,
|
||||
include,
|
||||
['objectId', 'updatedAt']
|
||||
);
|
||||
let optimizedObject = {};
|
||||
if (needGet) {
|
||||
optimizedObject = await objectsQueries.getObject(
|
||||
className,
|
||||
objectId,
|
||||
requiredKeys,
|
||||
include,
|
||||
undefined,
|
||||
undefined,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
}
|
||||
return { ...updatedObject, ...fields, ...optimizedObject };
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
@@ -138,19 +219,39 @@ const load = function(
|
||||
args: {
|
||||
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
|
||||
},
|
||||
type: new GraphQLNonNull(GraphQLBoolean),
|
||||
async resolve(_source, args, context) {
|
||||
type: new GraphQLNonNull(classGraphQLOutputType),
|
||||
async resolve(_source, args, context, mutationInfo) {
|
||||
try {
|
||||
const { objectId } = args;
|
||||
const { config, auth, info } = context;
|
||||
const selectedFields = getFieldNames(mutationInfo);
|
||||
const { keys, include } = parseClassTypes.extractKeysAndInclude(
|
||||
selectedFields
|
||||
);
|
||||
|
||||
return await objectsMutations.deleteObject(
|
||||
let optimizedObject = {};
|
||||
const splitedKeys = keys.split(',');
|
||||
if (splitedKeys.length > 1 || splitedKeys[0] !== 'objectId') {
|
||||
optimizedObject = await objectsQueries.getObject(
|
||||
className,
|
||||
objectId,
|
||||
keys,
|
||||
include,
|
||||
undefined,
|
||||
undefined,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
}
|
||||
await objectsMutations.deleteObject(
|
||||
className,
|
||||
objectId,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
return { objectId: objectId, ...optimizedObject };
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
|
||||
@@ -715,9 +715,28 @@ const load = (
|
||||
}
|
||||
}, {}),
|
||||
});
|
||||
|
||||
const userLogInInputTypeName = '_UserLoginFields';
|
||||
const userLogInInputType = new GraphQLInputObjectType({
|
||||
name: userLogInInputTypeName,
|
||||
description: `The ${userLogInInputTypeName} input type is used to login.`,
|
||||
fields: {
|
||||
username: {
|
||||
description: 'This is the username used to log the user in.',
|
||||
type: new GraphQLNonNull(GraphQLString),
|
||||
},
|
||||
password: {
|
||||
description: 'This is the password used to log the user in.',
|
||||
type: new GraphQLNonNull(GraphQLString),
|
||||
},
|
||||
},
|
||||
});
|
||||
parseGraphQLSchema.parseClassTypes[
|
||||
'_User'
|
||||
].signUpInputType = userSignUpInputType;
|
||||
parseGraphQLSchema.parseClassTypes[
|
||||
'_User'
|
||||
].logInInputType = userLogInInputType;
|
||||
parseGraphQLSchema.graphQLTypes.push(userSignUpInputType);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -1,12 +1,7 @@
|
||||
import {
|
||||
GraphQLBoolean,
|
||||
GraphQLNonNull,
|
||||
GraphQLObjectType,
|
||||
GraphQLString,
|
||||
} from 'graphql';
|
||||
import { GraphQLBoolean, GraphQLNonNull, GraphQLObjectType } from 'graphql';
|
||||
import UsersRouter from '../../Routers/UsersRouter';
|
||||
import * as defaultGraphQLTypes from './defaultGraphQLTypes';
|
||||
import * as objectsMutations from './objectsMutations';
|
||||
import { getUserFromSessionToken } from './usersQueries';
|
||||
|
||||
const usersRouter = new UsersRouter();
|
||||
|
||||
@@ -24,19 +19,23 @@ const load = parseGraphQLSchema => {
|
||||
type: parseGraphQLSchema.parseClassTypes['_User'].signUpInputType,
|
||||
},
|
||||
},
|
||||
type: new GraphQLNonNull(defaultGraphQLTypes.SIGN_UP_RESULT),
|
||||
async resolve(_source, args, context) {
|
||||
type: new GraphQLNonNull(parseGraphQLSchema.meType),
|
||||
async resolve(_source, args, context, mutationInfo) {
|
||||
try {
|
||||
const { fields } = args;
|
||||
const { config, auth, info } = context;
|
||||
|
||||
return await objectsMutations.createObject(
|
||||
const { sessionToken } = await objectsMutations.createObject(
|
||||
'_User',
|
||||
fields,
|
||||
config,
|
||||
auth,
|
||||
info
|
||||
);
|
||||
|
||||
info.sessionToken = sessionToken;
|
||||
|
||||
return await getUserFromSessionToken(config, info, mutationInfo);
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
@@ -46,19 +45,17 @@ const load = parseGraphQLSchema => {
|
||||
fields.logIn = {
|
||||
description: 'The logIn mutation can be used to log the user in.',
|
||||
args: {
|
||||
username: {
|
||||
description: 'This is the username used to log the user in.',
|
||||
type: new GraphQLNonNull(GraphQLString),
|
||||
},
|
||||
password: {
|
||||
description: 'This is the password used to log the user in.',
|
||||
type: new GraphQLNonNull(GraphQLString),
|
||||
input: {
|
||||
description: 'This is data needed to login',
|
||||
type: parseGraphQLSchema.parseClassTypes['_User'].logInInputType,
|
||||
},
|
||||
},
|
||||
type: new GraphQLNonNull(parseGraphQLSchema.meType),
|
||||
async resolve(_source, args, context) {
|
||||
try {
|
||||
const { username, password } = args;
|
||||
const {
|
||||
input: { username, password },
|
||||
} = args;
|
||||
const { config, auth, info } = context;
|
||||
|
||||
return (await usersRouter.handleLogIn({
|
||||
|
||||
@@ -5,6 +5,46 @@ import rest from '../../rest';
|
||||
import Auth from '../../Auth';
|
||||
import { extractKeysAndInclude } from './parseClassTypes';
|
||||
|
||||
const getUserFromSessionToken = async (config, info, queryInfo) => {
|
||||
if (!info || !info.sessionToken) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_SESSION_TOKEN,
|
||||
'Invalid session token'
|
||||
);
|
||||
}
|
||||
const sessionToken = info.sessionToken;
|
||||
const selectedFields = getFieldNames(queryInfo);
|
||||
|
||||
const { include } = extractKeysAndInclude(selectedFields);
|
||||
const response = await rest.find(
|
||||
config,
|
||||
Auth.master(config),
|
||||
'_Session',
|
||||
{ sessionToken },
|
||||
{
|
||||
include: include
|
||||
.split(',')
|
||||
.map(included => `user.${included}`)
|
||||
.join(','),
|
||||
},
|
||||
info.clientVersion
|
||||
);
|
||||
if (
|
||||
!response.results ||
|
||||
response.results.length == 0 ||
|
||||
!response.results[0].user
|
||||
) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_SESSION_TOKEN,
|
||||
'Invalid session token'
|
||||
);
|
||||
} else {
|
||||
const user = response.results[0].user;
|
||||
user.sessionToken = sessionToken;
|
||||
return user;
|
||||
}
|
||||
};
|
||||
|
||||
const load = parseGraphQLSchema => {
|
||||
if (parseGraphQLSchema.isUsersClassDisabled) {
|
||||
return;
|
||||
@@ -17,44 +57,7 @@ const load = parseGraphQLSchema => {
|
||||
async resolve(_source, _args, context, queryInfo) {
|
||||
try {
|
||||
const { config, info } = context;
|
||||
|
||||
if (!info || !info.sessionToken) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_SESSION_TOKEN,
|
||||
'Invalid session token'
|
||||
);
|
||||
}
|
||||
const sessionToken = info.sessionToken;
|
||||
const selectedFields = getFieldNames(queryInfo);
|
||||
|
||||
const { include } = extractKeysAndInclude(selectedFields);
|
||||
const response = await rest.find(
|
||||
config,
|
||||
Auth.master(config),
|
||||
'_Session',
|
||||
{ sessionToken },
|
||||
{
|
||||
include: include
|
||||
.split(',')
|
||||
.map(included => `user.${included}`)
|
||||
.join(','),
|
||||
},
|
||||
info.clientVersion
|
||||
);
|
||||
if (
|
||||
!response.results ||
|
||||
response.results.length == 0 ||
|
||||
!response.results[0].user
|
||||
) {
|
||||
throw new Parse.Error(
|
||||
Parse.Error.INVALID_SESSION_TOKEN,
|
||||
'Invalid session token'
|
||||
);
|
||||
} else {
|
||||
const user = response.results[0].user;
|
||||
user.sessionToken = sessionToken;
|
||||
return user;
|
||||
}
|
||||
return await getUserFromSessionToken(config, info, queryInfo);
|
||||
} catch (e) {
|
||||
parseGraphQLSchema.handleError(e);
|
||||
}
|
||||
@@ -75,4 +78,4 @@ const load = parseGraphQLSchema => {
|
||||
};
|
||||
};
|
||||
|
||||
export { load };
|
||||
export { load, getUserFromSessionToken };
|
||||
|
||||
Reference in New Issue
Block a user