Skip to content

Commit 255cb15

Browse files
authored
GraphQL: Handle properly keys for pointer fields (#6499)
* Fix Unknow type bug on overloaded types * check args too * Additional fix to detect custom fields on pointer
1 parent 312a4bc commit 255cb15

File tree

4 files changed

+68
-19
lines changed

4 files changed

+68
-19
lines changed

src/GraphQL/helpers/objectsQueries.js

Lines changed: 41 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,32 @@ import { offsetToCursor, cursorToOffset } from 'graphql-relay';
33
import rest from '../../rest';
44
import { transformQueryInputToParse } from '../transformers/query';
55

6-
const needToGetAllKeys = (fields, keys) =>
6+
// Eslint/Prettier conflict
7+
/* eslint-disable*/
8+
const needToGetAllKeys = (fields, keys, parseClasses) =>
79
keys
8-
? !!keys.split(',').find(keyName => !fields[keyName.split('.')[0]])
10+
? keys.split(',').some(keyName => {
11+
const key = keyName.split('.');
12+
if (fields[key[0]]) {
13+
if (fields[key[0]].type === 'Pointer') {
14+
const subClass = parseClasses.find(
15+
({ className: parseClassName }) =>
16+
fields[key[0]].targetClass === parseClassName
17+
);
18+
if (subClass && subClass.fields[key[1]]) {
19+
// Current sub key is not custom
20+
return false;
21+
}
22+
} else if (!key[1]) {
23+
// current key is not custom
24+
return false;
25+
}
26+
}
27+
// Key not found into Parse Schema so it's custom
28+
return true;
29+
})
930
: true;
31+
/* eslint-enable*/
1032

1133
const transformOrder = order =>
1234
order
@@ -32,11 +54,23 @@ const getObject = async (
3254
config,
3355
auth,
3456
info,
35-
parseClass
57+
parseClasses
3658
) => {
3759
const options = {};
38-
if (!needToGetAllKeys(parseClass.fields, keys)) {
39-
options.keys = keys;
60+
try {
61+
if (
62+
!needToGetAllKeys(
63+
parseClasses.find(
64+
({ className: parseClassName }) => className === parseClassName
65+
).fields,
66+
keys,
67+
parseClasses
68+
)
69+
) {
70+
options.keys = keys;
71+
}
72+
} catch (e) {
73+
console.log(e);
4074
}
4175
if (include) {
4276
options.include = include;
@@ -158,7 +192,8 @@ const findObjects = async (
158192
parseClasses.find(
159193
({ className: parseClassName }) => className === parseClassName
160194
).fields,
161-
keys
195+
keys,
196+
parseClasses
162197
)
163198
) {
164199
options.keys = keys;

src/GraphQL/loaders/defaultRelaySchema.js

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,7 @@ const load = parseGraphQLSchema => {
3131
config,
3232
auth,
3333
info,
34-
parseGraphQLSchema.parseClasses.find(
35-
({ className }) => type === className
36-
)
34+
parseGraphQLSchema.parseClasses
3735
)),
3836
};
3937
} catch (e) {

src/GraphQL/loaders/parseClassMutations.js

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ const load = function(
114114
);
115115
const needToGetAllKeys = objectsQueries.needToGetAllKeys(
116116
parseClass.fields,
117-
keys
117+
keys,
118+
parseGraphQLSchema.parseClasses
118119
);
119120
let optimizedObject = {};
120121
if (needGet && !needToGetAllKeys) {
@@ -128,7 +129,7 @@ const load = function(
128129
config,
129130
auth,
130131
info,
131-
parseClass
132+
parseGraphQLSchema.parseClasses
132133
);
133134
} else if (needToGetAllKeys) {
134135
optimizedObject = await objectsQueries.getObject(
@@ -141,7 +142,7 @@ const load = function(
141142
config,
142143
auth,
143144
info,
144-
parseClass
145+
parseGraphQLSchema.parseClasses
145146
);
146147
}
147148
return {
@@ -232,7 +233,8 @@ const load = function(
232233
);
233234
const needToGetAllKeys = objectsQueries.needToGetAllKeys(
234235
parseClass.fields,
235-
keys
236+
keys,
237+
parseGraphQLSchema.parseClasses
236238
);
237239
let optimizedObject = {};
238240
if (needGet && !needToGetAllKeys) {
@@ -246,7 +248,7 @@ const load = function(
246248
config,
247249
auth,
248250
info,
249-
parseClass
251+
parseGraphQLSchema.parseClasses
250252
);
251253
} else if (needToGetAllKeys) {
252254
optimizedObject = await objectsQueries.getObject(
@@ -259,7 +261,7 @@ const load = function(
259261
config,
260262
auth,
261263
info,
262-
parseClass
264+
parseGraphQLSchema.parseClasses
263265
);
264266
}
265267
return {
@@ -337,7 +339,7 @@ const load = function(
337339
config,
338340
auth,
339341
info,
340-
parseClass
342+
parseGraphQLSchema.parseClasses
341343
);
342344
}
343345
await objectsMutations.deleteObject(

src/GraphQL/loaders/parseClassQueries.js

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,14 @@ const getParseClassQueryConfig = function(
1414
return (parseClassConfig && parseClassConfig.query) || {};
1515
};
1616

17-
const getQuery = async (parseClass, _source, args, context, queryInfo) => {
17+
const getQuery = async (
18+
parseClass,
19+
_source,
20+
args,
21+
context,
22+
queryInfo,
23+
parseClasses
24+
) => {
1825
let { id } = args;
1926
const { options } = args;
2027
const { readPreference, includeReadPreference } = options || {};
@@ -39,7 +46,7 @@ const getQuery = async (parseClass, _source, args, context, queryInfo) => {
3946
config,
4047
auth,
4148
info,
42-
parseClass
49+
parseClasses
4350
);
4451
};
4552

@@ -80,7 +87,14 @@ const load = function(
8087
),
8188
async resolve(_source, args, context, queryInfo) {
8289
try {
83-
return await getQuery(parseClass, _source, args, context, queryInfo);
90+
return await getQuery(
91+
parseClass,
92+
_source,
93+
args,
94+
context,
95+
queryInfo,
96+
parseGraphQLSchema.parseClasses
97+
);
8498
} catch (e) {
8599
parseGraphQLSchema.handleError(e);
86100
}

0 commit comments

Comments
 (0)