Skip to content

Commit 337140f

Browse files
committed
Client mutation id on signUp mutation
1 parent 58b9dd9 commit 337140f

File tree

4 files changed

+106
-74
lines changed

4 files changed

+106
-74
lines changed

spec/ParseGraphQLServer.spec.js

Lines changed: 56 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -867,6 +867,42 @@ describe('ParseGraphQLServer', () => {
867867
'result',
868868
]);
869869
});
870+
871+
it('should have clientMutationId in sign up mutation input', async () => {
872+
const inputFields = (await apolloClient.query({
873+
query: gql`
874+
query {
875+
__type(name: "SignUpInput") {
876+
inputFields {
877+
name
878+
}
879+
}
880+
}
881+
`,
882+
})).data['__type'].inputFields
883+
.map(field => field.name)
884+
.sort();
885+
886+
expect(inputFields).toEqual(['clientMutationId', 'userFields']);
887+
});
888+
889+
it('should have clientMutationId in sign up mutation payload', async () => {
890+
const payloadFields = (await apolloClient.query({
891+
query: gql`
892+
query {
893+
__type(name: "SignUpPayload") {
894+
fields {
895+
name
896+
}
897+
}
898+
}
899+
`,
900+
})).data['__type'].fields
901+
.map(field => field.name)
902+
.sort();
903+
904+
expect(payloadFields).toEqual(['clientMutationId', 'viewer']);
905+
});
870906
});
871907

