Skip to content

Commit 8fac91a

Browse files
committed
Client mutation id on deleteClass mutation
1 parent 21b474c commit 8fac91a

File tree

2 files changed

+112
-49
lines changed

2 files changed

+112
-49
lines changed

spec/ParseGraphQLServer.spec.js

Lines changed: 64 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,6 +1059,42 @@ describe('ParseGraphQLServer', () => {
10591059

10601060
expect(payloadFields).toEqual(['class', 'clientMutationId']);
10611061
});
1062+
1063+
it('should have clientMutationId in deleteClass mutation input', async () => {
1064+
const inputFields = (await apolloClient.query({
1065+
query: gql`
1066+
query {
1067+
__type(name: "DeleteClassInput") {
1068+
inputFields {
1069+
name
1070+
}
1071+
}
1072+
}
1073+
`,
1074+
})).data['__type'].inputFields
1075+
.map(field => field.name)
1076+
.sort();
1077+
1078+
expect(inputFields).toEqual(['clientMutationId', 'name']);
1079+
});
1080+
1081+
it('should have clientMutationId in deleteClass mutation payload', async () => {
1082+
const payloadFields = (await apolloClient.query({
1083+
query: gql`
1084+
query {
1085+
__type(name: "UpdateClassPayload") {
1086+
fields {
1087+
name
1088+
}
1089+
}
1090+
}
1091+
`,
1092+
})).data['__type'].fields
1093+
.map(field => field.name)
1094+
.sort();
1095+
1096+
expect(payloadFields).toEqual(['class', 'clientMutationId']);
1097+
});
10621098
});
10631099

