Skip to content

Commit ff3b62f

Browse files
authored
Merge 1df212e into 8d3117e
2 parents 8d3117e + 1df212e commit ff3b62f

File tree

2 files changed

+32
-1
lines changed

2 files changed

+32
-1
lines changed

spec/CloudCode.spec.js

+25
Original file line numberDiff line numberDiff line change
@@ -2510,6 +2510,31 @@ describe('beforeFind hooks', () => {
25102510
expect(res2.get('pointerFieldArray')[0].get('aField')).toBe('aFieldValue');
25112511
expect(spy).toHaveBeenCalledTimes(2);
25122512
});
2513+
2514+
it('should have access to context in include query in beforeFind hook', async () => {
2515+
let beforeFindTestObjectCalled = false;
2516+
let beforeFindTestObject2Called = false;
2517+
const obj1 = new Parse.Object('TestObject');
2518+
const obj2 = new Parse.Object('TestObject2');
2519+
obj2.set('aField', 'aFieldValue');
2520+
await obj2.save();
2521+
obj1.set('pointerField', obj2);
2522+
await obj1.save();
2523+
Parse.Cloud.beforeFind('TestObject', req => {
2524+
expect(req.context).toBeDefined();
2525+
expect(req.context.a).toEqual('a');
2526+
beforeFindTestObjectCalled = true;
2527+
});
2528+
Parse.Cloud.beforeFind('TestObject2', req => {
2529+
expect(req.context).toBeDefined();
2530+
expect(req.context.a).toEqual('a');
2531+
beforeFindTestObject2Called = true;
2532+
});
2533+
const query = new Parse.Query('TestObject');
2534+
await query.include('pointerField').find({ context: { a: 'a' } });
2535+
expect(beforeFindTestObjectCalled).toBeTrue();
2536+
expect(beforeFindTestObject2Called).toBeTrue();
2537+
});
25132538
});
25142539

25152540
describe('afterFind hooks', () => {

src/RestQuery.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -478,6 +478,7 @@ _UnsafeRestQuery.prototype.replaceInQuery = async function () {
478478
className: inQueryValue.className,
479479
restWhere: inQueryValue.where,
480480
restOptions: additionalOptions,
481+
context: this.context,
481482
});
482483
return subquery.execute().then(response => {
483484
transformInQuery(inQueryObject, subquery.className, response.results);
@@ -537,6 +538,7 @@ _UnsafeRestQuery.prototype.replaceNotInQuery = async function () {
537538
className: notInQueryValue.className,
538539
restWhere: notInQueryValue.where,
539540
restOptions: additionalOptions,
541+
context: this.context,
540542
});
541543

542544
return subquery.execute().then(response => {
@@ -609,6 +611,7 @@ _UnsafeRestQuery.prototype.replaceSelect = async function () {
609611
className: selectValue.query.className,
610612
restWhere: selectValue.query.where,
611613
restOptions: additionalOptions,
614+
context: this.context,
612615
});
613616

614617
return subquery.execute().then(response => {
@@ -671,6 +674,7 @@ _UnsafeRestQuery.prototype.replaceDontSelect = async function () {
671674
className: dontSelectValue.query.className,
672675
restWhere: dontSelectValue.query.where,
673676
restOptions: additionalOptions,
677+
context: this.context,
674678
});
675679

676680
return subquery.execute().then(response => {
@@ -860,6 +864,7 @@ _UnsafeRestQuery.prototype.handleInclude = function () {
860864
this.auth,
861865
this.response,
862866
this.include[0],
867+
this.context,
863868
this.restOptions
864869
);
865870
if (pathResponse.then) {
@@ -946,7 +951,7 @@ _UnsafeRestQuery.prototype.handleAuthAdapters = async function () {
946951
// Adds included values to the response.
947952
// Path is a list of field names.
948953
// Returns a promise for an augmented response.
949-
function includePath(config, auth, response, path, restOptions = {}) {
954+
function includePath(config, auth, response, path, context, restOptions = {}) {
950955
var pointers = findPointers(response.results, path);
951956
if (pointers.length == 0) {
952957
return response;
@@ -1026,6 +1031,7 @@ function includePath(config, auth, response, path, restOptions = {}) {
10261031
className,
10271032
restWhere: where,
10281033
restOptions: includeRestOptions,
1034+
context: context,
10291035
});
10301036
return query.execute({ op: 'get' }).then(results => {
10311037
results.className = className;

0 commit comments

Comments
 (0)