Skip to content

Commit 9d689f1

Browse files
committed
Client mutation id on logIn mutation
1 parent 337140f commit 9d689f1

File tree

4 files changed

+113
-67
lines changed

4 files changed

+113
-67
lines changed

spec/ParseGraphQLServer.spec.js

Lines changed: 62 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,46 @@ describe('ParseGraphQLServer', () => {
903903

904904
expect(payloadFields).toEqual(['clientMutationId', 'viewer']);
905905
});
906+
907+
it('should have clientMutationId in log in mutation input', async () => {
908+
const inputFields = (await apolloClient.query({
909+
query: gql`
910+
query {
911+
__type(name: "LogInInput") {
912+
inputFields {
913+
name
914+
}
915+
}
916+
}
917+
`,
918+
})).data['__type'].inputFields
919+
.map(field => field.name)
920+
.sort();
921+
922+
expect(inputFields).toEqual([
923+
'clientMutationId',
924+
'password',
925+
'username',
926+
]);
927+
});
928+
929+
it('should have clientMutationId in log in mutation payload', async () => {
930+
const payloadFields = (await apolloClient.query({
931+
query: gql`
932+
query {
933+
__type(name: "LogInPayload") {
934+
fields {
935+
name
936+
}
937+
}
938+
}
939+
`,
940+
})).data['__type'].fields
941+
.map(field => field.name)
942+
.sort();
943+
944+
expect(payloadFields).toEqual(['clientMutationId', 'viewer']);
945+
});
906946
});
907947

