Skip to content

Filter on reference fields #272

@UlyssesTech

Description

@UlyssesTech

I am wondering if it is to filter based on fields of a reference scheme(s)?

Let's say we have the following schemas:

var company = schema.Schema{
    "id": schema.IDField,
    "name": {    		
      Filterable: true,
      Sortable:   true,
      Validator: &schema.String{},
  },
}

var machine = schema.Schema{
    "id": schema.IDField,
    "name": {    		
      Filterable: true,
      Sortable:   true,
      Validator: &schema.String{},
    "user": {
      Filterable: true,
      Sortable:   true,
      Validator:  &schema.Reference{Path: "users"},
    },
  },
}

var measurement = schema.Schema{
    "id": schema.IDField,
    "name": {    		
      Filterable: true,
      Sortable:   true,
      Validator: &schema.String{},
    "machine": {
      Filterable: true,
      Sortable:   true,
      Validator:  &schema.Reference{Path: "machines"},
    },
  },
}

In this case a company can have different machines, and each machine can have different measurements. If we want to get all the measurements done by a specific machine, then we can use a filter request, as follows:

$ http GET :8080/measurements filter='{machine: "machine_id"}'

But what if we want to get all the measurements for a specific company? It looks like the following syntax does not work:

$ http GET :8080/measurements filter='{machine.company: "company_id"}'

nor

$ http GET :8080/measurements fields='machine{company(filter: "{id: \"company_id\"}")}'

I know that we can go around it by creating sub-resources or applying a request on "/companies" with proper fields set, but it will be nice if we can filter directly on reference fields as we do on sub-documents.

The following syntax seems really neat to apply:

$ http GET :8080/measurements filter='{machine.company.id: "company_id"}'

Where we can filter on nested reference fields and can choose the field (i.e. the "id", but it can be also "name").

In my case, I solve this by adding company as a reference to measurements as well:

var measurement = schema.Schema{
    "id": schema.IDField,
    "name": {    		
      Filterable: true,
      Sortable:   true,
      Validator: &schema.String{},
    "machine": {
      Filterable: true,
      Sortable:   true,
      Validator:  &schema.Reference{Path: "machines"},
    },
    "company": {
      Filterable: true,
      Sortable:   true,
      Validator:  &schema.Reference{Path: "companies"},
    },
  },
}

This is not optimal as "machine" already includes a reference to the company, but this solves the problem of filtering as we can filter directly on company field from measurements. This makes it easier as well to implement authorization logic in the resource hooks. For example, if a user has just permission to see measurements related to a specific company, I can easily add a filter predicate to the query.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions