Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

distinct_on support #124

Merged
merged 2 commits into from
Aug 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
};