2019-08-17 11:02:19 -07:00
|
|
|
import { GraphQLNonNull } from 'graphql';
|
2019-06-19 17:19:47 -07:00
|
|
|
import UsersRouter from '../../Routers/UsersRouter';
|
2019-09-01 22:11:03 -07:00
|
|
|
import * as objectsMutations from '../helpers/objectsMutations';
|
2019-08-12 10:38:59 +02:00
|
|
|
import { getUserFromSessionToken } from './usersQueries';
|
2019-06-19 17:19:47 -07:00
|
|
|
|
|
|
|
|
const usersRouter = new UsersRouter();
|
|
|
|
|
|
|
|
|
|
const load = parseGraphQLSchema => {
|
2019-07-25 20:46:25 +01:00
|
|
|
if (parseGraphQLSchema.isUsersClassDisabled) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
|
2019-08-17 11:02:19 -07:00
|
|
|
parseGraphQLSchema.addGraphQLMutation(
|
|
|
|
|
'signUp',
|
|
|
|
|
{
|
|
|
|
|
description: 'The signUp mutation can be used to sign the user up.',
|
|
|
|
|
args: {
|
|
|
|
|
fields: {
|
|
|
|
|
descriptions: 'These are the fields of the user.',
|
|
|
|
|
type: parseGraphQLSchema.parseClassTypes['_User'].signUpInputType,
|
|
|
|
|
},
|
2019-06-19 17:19:47 -07:00
|
|
|
},
|
2019-08-17 11:02:19 -07:00
|
|
|
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
|
|
|
|
|
async resolve(_source, args, context, mutationInfo) {
|
|
|
|
|
try {
|
|
|
|
|
const { fields } = args;
|
|
|
|
|
|
|
|
|
|
const { config, auth, info } = context;
|
|
|
|
|
|
|
|
|
|
const { sessionToken } = await objectsMutations.createObject(
|
|
|
|
|
'_User',
|
|
|
|
|
fields,
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
info.sessionToken = sessionToken;
|
|
|
|
|
|
|
|
|
|
return await getUserFromSessionToken(config, info, mutationInfo);
|
|
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
},
|
|
|
|
|
},
|
2019-08-17 11:02:19 -07:00
|
|
|
true,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
parseGraphQLSchema.addGraphQLMutation(
|
|
|
|
|
'logIn',
|
|
|
|
|
{
|
|
|
|
|
description: 'The logIn mutation can be used to log the user in.',
|
|
|
|
|
args: {
|
|
|
|
|
fields: {
|
|
|
|
|
description: 'This is data needed to login',
|
|
|
|
|
type: parseGraphQLSchema.parseClassTypes['_User'].logInInputType,
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
|
|
|
|
|
async resolve(_source, args, context) {
|
|
|
|
|
try {
|
|
|
|
|
const {
|
|
|
|
|
fields: { username, password },
|
|
|
|
|
} = args;
|
|
|
|
|
const { config, auth, info } = context;
|
|
|
|
|
|
|
|
|
|
return (await usersRouter.handleLogIn({
|
|
|
|
|
body: {
|
|
|
|
|
username,
|
|
|
|
|
password,
|
|
|
|
|
},
|
|
|
|
|
query: {},
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info,
|
|
|
|
|
})).response;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-06-19 17:19:47 -07:00
|
|
|
},
|
2019-08-17 11:02:19 -07:00
|
|
|
true,
|
|
|
|
|
true
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
parseGraphQLSchema.addGraphQLMutation(
|
|
|
|
|
'logOut',
|
|
|
|
|
{
|
|
|
|
|
description: 'The logOut mutation can be used to log the user out.',
|
|
|
|
|
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
|
|
|
|
|
async resolve(_source, _args, context, mutationInfo) {
|
|
|
|
|
try {
|
|
|
|
|
const { config, auth, info } = context;
|
|
|
|
|
|
|
|
|
|
const viewer = await getUserFromSessionToken(
|
|
|
|
|
config,
|
|
|
|
|
info,
|
|
|
|
|
mutationInfo
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
await usersRouter.handleLogOut({
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
return viewer;
|
|
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
|
|
|
|
},
|
2019-06-19 17:19:47 -07:00
|
|
|
},
|
2019-08-17 11:02:19 -07:00
|
|
|
true,
|
|
|
|
|
true
|
|
|
|
|
);
|
2019-06-19 17:19:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { load };
|