mirror of
https://github.com/vale981/grapher
synced 2025-03-05 09:31:42 -05:00
Scoped query PoC
This commit is contained in:
parent
77c4b4b2e1
commit
480614cdc4
5 changed files with 43 additions and 2 deletions
|
@ -184,6 +184,7 @@ export default class extends Base {
|
|||
createGraph(this.collection, body),
|
||||
undefined, {
|
||||
scoped: this.options.scoped,
|
||||
queryScoped: true,
|
||||
subscriptionHandle: this.subscriptionHandle,
|
||||
});
|
||||
}
|
||||
|
|
0
lib/query/lib/applyProps.js
Normal file → Executable file
0
lib/query/lib/applyProps.js
Normal file → Executable file
16
lib/query/lib/createGraph.js
Normal file → Executable file
16
lib/query/lib/createGraph.js
Normal file → Executable file
|
@ -99,6 +99,22 @@ export function addFieldNode(body, fieldName, root) {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns namespace for node when using query path scoping.
|
||||
*
|
||||
* @param node
|
||||
* @returns {String}
|
||||
*/
|
||||
export function getNodeNamespace(node) {
|
||||
const parts = [];
|
||||
let n = node;
|
||||
while (n) {
|
||||
parts.push(n.collection._name);
|
||||
n = n.parent;
|
||||
}
|
||||
return parts.reverse().join('_');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param collection
|
||||
* @param body
|
||||
|
|
23
lib/query/lib/recursiveCompose.js
Normal file → Executable file
23
lib/query/lib/recursiveCompose.js
Normal file → Executable file
|
@ -1,4 +1,19 @@
|
|||
import applyProps from './applyProps.js';
|
||||
import {getNodeNamespace} from './createGraph';
|
||||
|
||||
function patchCursor(cursor, ns) {
|
||||
const originalObserve = cursor.observe;
|
||||
cursor.observe = function (callbacks) {
|
||||
const newCallbacks = {};
|
||||
if (callbacks.added) {
|
||||
newCallbacks.added = doc => {
|
||||
doc[`__query_path_${ns}`] = ns;
|
||||
callbacks.added(doc);
|
||||
};
|
||||
}
|
||||
originalObserve.call(cursor, newCallbacks);
|
||||
};
|
||||
}
|
||||
|
||||
function compose(node, userId) {
|
||||
return {
|
||||
|
@ -18,7 +33,9 @@ function compose(node, userId) {
|
|||
});
|
||||
}
|
||||
|
||||
return accessor.find(filters, options, userId);
|
||||
const cursor = accessor.find(filters, options, userId);
|
||||
patchCursor(cursor, getNodeNamespace(node));
|
||||
return cursor;
|
||||
}
|
||||
},
|
||||
|
||||
|
@ -31,7 +48,9 @@ export default (node, userId, config = {bypassFirewalls: false}) => {
|
|||
find() {
|
||||
let {filters, options} = applyProps(node);
|
||||
|
||||
return node.collection.find(filters, options, userId);
|
||||
const cursor = node.collection.find(filters, options, userId);
|
||||
patchCursor(cursor, getNodeNamespace(node));
|
||||
return cursor;
|
||||
},
|
||||
|
||||
children: _.map(node.collectionNodes, n => {
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import applyProps from './applyProps.js';
|
||||
import { assembleMetadata, removeLinkStorages, storeOneResults } from './prepareForDelivery';
|
||||
import prepareForDelivery from './prepareForDelivery';
|
||||
import {getNodeNamespace} from './createGraph';
|
||||
|
||||
/**
|
||||
* This is always run client side to build the data graph out of client-side collections.
|
||||
|
@ -16,6 +17,10 @@ function fetch(node, parentObject, fetchOptions = {}) {
|
|||
if (fetchOptions.scoped && fetchOptions.subscriptionHandle) {
|
||||
_.extend(filters, fetchOptions.subscriptionHandle.scopeQuery());
|
||||
}
|
||||
// does not make sense without scoped parameter
|
||||
if (fetchOptions.scoped && fetchOptions.queryScoped) {
|
||||
_.extend(filters, {[`__query_path_${getNodeNamespace(node)}`]: {$exists: true}});
|
||||
}
|
||||
|
||||
let results = [];
|
||||
|
||||
|
|
Loading…
Add table
Reference in a new issue