2019-12-01 21:43:08 -08:00
|
|
|
import { GraphQLNonNull, GraphQLString } from 'graphql';
|
|
|
|
|
import { mutationWithClientMutationId } from 'graphql-relay';
|
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-12-01 21:43:08 -08:00
|
|
|
const signUpMutation = mutationWithClientMutationId({
|
|
|
|
|
name: 'SignUp',
|
|
|
|
|
description:
|
|
|
|
|
'The signUp mutation can be used to create and sign up a new user.',
|
|
|
|
|
inputFields: {
|
|
|
|
|
userFields: {
|
|
|
|
|
descriptions:
|
|
|
|
|
'These are the fields of the new user to be created and signed up.',
|
|
|
|
|
type:
|
|
|
|
|
parseGraphQLSchema.parseClassTypes['_User'].classGraphQLCreateType,
|
2019-06-19 17:19:47 -07:00
|
|
|
},
|
2019-12-01 21:43:08 -08:00
|
|
|
},
|
|
|
|
|
outputFields: {
|
|
|
|
|
viewer: {
|
|
|
|
|
description:
|
|
|
|
|
'This is the new user that was created, signed up and returned as a viewer.',
|
|
|
|
|
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
mutateAndGetPayload: async (args, context, mutationInfo) => {
|
|
|
|
|
try {
|
|
|
|
|
const { userFields } = args;
|
|
|
|
|
const { config, auth, info } = context;
|
2019-08-17 11:02:19 -07:00
|
|
|
|
2019-12-01 21:43:08 -08:00
|
|
|
const { sessionToken } = await objectsMutations.createObject(
|
|
|
|
|
'_User',
|
|
|
|
|
userFields,
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info
|
|
|
|
|
);
|
2019-08-17 11:02:19 -07:00
|
|
|
|
2019-12-01 21:43:08 -08:00
|
|
|
info.sessionToken = sessionToken;
|
2019-08-17 11:02:19 -07:00
|
|
|
|
2019-12-01 21:43:08 -08:00
|
|
|
return {
|
|
|
|
|
viewer: await getUserFromSessionToken(
|
|
|
|
|
config,
|
|
|
|
|
info,
|
|
|
|
|
mutationInfo,
|
|
|
|
|
'viewer.user.',
|
|
|
|
|
true
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
},
|
2019-12-01 21:43:08 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
parseGraphQLSchema.addGraphQLType(
|
|
|
|
|
signUpMutation.args.input.type.ofType,
|
2019-08-17 11:02:19 -07:00
|
|
|
true,
|
|
|
|
|
true
|
|
|
|
|
);
|
2019-12-01 21:43:08 -08:00
|
|
|
parseGraphQLSchema.addGraphQLType(signUpMutation.type, true, true);
|
|
|
|
|
parseGraphQLSchema.addGraphQLMutation('signUp', signUpMutation, true, true);
|
2019-08-17 11:02:19 -07:00
|
|
|
|
2019-12-01 21:43:08 -08:00
|
|
|
const logInMutation = mutationWithClientMutationId({
|
|
|
|
|
name: 'LogIn',
|
|
|
|
|
description: 'The logIn mutation can be used to log in an existing user.',
|
|
|
|
|
inputFields: {
|
|
|
|
|
username: {
|
|
|
|
|
description: 'This is the username used to log in the user.',
|
|
|
|
|
type: new GraphQLNonNull(GraphQLString),
|
|
|
|
|
},
|
|
|
|
|
password: {
|
|
|
|
|
description: 'This is the password used to log in the user.',
|
|
|
|
|
type: new GraphQLNonNull(GraphQLString),
|
2019-08-17 11:02:19 -07:00
|
|
|
},
|
2019-12-01 21:43:08 -08:00
|
|
|
},
|
|
|
|
|
outputFields: {
|
|
|
|
|
viewer: {
|
|
|
|
|
description:
|
|
|
|
|
'This is the existing user that was logged in and returned as a viewer.',
|
|
|
|
|
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
mutateAndGetPayload: async (args, context, mutationInfo) => {
|
|
|
|
|
try {
|
|
|
|
|
const { username, password } = args;
|
|
|
|
|
const { config, auth, info } = context;
|
|
|
|
|
|
|
|
|
|
const { sessionToken } = (await usersRouter.handleLogIn({
|
|
|
|
|
body: {
|
|
|
|
|
username,
|
|
|
|
|
password,
|
|
|
|
|
},
|
|
|
|
|
query: {},
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info,
|
|
|
|
|
})).response;
|
|
|
|
|
|
|
|
|
|
info.sessionToken = sessionToken;
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
viewer: await getUserFromSessionToken(
|
2019-08-17 11:02:19 -07:00
|
|
|
config,
|
|
|
|
|
info,
|
2019-12-01 21:43:08 -08:00
|
|
|
mutationInfo,
|
|
|
|
|
'viewer.user.',
|
|
|
|
|
true
|
|
|
|
|
),
|
|
|
|
|
};
|
|
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
},
|
2019-12-01 21:43:08 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
parseGraphQLSchema.addGraphQLType(
|
|
|
|
|
logInMutation.args.input.type.ofType,
|
2019-08-17 11:02:19 -07:00
|
|
|
true,
|
|
|
|
|
true
|
|
|
|
|
);
|
2019-12-01 21:43:08 -08:00
|
|
|
parseGraphQLSchema.addGraphQLType(logInMutation.type, true, true);
|
|
|
|
|
parseGraphQLSchema.addGraphQLMutation('logIn', logInMutation, true, true);
|
2019-08-17 11:02:19 -07:00
|
|
|
|
2019-12-01 21:43:08 -08:00
|
|
|
const logOutMutation = mutationWithClientMutationId({
|
|
|
|
|
name: 'LogOut',
|
|
|
|
|
description: 'The logOut mutation can be used to log out an existing user.',
|
|
|
|
|
outputFields: {
|
|
|
|
|
viewer: {
|
|
|
|
|
description:
|
|
|
|
|
'This is the existing user that was logged out and returned as a viewer.',
|
|
|
|
|
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
mutateAndGetPayload: async (_args, context, mutationInfo) => {
|
|
|
|
|
try {
|
|
|
|
|
const { config, auth, info } = context;
|
2019-08-17 11:02:19 -07:00
|
|
|
|
2019-12-01 21:43:08 -08:00
|
|
|
const viewer = await getUserFromSessionToken(
|
|
|
|
|
config,
|
|
|
|
|
info,
|
|
|
|
|
mutationInfo,
|
|
|
|
|
'viewer.user.',
|
|
|
|
|
true
|
|
|
|
|
);
|
2019-08-17 11:02:19 -07:00
|
|
|
|
2019-12-01 21:43:08 -08:00
|
|
|
await usersRouter.handleLogOut({
|
|
|
|
|
config,
|
|
|
|
|
auth,
|
|
|
|
|
info,
|
|
|
|
|
});
|
2019-08-17 11:02:19 -07:00
|
|
|
|
2019-12-01 21:43:08 -08:00
|
|
|
return { viewer };
|
|
|
|
|
} catch (e) {
|
|
|
|
|
parseGraphQLSchema.handleError(e);
|
|
|
|
|
}
|
2019-06-19 17:19:47 -07:00
|
|
|
},
|
2019-12-01 21:43:08 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
parseGraphQLSchema.addGraphQLType(
|
|
|
|
|
logOutMutation.args.input.type.ofType,
|
2019-08-17 11:02:19 -07:00
|
|
|
true,
|
|
|
|
|
true
|
|
|
|
|
);
|
2019-12-01 21:43:08 -08:00
|
|
|
parseGraphQLSchema.addGraphQLType(logOutMutation.type, true, true);
|
|
|
|
|
parseGraphQLSchema.addGraphQLMutation('logOut', logOutMutation, true, true);
|
2019-06-19 17:19:47 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export { load };
|