10641100
describe('Parse Class Types', () => {
@@ -3481,6 +3517,7 @@ describe('ParseGraphQLServer', () => {
34813517

34823518
it('should delete an existing class', async () => {
34833519
try {
3520+
const clientMutationId = uuidv4();
34843521
const result = await apolloClient.mutate({
34853522
mutation: gql`
34863523
mutation {
@@ -3498,10 +3535,13 @@ describe('ParseGraphQLServer', () => {
34983535
}
34993536
}
35003537
}
3501-
deleteClass(name: "MyNewClass") {
3502-
name
3503-
schemaFields {
3538+
deleteClass(input: { clientMutationId: "${clientMutationId}" name: "MyNewClass" }) {
3539+
clientMutationId
3540+
class {
35043541
name
3542+
schemaFields {
3543+
name
3544+
}
35053545
}
35063546
}
35073547
}
@@ -3515,7 +3555,7 @@ describe('ParseGraphQLServer', () => {
35153555
result.data.createClass.class.schemaFields = result.data.createClass.class.schemaFields.sort(
35163556
(a, b) => (a.name > b.name ? 1 : -1)
35173557
);
3518-
result.data.deleteClass.schemaFields = result.data.deleteClass.schemaFields.sort(
3558+
result.data.deleteClass.class.schemaFields = result.data.deleteClass.class.schemaFields.sort(
35193559
(a, b) => (a.name > b.name ? 1 : -1)
35203560
);
35213561
expect(result).toEqual({
@@ -3538,15 +3578,22 @@ describe('ParseGraphQLServer', () => {
35383578
__typename: 'CreateClassPayload',
35393579
},
35403580
deleteClass: {
3541-
name: 'MyNewClass',
3542-
schemaFields: [
3543-
{ name: 'ACL', __typename: 'SchemaACLField' },
3544-
{ name: 'createdAt', __typename: 'SchemaDateField' },
3545-
{ name: 'objectId', __typename: 'SchemaStringField' },
3546-
{ name: 'updatedAt', __typename: 'SchemaDateField' },
3547-
{ name: 'willBeRemoved', __typename: 'SchemaStringField' },
3548-
],
3549-
__typename: 'Class',
3581+
clientMutationId,
3582+
class: {
3583+
name: 'MyNewClass',
3584+
schemaFields: [
3585+
{ name: 'ACL', __typename: 'SchemaACLField' },
3586+
{ name: 'createdAt', __typename: 'SchemaDateField' },
3587+
{ name: 'objectId', __typename: 'SchemaStringField' },
3588+
{ name: 'updatedAt', __typename: 'SchemaDateField' },
3589+
{
3590+
name: 'willBeRemoved',
3591+
__typename: 'SchemaStringField',
3592+
},
3593+
],
3594+
__typename: 'Class',
3595+
},
3596+
__typename: 'DeleteClassPayload',
35503597
},
35513598
},
35523599
});
@@ -3604,8 +3651,8 @@ describe('ParseGraphQLServer', () => {
36043651
await apolloClient.mutate({
36053652
mutation: gql`
36063653
mutation {
3607-
deleteClass(name: "SomeClass") {
3608-
name
3654+
deleteClass(input: { name: "SomeClass" }) {
3655+
clientMutationId
36093656
}
36103657
}
36113658
`,
@@ -3626,8 +3673,8 @@ describe('ParseGraphQLServer', () => {
36263673
await apolloClient.mutate({
36273674
mutation: gql`
36283675
mutation {
3629-
deleteClass(name: "SomeInexistentClass") {
3630-
name
3676+
deleteClass(input: { name: "SomeInexistentClass" }) {
3677+
clientMutationId
36313678
}
36323679
}
36333680
`,

src/GraphQL/loaders/schemaMutations.js

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ const load = parseGraphQLSchema => {
8484
},
8585
outputFields: {
8686
class: {
87-
description: 'This is the created class.',
87+
description: 'This is the updated class.',
8888
type: new GraphQLNonNull(schemaTypes.CLASS),
8989
},
9090
},
@@ -136,41 +136,57 @@ const load = parseGraphQLSchema => {
136136
true
137137
);
138138

139-
parseGraphQLSchema.addGraphQLMutation(
140-
'deleteClass',
141-
{
142-
description:
143-
'The deleteClass mutation can be used to delete an existing object class.',
144-
args: {
145-
name: schemaTypes.CLASS_NAME_ATT,
139+
const deleteClassMutation = mutationWithClientMutationId({
140+
name: 'DeleteClass',
141+
description:
142+
'The deleteClass mutation can be used to delete an existing object class.',
143+
inputFields: {
144+
name: schemaTypes.CLASS_NAME_ATT,
145+
},
146+
outputFields: {
147+
class: {
148+
description: 'This is the deleted class.',
149+
type: new GraphQLNonNull(schemaTypes.CLASS),
146150
},
147-
type: new GraphQLNonNull(schemaTypes.CLASS),
148-
resolve: async (_source, args, context) => {
149-
try {
150-
const { name } = args;
151-
const { config, auth } = context;
152-
153-
enforceMasterKeyAccess(auth);
154-
155-
if (auth.isReadOnly) {
156-
throw new Parse.Error(
157-
Parse.Error.OPERATION_FORBIDDEN,
158-
"read-only masterKey isn't allowed to delete a schema."
159-
);
160-
}
161-
162-
const schema = await config.database.loadSchema({ clearCache: true });
163-
const existingParseClass = await getClass(name, schema);
164-
await config.database.deleteSchema(name);
165-
return {
151+
},
152+
mutateAndGetPayload: async (args, context) => {
153+
try {
154+
const { name } = args;
155+
const { config, auth } = context;
156+
157+
enforceMasterKeyAccess(auth);
158+
159+
if (auth.isReadOnly) {
160+
throw new Parse.Error(
161+
Parse.Error.OPERATION_FORBIDDEN,
162+
"read-only masterKey isn't allowed to delete a schema."
163+
);
164+
}
165+
166+
const schema = await config.database.loadSchema({ clearCache: true });
167+
const existingParseClass = await getClass(name, schema);
168+
await config.database.deleteSchema(name);
169+
return {
170+
class: {
166171
name: existingParseClass.className,
167172
schemaFields: transformToGraphQL(existingParseClass.fields),
168-
};
169-
} catch (e) {
170-
parseGraphQLSchema.handleError(e);
171-
}
172-
},
173+
},
174+
};
175+
} catch (e) {
176+
parseGraphQLSchema.handleError(e);
177+
}
173178
},
179+
});
180+
181+
parseGraphQLSchema.addGraphQLType(
182+
deleteClassMutation.args.input.type.ofType,
183+
true,
184+
true
185+
);
186+
parseGraphQLSchema.addGraphQLType(deleteClassMutation.type, true, true);
187+
parseGraphQLSchema.addGraphQLMutation(
188+
'deleteClass',
189+
deleteClassMutation,
174190
true,
175191
true
176192
);

0 commit comments

Comments
 (0)