Skip to content

Commit d75b3ec

Browse files
committed
Add support to global id in other operations
1 parent d2e30e8 commit d75b3ec

File tree

4 files changed

+57
-24
lines changed

4 files changed

+57
-24
lines changed

src/GraphQL/loaders/parseClassMutations.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const load = function(
9292
fields,
9393
keys,
9494
include,
95-
['id', 'createdAt', 'updatedAt']
95+
['id', 'objectId', 'createdAt', 'updatedAt']
9696
);
9797
let optimizedObject = {};
9898
if (needGet) {
@@ -168,7 +168,7 @@ const load = function(
168168
fields,
169169
keys,
170170
include,
171-
['id', 'updatedAt']
171+
['id', 'objectId', 'updatedAt']
172172
);
173173
let optimizedObject = {};
174174
if (needGet) {
@@ -185,7 +185,7 @@ const load = function(
185185
);
186186
}
187187
return {
188-
id,
188+
objectId: id,
189189
...updatedObject,
190190
...fields,
191191
...optimizedObject,
@@ -222,7 +222,10 @@ const load = function(
222222

223223
let optimizedObject = {};
224224
const splitedKeys = keys.split(',');
225-
if (splitedKeys.length > 1 || splitedKeys[0] !== 'id') {
225+
if (
226+
splitedKeys.filter(key => !['id', 'objectId'].includes(key))
227+
.length > 0
228+
) {
226229
optimizedObject = await objectsQueries.getObject(
227230
className,
228231
id,
@@ -242,7 +245,7 @@ const load = function(
242245
auth,
243246
info
244247
);
245-
return { id, ...optimizedObject };
248+
return { objectId: id, ...optimizedObject };
246249
} catch (e) {
247250
parseGraphQLSchema.handleError(e);
248251
}

src/GraphQL/loaders/parseClassTypes.js

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ const load = (
209209
fields: () => {
210210
const fields = {
211211
link: {
212-
description: `Link an existing object from ${graphQLClassName} class.`,
212+
description: `Link an existing object from ${graphQLClassName} class. You can use either the global or the object id.`,
213213
type: GraphQLID,
214214
},
215215
};
@@ -233,17 +233,17 @@ const load = (
233233
fields: () => {
234234
const fields = {
235235
add: {
236-
description: `Add an existing object from the ${graphQLClassName} class into the relation.`,
236+
description: `Add existing objects from the ${graphQLClassName} class into the relation. You can use either the global or the object ids.`,
237237
type: new GraphQLList(defaultGraphQLTypes.OBJECT_ID),
238238
},
239239
remove: {
240-
description: `Remove an existing object from the ${graphQLClassName} class out of the relation.`,
240+
description: `Remove existing objects from the ${graphQLClassName} class out of the relation. You can use either the global or the object ids.`,
241241
type: new GraphQLList(defaultGraphQLTypes.OBJECT_ID),
242242
},
243243
};
244244
if (isCreateEnabled) {
245245
fields['createAndAdd'] = {
246-
description: `Create and add an object of the ${graphQLClassName} class into the relation.`,
246+
description: `Create and add objects of the ${graphQLClassName} class into the relation.`,
247247
type: new GraphQLList(new GraphQLNonNull(classGraphQLCreateType)),
248248
};
249249
}
@@ -268,12 +268,12 @@ const load = (
268268
notInQueryKey: defaultGraphQLTypes.notInQueryKey,
269269
inQuery: {
270270
description:
271-
'This is the inQuery operator to specify a constraint to select the objects where a field equals to any of the ids in the result of a different query.',
271+
'This is the inQuery operator to specify a constraint to select the objects where a field equals to any of the object ids in the result of a different query.',
272272
type: defaultGraphQLTypes.SUBQUERY_INPUT,
273273
},
274274
notInQuery: {
275275
description:
276-
'This is the notInQuery operator to specify a constraint to select the objects where a field do not equal to any of the ids in the result of a different query.',
276+
'This is the notInQuery operator to specify a constraint to select the objects where a field do not equal to any of the object ids in the result of a different query.',
277277
type: defaultGraphQLTypes.SUBQUERY_INPUT,
278278
},
279279
},

src/GraphQL/transformers/mutation.js

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { fromGlobalId } from 'graphql-relay';
12
import * as defaultGraphQLTypes from '../loaders/defaultGraphQLTypes';
23
import * as objectsMutations from '../helpers/objectsMutations';
34

@@ -116,11 +117,17 @@ const transformers = {
116117

117118
if (value.add || nestedObjectsToAdd.length > 0) {
118119
if (!value.add) value.add = [];
119-
value.add = value.add.map(input => ({
120-
__type: 'Pointer',
121-
className: targetClass,
122-
objectId: input,
123-
}));
120+
value.add = value.add.map(input => {
121+
const globalIdObject = fromGlobalId(input);
122+
if (globalIdObject.type === targetClass) {
123+
input = globalIdObject.id;
124+
}
125+
return {
126+
__type: 'Pointer',
127+
className: targetClass,
128+
objectId: input,
129+
};
130+
});
124131
op.ops.push({
125132
__op: 'AddRelation',
126133
objects: [...value.add, ...nestedObjectsToAdd],
@@ -130,11 +137,17 @@ const transformers = {
130137
if (value.remove) {
131138
op.ops.push({
132139
__op: 'RemoveRelation',
133-
objects: value.remove.map(input => ({
134-
__type: 'Pointer',
135-
className: targetClass,
136-
objectId: input,
137-
})),
140+
objects: value.remove.map(input => {
141+
const globalIdObject = fromGlobalId(input);
142+
if (globalIdObject.type === targetClass) {
143+
input = globalIdObject.id;
144+
}
145+
return {
146+
__type: 'Pointer',
147+
className: targetClass,
148+
objectId: input,
149+
};
150+
}),
138151
});
139152
}
140153
return op;
@@ -172,10 +185,15 @@ const transformers = {
172185
};
173186
}
174187
if (value.link) {
188+
let objectId = value.link;
189+
const globalIdObject = fromGlobalId(objectId);
190+
if (globalIdObject.type === targetClass) {
191+
objectId = globalIdObject.id;
192+
}
175193
return {
176194
__type: 'Pointer',
177195
className: targetClass,
178-
objectId: value.link,
196+
objectId,
179197
};
180198
}
181199
},

src/GraphQL/transformers/query.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { fromGlobalId } from 'graphql-relay';
2+
13
const parseQueryMap = {
24
id: 'objectId',
35
OR: '$or',
@@ -102,10 +104,15 @@ const transformQueryConstraintInputToParse = (
102104
typeof fieldValue === 'string'
103105
) {
104106
const { targetClass } = fields[parentFieldName];
107+
let objectId = fieldValue;
108+
const globalIdObject = fromGlobalId(objectId);
109+
if (globalIdObject.type === targetClass) {
110+
objectId = globalIdObject.id;
111+
}
105112
constraints[fieldName] = {
106113
__type: 'Pointer',
107114
className: targetClass,
108-
objectId: fieldValue,
115+
objectId,
109116
};
110117
}
111118
}
@@ -198,7 +205,12 @@ const transformQueryInputToParse = (constraints, fields) => {
198205
}
199206

200207
if (typeof fieldValue === 'object') {
201-
transformQueryConstraintInputToParse(fieldValue, fields, fieldName, constraints);
208+
transformQueryConstraintInputToParse(
209+
fieldValue,
210+
fields,
211+
fieldName,
212+
constraints
213+
);
202214
}
203215
});
204216
};

0 commit comments

Comments
 (0)