Skip to content

Commit b47d9fb

Browse files
douglasmuraokadavimacedo
authored andcommitted
refactor(GraphQL): Rename objectId to id (#5985)
* refactor(GraphQL): Rename objectId to id Renames `objectId` to `id` for the GraphQL API. Queries, mutations, custom and generic types were updated. Removes `RELATION_INPUT` and `POINTER_INPUT`. Now the user just need to provide the ID of the object to link. * fix: Column "id" not found on Postgres * fix: Avoid deleting Parse class objectId * fix: Undo objectId removal on mutations * fix: Handle generic mutation id
1 parent 194f548 commit b47d9fb

File tree

10 files changed

+496
-546
lines changed

10 files changed

+496
-546
lines changed

spec/ParseGraphQLServer.spec.js

Lines changed: 417 additions & 464 deletions
Large diffs are not rendered by default.

src/GraphQL/loaders/defaultGraphQLTypes.js

Lines changed: 6 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -395,16 +395,7 @@ const POLYGON_INPUT = new GraphQLList(new GraphQLNonNull(GEO_POINT_INPUT));
395395

396396
const POLYGON = new GraphQLList(new GraphQLNonNull(GEO_POINT));
397397

398-
const RELATION_INPUT = new GraphQLInputObjectType({
399-
name: 'RelationInput',
400-
description: 'Object involved into a relation',
401-
fields: {
402-
objectId: {
403-
description: 'Id of the object involved.',
404-
type: new GraphQLNonNull(GraphQLID),
405-
},
406-
},
407-
});
398+
const OBJECT_ID = new GraphQLNonNull(GraphQLID);
408399

409400
const CLASS_NAME_ATT = {
410401
description: 'This is the class name of the object.',
@@ -418,7 +409,8 @@ const FIELDS_ATT = {
418409

419410
const OBJECT_ID_ATT = {
420411
description: 'This is the object id.',
421-
type: new GraphQLNonNull(GraphQLID),
412+
type: OBJECT_ID,
413+
resolve: ({ objectId }) => objectId,
422414
};
423415

424416
const CREATED_AT_ATT = {
@@ -441,7 +433,7 @@ const INPUT_FIELDS = {
441433
};
442434

443435
const CREATE_RESULT_FIELDS = {
444-
objectId: OBJECT_ID_ATT,
436+
id: OBJECT_ID_ATT,
445437
createdAt: CREATED_AT_ATT,
446438
};
447439

@@ -491,17 +483,6 @@ const INCLUDE_ATT = {
491483
type: GraphQLString,
492484
};
493485

494-
const POINTER_INPUT = new GraphQLInputObjectType({
495-
name: 'PointerInput',
496-
description: 'Allow to link an object to another object',
497-
fields: {
498-
objectId: {
499-
description: 'Id of the object involved.',
500-
type: new GraphQLNonNull(GraphQLID),
501-
},
502-
},
503-
});
504-
505486
const READ_PREFERENCE = new GraphQLEnumType({
506487
name: 'ReadPreference',
507488
description:
@@ -1118,8 +1099,7 @@ const load = parseGraphQLSchema => {
11181099
parseGraphQLSchema.addGraphQLType(FIND_RESULT, true);
11191100
parseGraphQLSchema.addGraphQLType(SIGN_UP_RESULT, true);
11201101
parseGraphQLSchema.addGraphQLType(ELEMENT, true);
1121-
parseGraphQLSchema.addGraphQLType(RELATION_INPUT, true);
1122-
parseGraphQLSchema.addGraphQLType(POINTER_INPUT, true);
1102+
parseGraphQLSchema.addGraphQLType(OBJECT_ID, true);
11231103
};
11241104

11251105
export {
@@ -1145,6 +1125,7 @@ export {
11451125
GEO_POINT,
11461126
POLYGON_INPUT,
11471127
POLYGON,
1128+
OBJECT_ID,
11481129
CLASS_NAME_ATT,
11491130
FIELDS_ATT,
11501131
OBJECT_ID_ATT,
@@ -1206,8 +1187,6 @@ export {
12061187
SIGN_UP_RESULT,
12071188
ARRAY_RESULT,
12081189
ELEMENT,
1209-
POINTER_INPUT,
1210-
RELATION_INPUT,
12111190
load,
12121191
loadArrayResult,
12131192
};

src/GraphQL/loaders/objectsMutations.js

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,15 @@ const load = parseGraphQLSchema => {
5454
const { className, fields } = args;
5555
const { config, auth, info } = context;
5656

57-
return await createObject(className, fields, config, auth, info);
57+
const object = await createObject(
58+
className,
59+
fields,
60+
config,
61+
auth,
62+
info
63+
);
64+
65+
return object;
5866
} catch (e) {
5967
parseGraphQLSchema.handleError(e);
6068
}
@@ -71,23 +79,16 @@ const load = parseGraphQLSchema => {
7179
'The update mutation can be used to update an object of a certain class.',
7280
args: {
7381
className: defaultGraphQLTypes.CLASS_NAME_ATT,
74-
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
82+
id: defaultGraphQLTypes.OBJECT_ID_ATT,
7583
fields: defaultGraphQLTypes.FIELDS_ATT,
7684
},
7785
type: new GraphQLNonNull(defaultGraphQLTypes.UPDATE_RESULT),
7886
async resolve(_source, args, context) {
7987
try {
80-
const { className, objectId, fields } = args;
88+
const { className, id, fields } = args;
8189
const { config, auth, info } = context;
8290

83-
return await updateObject(
84-
className,
85-
objectId,
86-
fields,
87-
config,
88-
auth,
89-
info
90-
);
91+
return await updateObject(className, id, fields, config, auth, info);
9192
} catch (e) {
9293
parseGraphQLSchema.handleError(e);
9394
}
@@ -104,15 +105,15 @@ const load = parseGraphQLSchema => {
104105
'The delete mutation can be used to delete an object of a certain class.',
105106
args: {
106107
className: defaultGraphQLTypes.CLASS_NAME_ATT,
107-
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
108+
id: defaultGraphQLTypes.OBJECT_ID_ATT,
108109
},
109110
type: new GraphQLNonNull(GraphQLBoolean),
110111
async resolve(_source, args, context) {
111112
try {
112-
const { className, objectId } = args;
113+
const { className, id } = args;
113114
const { config, auth, info } = context;
114115

115-
return await deleteObject(className, objectId, config, auth, info);
116+
return await deleteObject(className, id, config, auth, info);
116117
} catch (e) {
117118
parseGraphQLSchema.handleError(e);
118119
}

src/GraphQL/loaders/objectsQueries.js

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,11 @@ const getObject = async (
4343
throw new Parse.Error(Parse.Error.OBJECT_NOT_FOUND, 'Object not found.');
4444
}
4545

46+
const object = response.results[0];
4647
if (className === '_User') {
47-
delete response.results[0].sessionToken;
48+
delete object.sessionToken;
4849
}
49-
50-
return response.results[0];
50+
return object;
5151
};
5252

5353
const findObjects = async (
@@ -70,7 +70,6 @@ const findObjects = async (
7070
if (!where) {
7171
where = {};
7272
}
73-
7473
transformQueryInputToParse(where);
7574

7675
const options = {};
@@ -136,7 +135,7 @@ const load = parseGraphQLSchema => {
136135
'The get query can be used to get an object of a certain class by its objectId.',
137136
args: {
138137
className: defaultGraphQLTypes.CLASS_NAME_ATT,
139-
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
138+
id: defaultGraphQLTypes.OBJECT_ID_ATT,
140139
keys: defaultGraphQLTypes.KEYS_ATT,
141140
include: defaultGraphQLTypes.INCLUDE_ATT,
142141
readPreference: defaultGraphQLTypes.READ_PREFERENCE_ATT,
@@ -147,7 +146,7 @@ const load = parseGraphQLSchema => {
147146
try {
148147
const {
149148
className,
150-
objectId,
149+
id,
151150
keys,
152151
include,
153152
readPreference,
@@ -156,9 +155,9 @@ const load = parseGraphQLSchema => {
156155

157156
const { config, auth, info } = context;
158157

159-
return await getObject(
158+
const object = await getObject(
160159
className,
161-
objectId,
160+
id,
162161
keys,
163162
include,
164163
readPreference,
@@ -167,6 +166,10 @@ const load = parseGraphQLSchema => {
167166
auth,
168167
info
169168
);
169+
object.id = object.objectId;
170+
delete object.objectId;
171+
172+
return object;
170173
} catch (e) {
171174
parseGraphQLSchema.handleError(e);
172175
}
@@ -222,7 +225,7 @@ const load = parseGraphQLSchema => {
222225
const { config, auth, info } = context;
223226
const selectedFields = getFieldNames(queryInfo);
224227

225-
return await findObjects(
228+
const objects = await findObjects(
226229
className,
227230
where,
228231
order,
@@ -239,6 +242,11 @@ const load = parseGraphQLSchema => {
239242
info,
240243
selectedFields
241244
);
245+
objects.results.forEach(obj => {
246+
obj.id = obj.objectId;
247+
delete obj.objectId;
248+
});
249+
return objects;
242250
} catch (e) {
243251
parseGraphQLSchema.handleError(e);
244252
}

src/GraphQL/loaders/parseClassMutations.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ const load = function(
9191
fields,
9292
keys,
9393
include,
94-
['objectId', 'createdAt', 'updatedAt']
94+
['id', 'createdAt', 'updatedAt']
9595
);
9696
let optimizedObject = {};
9797
if (needGet) {
@@ -125,7 +125,7 @@ const load = function(
125125
parseGraphQLSchema.addGraphQLMutation(updateGraphQLMutationName, {
126126
description: `The ${updateGraphQLMutationName} mutation can be used to update an object of the ${graphQLClassName} class.`,
127127
args: {
128-
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
128+
id: defaultGraphQLTypes.OBJECT_ID_ATT,
129129
fields: {
130130
description: 'These are the fields used to update the object.',
131131
type: classGraphQLUpdateType || defaultGraphQLTypes.OBJECT,
@@ -136,7 +136,7 @@ const load = function(
136136
),
137137
async resolve(_source, args, context, mutationInfo) {
138138
try {
139-
const { objectId, fields } = args;
139+
const { id, fields } = args;
140140
const { config, auth, info } = context;
141141

142142
const parseFields = await transformTypes('update', fields, {
@@ -147,7 +147,7 @@ const load = function(
147147

148148
const updatedObject = await objectsMutations.updateObject(
149149
className,
150-
objectId,
150+
id,
151151
parseFields,
152152
config,
153153
auth,
@@ -160,13 +160,13 @@ const load = function(
160160
fields,
161161
keys,
162162
include,
163-
['objectId', 'updatedAt']
163+
['id', 'updatedAt']
164164
);
165165
let optimizedObject = {};
166166
if (needGet) {
167167
optimizedObject = await objectsQueries.getObject(
168168
className,
169-
objectId,
169+
id,
170170
requiredKeys,
171171
include,
172172
undefined,
@@ -177,7 +177,7 @@ const load = function(
177177
);
178178
}
179179
return {
180-
objectId: objectId,
180+
id,
181181
...updatedObject,
182182
...fields,
183183
...optimizedObject,
@@ -194,24 +194,24 @@ const load = function(
194194
parseGraphQLSchema.addGraphQLMutation(deleteGraphQLMutationName, {
195195
description: `The ${deleteGraphQLMutationName} mutation can be used to delete an object of the ${graphQLClassName} class.`,
196196
args: {
197-
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
197+
id: defaultGraphQLTypes.OBJECT_ID_ATT,
198198
},
199199
type: new GraphQLNonNull(
200200
classGraphQLOutputType || defaultGraphQLTypes.OBJECT
201201
),
202202
async resolve(_source, args, context, mutationInfo) {
203203
try {
204-
const { objectId } = args;
204+
const { id } = args;
205205
const { config, auth, info } = context;
206206
const selectedFields = getFieldNames(mutationInfo);
207207
const { keys, include } = extractKeysAndInclude(selectedFields);
208208

209209
let optimizedObject = {};
210210
const splitedKeys = keys.split(',');
211-
if (splitedKeys.length > 1 || splitedKeys[0] !== 'objectId') {
211+
if (splitedKeys.length > 1 || splitedKeys[0] !== 'id') {
212212
optimizedObject = await objectsQueries.getObject(
213213
className,
214-
objectId,
214+
id,
215215
keys,
216216
include,
217217
undefined,
@@ -223,12 +223,12 @@ const load = function(
223223
}
224224
await objectsMutations.deleteObject(
225225
className,
226-
objectId,
226+
id,
227227
config,
228228
auth,
229229
info
230230
);
231-
return { objectId: objectId, ...optimizedObject };
231+
return { id, ...optimizedObject };
232232
} catch (e) {
233233
parseGraphQLSchema.handleError(e);
234234
}

src/GraphQL/loaders/parseClassQueries.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,15 @@ const getParseClassQueryConfig = function(
1414
};
1515

1616
const getQuery = async (className, _source, args, context, queryInfo) => {
17-
const { objectId, readPreference, includeReadPreference } = args;
17+
const { id, readPreference, includeReadPreference } = args;
1818
const { config, auth, info } = context;
1919
const selectedFields = getFieldNames(queryInfo);
2020

2121
const { keys, include } = extractKeysAndInclude(selectedFields);
2222

2323
return await objectsQueries.getObject(
2424
className,
25-
objectId,
25+
id,
2626
keys,
2727
include,
2828
readPreference,
@@ -57,7 +57,7 @@ const load = function(
5757
parseGraphQLSchema.addGraphQLQuery(getGraphQLQueryName, {
5858
description: `The ${getGraphQLQueryName} query can be used to get an object of the ${graphQLClassName} class by its id.`,
5959
args: {
60-
objectId: defaultGraphQLTypes.OBJECT_ID_ATT,
60+
id: defaultGraphQLTypes.OBJECT_ID_ATT,
6161
readPreference: defaultGraphQLTypes.READ_PREFERENCE_ATT,
6262
includeReadPreference: defaultGraphQLTypes.INCLUDE_READ_PREFERENCE_ATT,
6363
},

0 commit comments

Comments
 (0)