cube-js / cube-js/cube

Add tags/identifiers to query requests made to Cube.js

Open
#8,287 0 comments 3 reactions 0 assignees View on GitHub
Dominant language
Rust
Stars
20.8k
Forks
2.1k
Avg merge
1d 2h
Merged PRs (30d)
181

Description

**Is your feature request related to a problem? Please describe.**
Hi! I have a regular BI dashboard React application that uses Cube.js as it's backend query layer. There are dozens of visualizations in this BI application, and each one of them dispatches one or more requests to our Cube backend. It would be really useful to be able to tag each of the dozen+ queries so it becomes much easier to track down and handle different queries on the backend.

Say "one of the queries" or even "all queries that fetch data from a specific table" have to bypass the regular security rules defined in ```queryRewrite```. The only way I found to do that is literally check if the stringified version of the query contains a very specific set of dimensions/measures/filters, and bypass adding filters to it in ```queryRewrite``` if the criteria is met, which sounds like a VERY bad and ugly workaround, as other queries can have similar or identical structures, but maybe come from different components in my frontend application.

**Describe the solution you'd like**
Be able to pass along an extra ```context``` or ```tag``` parameter to the query object when making a request to Cube.js. This parameter is then easily accessible in the ```cube.js``` or ```cube.py``` file, and I can handle edge cases with simple ```if``` statements.

A sample query object could be (POST this to your _\/cubejs-api/v1/load_ with proper headers and this request body):
```
{
"query": {
"measures": [
"myCube.count"
],
"dimensions": [
"myCube.someField"
],
// New context/tag/metadata object
"context": {
"someTag": "ThisQueryWasTriggeredByXYZComponent",
"someOtherTag": "SomeOtherValue",
}}
}
```

Then in my ```cube.js``` or ```cube.py``` file, I can do something like this:
```
function applySecurityContextToQuery(query, securityContext) {
console.log(query['context']['someTag']) // Outputs: ThisQueryWasTriggeredByXYZComponent

if (query['context']['someTag'] == 'ThisQueryWasTriggeredByXYZComponent') {
// Do something here is query has this specific tag. The user has control over what is done so warn them to be careful.
// Let's remove one of the filters in this example:
query.filters = query.filters.filter(
filter => filter.member !== "someCube.someField"
);
}
```
**Describe alternatives you've considered**
Currently my checks are done by literally stringifying the received query and checking for specific patterns in the dimension and measure fields. Conditionals are triggered based on that. You can imagine the mess.

**Additional context**
No further context is needed.

Anyone ever faced this? How did you solve it?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.