Skip to content

Commit

Permalink
distinct_on support (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
bharatkashyap authored Aug 16, 2022
1 parent 080e0aa commit 24e82fd
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,33 @@ will generate the following GraphQL query variables:
}
```

and

```jsx
export const AddressList = () => (
<List
sort={{ field: 'city', order: 'DESC' }}
filter={{ distinct_on: 'city' }}
>
...
</List>
);
```

will generate the following GraphQL query variables:

```json
{
// ...
"order_by": {
"city": "desc"
},
"distinct_on": "city"
}
```

Keep in mind that `distinct_on` must be used in conjunction with `order_by`, otherwise a `"distinct_on" columns must match initial "order_by" columns"` error will result. See more [here](https://hasura.io/docs/latest/queries/postgres/distinct-queries/#the-distinct_on-argument).

## Options

### Customize the Apollo client
Expand Down
12 changes: 12 additions & 0 deletions src/buildVariables/buildGetListVariables.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,14 @@ export const buildGetListVariables: BuildGetListVariables =
let { filter: filterObj = {} } = params;
const { customFilters = [] } = params;

const distinctOnField = 'distinct_on';
/** Setting "distinct_on" to be the `filters` object attribute to be used inside RA
* and setting to a `distinct_on` variable
* and removing from the filter object
*/
const { distinct_on = '' } = filterObj;
filterObj = omit(filterObj, [distinctOnField]);

/**
* Nested entities are parsed by CRA, which returns a nested object
* { 'level1': {'level2': 'test'}}
Expand Down Expand Up @@ -173,5 +181,9 @@ export const buildGetListVariables: BuildGetListVariables =
}
}

if (distinct_on) {
result["distinct_on"] = distinct_on;
}

return result;
};

0 comments on commit 24e82fd

Please sign in to comment.