SoftInstigate / restheart

Rapid API Development with MongoDB
https://restheart.org
GNU Affero General Public License v3.0
805 stars 171 forks source link

Support dot notation in aggregation $var and graphql mapping $arg operator #473

Closed ujibang closed 12 months ago

ujibang commented 1 year ago

Brief overview

aggregation and GraphQL app definition mappings allow using the $var and $arg operators respectively.

The argument value can be any bson, as a sting or an object.

This feature is about allowing to use the dot notation when the argument value is an object to reference nested values.

aggregation

This would allow to define an aggregation as follows (the same applies to GraphQL mappings):

{
  "stages": [
    { "$match": { "name": { "$var": "user.name" } } },
    { "$match": { "age": { "$var": "user.age" } } },
  ],
  "uri": "foo"
}

The user arg is passed as follows:

GET /coll/_aggrs/foo?avars={"user": { "name": "Sara", "age": 1 }}

Currently this can only be achieved using with two variables:

{
  "stages": [
    { "$match": { "name": { "$var": "name" } } },
    { "$match": { "age": { "$var": "age" } } },
  ],
  "uri": "foo"
}
GET /coll/_aggrs/foo?avars={"name": "Sara", "age": 1 }

graphql mapping

Consider the following GraphQL app definition that uses an input type:

{
    "descriptor": { ... },
    "schema": "type User { _id: String } input Options {limit: Int, skip: Int } type Query { users(options: Options): [User] }",
    "mappings": {
        "Query": {
            "users": {
                "db": "restheart",
                "collection": "the-users",
                "limit": {"$arg": "options.limit"}, <--- dot notation
                "skip": {"$arg": "options.skip"}  <--- dot notation
            }
        }
    }
}

We can request the following query:

{ 
    users(options: { skip: 0, limit:5 }) {
        _id
    }
}

Rationale

Referencing nested arguments would be useful for implementing querying operations like filtering and sorting on specific fields.

Support the dot notation in GraphQL mappings enables using complex input types.

Detailed documentation

Support of dot notation in:

ujibang commented 12 months ago

documented in https://restheart.org/docs/mongodb-rest/aggregations#parametric-aggregations and https://restheart.org/docs/mongodb-graphql/#mappings-operators