872908
describe('Parse Class Types', () => {
@@ -894,7 +930,6 @@ describe('ParseGraphQLServer', () => {
894930
'User',
895931
'UserWhereInput',
896932
'UserFindResult',
897-
'SignUpFieldsInput',
898933
'CreateUserFieldsInput',
899934
'UpdateUserFieldsInput',
900935
];
@@ -5755,31 +5790,39 @@ describe('ParseGraphQLServer', () => {
57555790

57565791
describe('Users Mutations', () => {
57575792
it('should sign user up', async () => {
5793+
const clientMutationId = uuidv4();
57585794
const userSchema = new Parse.Schema('_User');
57595795
userSchema.addString('someField');
57605796
await userSchema.update();
57615797
await parseGraphQLServer.parseGraphQLSchema.databaseController.schemaCache.clear();
57625798
const result = await apolloClient.mutate({
57635799
mutation: gql`
5764-
mutation SignUp($fields: SignUpFieldsInput) {
5765-
signUp(fields: $fields) {
5766-
sessionToken
5767-
someField
5800+
mutation SignUp($input: SignUpInput!) {
5801+
signUp(input: $input) {
5802+
clientMutationId
5803+
viewer {
5804+
sessionToken
5805+
someField
5806+
}
57685807
}
57695808
}
57705809
`,
57715810
variables: {
5772-
fields: {
5773-
username: 'user1',
5774-
password: 'user1',
5775-
someField: 'someValue',
5811+
input: {
5812+
clientMutationId,
5813+
userFields: {
5814+
username: 'user1',
5815+
password: 'user1',
5816+
someField: 'someValue',
5817+
},
57765818
},
57775819
},
57785820
});
57795821

5780-
expect(result.data.signUp.sessionToken).toBeDefined();
5781-
expect(result.data.signUp.someField).toEqual('someValue');
5782-
expect(typeof result.data.signUp.sessionToken).toBe('string');
5822+
expect(result.data.signUp.clientMutationId).toEqual(clientMutationId);
5823+
expect(result.data.signUp.viewer.sessionToken).toBeDefined();
5824+
expect(result.data.signUp.viewer.someField).toEqual('someValue');
5825+
expect(typeof result.data.signUp.viewer.sessionToken).toBe('string');
57835826
});
57845827

57855828
it('should log the user in', async () => {
@@ -8347,6 +8390,7 @@ describe('ParseGraphQLServer', () => {
83478390
variables: {
83488391
user: {
83498392
username: 'somefolk',
8393+
password: 'somepassword',
83508394
},
83518395
},
83528396
});

src/GraphQL/ParseGraphQLSchema.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ const RESERVED_GRAPHQL_TYPE_NAMES = [
3636
'CreateFileInput',
3737
'CreateFilePayload',
3838
'Viewer',
39-
'SignUpFieldsInput',
39+
'SignUpInput',
40+
'SignUpPayload',
4041
'LogInFieldsInput',
4142
'CloudCodeFunction',
4243
'CallCloudCodeInput',

src/GraphQL/loaders/parseClassTypes.js

Lines changed: 5 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,11 @@ const load = (
153153
...fields,
154154
[field]: {
155155
description: `This is the object ${field}.`,
156-
type,
156+
type:
157+
className === '_User' &&
158+
(field === 'username' || field === 'password')
159+
? new GraphQLNonNull(type)
160+
: type,
157161
},
158162
};
159163
} else {
@@ -566,35 +570,6 @@ const load = (
566570
parseGraphQLSchema.viewerType = viewerType;
567571
parseGraphQLSchema.addGraphQLType(viewerType, true, true);
568572

569-
const userSignUpInputTypeName = 'SignUpFieldsInput';
570-
const userSignUpInputType = new GraphQLInputObjectType({
571-
name: userSignUpInputTypeName,
572-
description: `The ${userSignUpInputTypeName} input type is used in operations that involve inputting objects of ${graphQLClassName} class when signing up.`,
573-
fields: () =>
574-
classCreateFields.reduce((fields, field) => {
575-
const type = transformInputTypeToGraphQL(
576-
parseClass.fields[field].type,
577-
parseClass.fields[field].targetClass,
578-
parseGraphQLSchema.parseClassTypes
579-
);
580-
if (type) {
581-
return {
582-
...fields,
583-
[field]: {
584-
description: `This is the object ${field}.`,
585-
type:
586-
field === 'username' || field === 'password'
587-
? new GraphQLNonNull(type)
588-
: type,
589-
},
590-
};
591-
} else {
592-
return fields;
593-
}
594-
}, {}),
595-
});
596-
parseGraphQLSchema.addGraphQLType(userSignUpInputType, true, true);
597-
598573
const userLogInInputTypeName = 'LogInFieldsInput';
599574
const userLogInInputType = new GraphQLInputObjectType({
600575
name: userLogInInputTypeName,
@@ -612,9 +587,6 @@ const load = (
612587
});
613588
parseGraphQLSchema.addGraphQLType(userLogInInputType, true, true);
614589

615-
parseGraphQLSchema.parseClassTypes[
616-
className
617-
].signUpInputType = userSignUpInputType;
618590
parseGraphQLSchema.parseClassTypes[
619591
className
620592
].logInInputType = userLogInInputType;

src/GraphQL/loaders/usersMutations.js

Lines changed: 43 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { GraphQLNonNull } from 'graphql';
2+
import { mutationWithClientMutationId } from 'graphql-relay';
23
import UsersRouter from '../../Routers/UsersRouter';
34
import * as objectsMutations from '../helpers/objectsMutations';
45
import { getUserFromSessionToken } from './usersQueries';
@@ -10,42 +11,56 @@ const load = parseGraphQLSchema => {
1011
return;
1112
}
1213

13-
parseGraphQLSchema.addGraphQLMutation(
14-
'signUp',
15-
{
16-
description: 'The signUp mutation can be used to sign the user up.',
17-
args: {
18-
fields: {
19-
descriptions: 'These are the fields of the user.',
20-
type: parseGraphQLSchema.parseClassTypes['_User'].signUpInputType,
21-
},
14+
const signUpMutation = mutationWithClientMutationId({
15+
name: 'SignUp',
16+
description:
17+
'The signUp mutation can be used to create and sign up a new user.',
18+
inputFields: {
19+
userFields: {
20+
descriptions:
21+
'These are the fields of the new user to be created and signed up.',
22+
type:
23+
parseGraphQLSchema.parseClassTypes['_User'].classGraphQLCreateType,
2224
},
23-
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
24-
async resolve(_source, args, context, mutationInfo) {
25-
try {
26-
const { fields } = args;
27-
28-
const { config, auth, info } = context;
25+
},
26+
outputFields: {
27+
viewer: {
28+
description:
29+
'This is the new user that was created, signed up and returned as a viewer.',
30+
type: new GraphQLNonNull(parseGraphQLSchema.viewerType),
31+
},
32+
},
33+
mutateAndGetPayload: async (args, context, mutationInfo) => {
34+
try {
35+
const { userFields } = args;
36+
const { config, auth, info } = context;
2937

30-
const { sessionToken } = await objectsMutations.createObject(
31-
'_User',
32-
fields,
33-
config,
34-
auth,
35-
info
36-
);
38+
const { sessionToken } = await objectsMutations.createObject(
39+
'_User',
40+
userFields,
41+
config,
42+
auth,
43+
info
44+
);
3745

38-
info.sessionToken = sessionToken;
46+
info.sessionToken = sessionToken;
3947

40-
return await getUserFromSessionToken(config, info, mutationInfo);
41-
} catch (e) {
42-
parseGraphQLSchema.handleError(e);
43-
}
44-
},
48+
return {
49+
viewer: await getUserFromSessionToken(config, info, mutationInfo),
50+
};
51+
} catch (e) {
52+
parseGraphQLSchema.handleError(e);
53+
}
4554
},
55+
});
56+
57+
parseGraphQLSchema.addGraphQLType(
58+
signUpMutation.args.input.type.ofType,
4659
true,
4760
true
4861
);
62+
parseGraphQLSchema.addGraphQLType(signUpMutation.type, true, true);
63+
parseGraphQLSchema.addGraphQLMutation('signUp', signUpMutation, true, true);
4964

5065
parseGraphQLSchema.addGraphQLMutation(
5166
'logIn',

0 commit comments

Comments
 (0)