908948
describe('Parse Class Types', () => {
@@ -5826,6 +5866,7 @@ describe('ParseGraphQLServer', () => {
58265866
});
58275867

58285868
it('should log the user in', async () => {
5869+
const clientMutationId = uuidv4();
58295870
const user = new Parse.User();
58305871
user.setUsername('user1');
58315872
user.setPassword('user1');
@@ -5835,24 +5876,29 @@ describe('ParseGraphQLServer', () => {
58355876
await parseGraphQLServer.parseGraphQLSchema.databaseController.schemaCache.clear();
58365877
const result = await apolloClient.mutate({
58375878
mutation: gql`
5838-
mutation LogInUser($fields: LogInFieldsInput) {
5839-
logIn(fields: $fields) {
5840-
sessionToken
5841-
someField
5879+
mutation LogInUser($input: LogInInput!) {
5880+
logIn(input: $input) {
5881+
clientMutationId
5882+
viewer {
5883+
sessionToken
5884+
someField
5885+
}
58425886
}
58435887
}
58445888
`,
58455889
variables: {
5846-
fields: {
5890+
input: {
5891+
clientMutationId,
58475892
username: 'user1',
58485893
password: 'user1',
58495894
},
58505895
},
58515896
});
58525897

5853-
expect(result.data.logIn.sessionToken).toBeDefined();
5854-
expect(result.data.logIn.someField).toEqual('someValue');
5855-
expect(typeof result.data.logIn.sessionToken).toBe('string');
5898+
expect(result.data.logIn.clientMutationId).toEqual(clientMutationId);
5899+
expect(result.data.logIn.viewer.sessionToken).toBeDefined();
5900+
expect(result.data.logIn.viewer.someField).toEqual('someValue');
5901+
expect(typeof result.data.logIn.viewer.sessionToken).toBe('string');
58565902
});
58575903

58585904
it('should log the user out', async () => {
@@ -5864,21 +5910,23 @@ describe('ParseGraphQLServer', () => {
58645910

58655911
const logIn = await apolloClient.mutate({
58665912
mutation: gql`
5867-
mutation LogInUser($fields: LogInFieldsInput) {
5868-
logIn(fields: $fields) {
5869-
sessionToken
5913+
mutation LogInUser($input: LogInInput!) {
5914+
logIn(input: $input) {
5915+
viewer {
5916+
sessionToken
5917+
}
58705918
}
58715919
}
58725920
`,
58735921
variables: {
5874-
fields: {
5922+
input: {
58755923
username: 'user1',
58765924
password: 'user1',
58775925
},
58785926
},
58795927
});
58805928

5881-
const sessionToken = logIn.data.logIn.sessionToken;
5929+
const sessionToken = logIn.data.logIn.viewer.sessionToken;
58825930

58835931
const logOut = await apolloClient.mutate({
58845932
mutation: gql`
@@ -5900,7 +5948,7 @@ describe('ParseGraphQLServer', () => {
59005948
await apolloClient.query({
59015949
query: gql`
59025950
query GetCurrentUser {
5903-
me {
5951+
viewer {
59045952
username
59055953
}
59065954
}

src/GraphQL/ParseGraphQLSchema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ const RESERVED_GRAPHQL_TYPE_NAMES = [
3838
'Viewer',
3939
'SignUpInput',
4040
'SignUpPayload',
41-
'LogInFieldsInput',
41+
'LogInInput',
42+
'LogInPayload',
4243
'CloudCodeFunction',
4344
'CallCloudCodeInput',
4445
'CallCloudCodePayload',

src/GraphQL/loaders/parseClassTypes.js

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -569,27 +569,6 @@ const load = (
569569
});
570570
parseGraphQLSchema.viewerType = viewerType;
571571
parseGraphQLSchema.addGraphQLType(viewerType, true, true);
572-
573-
const userLogInInputTypeName = 'LogInFieldsInput';
574-
const userLogInInputType = new GraphQLInputObjectType({
575-
name: userLogInInputTypeName,
576-
description: `The ${userLogInInputTypeName} input type is used to login.`,
577-
fields: {
578-
username: {
579-
description: 'This is the username used to log the user in.',
580-
type: new GraphQLNonNull(GraphQLString),
581-
},
582-
password: {
583-
description: 'This is the password used to log the user in.',
584-
type: new GraphQLNonNull(GraphQLString),
585-
},
586-
},
587-
});
588-
parseGraphQLSchema.addGraphQLType(userLogInInputType, true, true);
589-
590-
parseGraphQLSchema.parseClassTypes[
591-
className
592-
].logInInputType = userLogInInputType;
593572
}
594573
};
595574

src/GraphQL/loaders/usersMutations.js

Lines changed: 49 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GraphQLNonNull } from 'graphql';
1+
import { GraphQLNonNull, GraphQLString } from 'graphql';
22
import { mutationWithClientMutationId } from 'graphql-relay';
33
import UsersRouter from '../../Routers/UsersRouter';
44
import * as objectsMutations from '../helpers/objectsMutations';
@@ -62,42 +62,60 @@ const load = parseGraphQLSchema => {
6262
parseGraphQLSchema.addGraphQLType(signUpMutation.type, true, true);
6363
parseGraphQLSchema.addGraphQLMutation('signUp', signUpMutation, true, true);
6464

65-
parseGraphQLSchema.addGraphQLMutation(
66-
'logIn',
67-
{
68-
description: 'The logIn mutation can be used to log the user in.',
69-
args: {
70-
fields: {
71-
description: 'This is data needed to login',
72-
type: parseGraphQLSchema.parseClassTypes['_User'].logInInputType,
73-
},
65+
const logInMutation = mutationWithClientMutationId({
66+
name: 'LogIn',
67+
description: 'The logIn mutation can be used to log in an existing user.',
68+
inputFields: {
69+
username: {
70+
description: 'This is the username used to log in the user.',
71+
type: new GraphQLNonNull(GraphQLString),
7472
},
75-
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
76-
async resolve(_source, args, context) {
77-
try {
78-
const {
79-
fields: { username, password },
80-
} = args;
81-
const { config, auth, info } = context;
82-
83-
return (await usersRouter.handleLogIn({
84-
body: {
85-
username,
86-
password,
87-
},
88-
query: {},
89-
config,
90-
auth,
91-
info,
92-
})).response;
93-
} catch (e) {
94-
parseGraphQLSchema.handleError(e);
95-
}
73+
password: {
74+
description: 'This is the password used to log in the user.',
75+
type: new GraphQLNonNull(GraphQLString),
76+
},
77+
},
78+
outputFields: {
79+
viewer: {
80+
description:
81+
'This is the existing user that was logged in and returned as a viewer.',
82+
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
9683
},
9784
},
85+
mutateAndGetPayload: async (args, context, mutationInfo) => {
86+
try {
87+
const { username, password } = args;
88+
const { config, auth, info } = context;
89+
90+
const { sessionToken } = (await usersRouter.handleLogIn({
91+
body: {
92+
username,
93+
password,
94+
},
95+
query: {},
96+
config,
97+
auth,
98+
info,
99+
})).response;
100+
101+
info.sessionToken = sessionToken;
102+
103+
return {
104+
viewer: await getUserFromSessionToken(config, info, mutationInfo),
105+
};
106+
} catch (e) {
107+
parseGraphQLSchema.handleError(e);
108+
}
109+
},
110+
});
111+
112+
parseGraphQLSchema.addGraphQLType(
113+
logInMutation.args.input.type.ofType,
98114
true,
99115
true
100116
);
117+
parseGraphQLSchema.addGraphQLType(logInMutation.type, true, true);
118+
parseGraphQLSchema.addGraphQLMutation('logIn', logInMutation, true, true);
101119

102120
parseGraphQLSchema.addGraphQLMutation(
103121
'logOut',

0 commit comments

Comments
 (0)