Search code examples
graphqlreact-adminhasura

Fetching only distinct data from table using hasura-graphql data provider in react-admin


I am working on a project where I have to fetch only distinct data from the table display in the dropdown. How can I do that? I am using '''hasura-graphql''' data-provider for it. So how can I get only distinct data from a particular column?

Thanks in advance.


Solution

  • I think passing a default "distinct_on" filter with the column name as value will do the job. Also, hasura recommends sorting by this column in first position.

    It is typically recommended to use order_by along with distinct_on to ensure we get predictable results (otherwise any arbitrary row with a distinct value of the column may be returned). Note that the distinct_on column needs to be the first column in the order_by expression.

    So i set default sort:

    <ReferenceInput
        reference="yourTable"
        source="yourDistinctColumn"
        sort={{field: "yourDistinctColumn", order: "ASC"}}//or DESC, your choice
        filter={{distinct_on: "yourDistinctColumn"}}
    >
        <SelectInput optionText="yourDistinctColumn"/>
    </ReferenceInput>
    

    https://hasura.io/docs/1.0/graphql/manual/queries/distinct-queries.html