ra-data-hasura: filter for count query, add httpClient (close #2100, #2741, #2771) (#2727)

* filter for count in GET_LIST and GET_MANY_REFERENCE

* update deps, add httpClient argument, release new version
This commit is contained in:
Praveen Durairaj 2019-09-18 08:06:17 +05:30 committed by Shahidh K Muhammed
parent 3b5886ed92
commit 0a64ef99b5
5 changed files with 1530 additions and 1647 deletions

View File

@ -1,3 +1,9 @@
## 0.0.7 (September 17, 2019)
- Bug Fix: Re-build library to fix discrepancies. Pass `where` arguments to `count` query.
- Feature: Add support for httpClient to pass in dynamic headers. Backwards compatibility maintained for static headers.
- Update package dependencies.
## 0.0.6 (June 14, 2019)
- Bug Fix: Fix sort order, fix primary key when response not an array and add filters to GET_* operations.

View File

@ -14,12 +14,12 @@ The `ra-data-hasura` provider accepts three arguments:
- `serverEndpoint` - The URL at which Hasura GraphQL Engine is running. (for example: http://localhost:8080). This is required. It should also expose `/v1/query` endpoint.
- `headers` - An optional argument. Pass your auth headers here.
- `httpClient` - HTTP Client function. To maintain backwards compatibility the `headers` object is supported.
- `config` - An optional argument. Pass your config here.
```
hasuraDataProvider(serverEndpoint, headers, config)
hasuraDataProvider(serverEndpoint, httpClient, config)
```
In the following example, we import `hasuraDataProvider` from `ra-data-hasura` and give it the hasura server endpoint (assumed to be running at http://localhost:8080) and an optional headers object.
@ -62,6 +62,22 @@ export default App;
In case the server is configured with admin secret or auth, configure the appropriate headers and pass it to the provider.
### Adding Custom Headers
The above example showed a simple use case of adding static headers. In order to update headers dynamically, the data provider accepts an HTTP client function as the second argument. It uses react-admin's fetchUtils.fetchJson() as HTTP client. Hence to add custom headers to your requests, you just need to wrap the `fetchUtils.fetchJson()` call inside your own function:
```javascript
const httpClient = (url, options = {}) => {
if (!options.headers) {
options.headers = new Headers({ Accept: 'application/json' });
}
// add your own headers here
options.headers.set('Authorization', 'Bearer xxxxx');
return fetchUtils.fetchJson(url, options);
};
const dataProvider = hasuraDataProvider('http://localhost:8080', httpClient);
```
### Multiple schemas
To query schemas other than `public`, you can pass schema to resource in the format

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "ra-data-hasura",
"version": "0.0.6",
"version": "0.0.7",
"description": "react-admin data provider for Hasura GraphQL Engine",
"main": "lib/ra-data-hasura.min.js",
"scripts": {

View File

@ -13,7 +13,7 @@ const cloneQuery = (query) => {
return JSON.parse(JSON.stringify(query));
};
export default (serverEndpoint, headers, config) => {
export default (serverEndpoint, httpClient, config) => {
const getTableSchema = (resource) => {
let tableName;
@ -47,9 +47,11 @@ export default (serverEndpoint, headers, config) => {
if (!filter) return where;
const filterKeys = Object.keys(filter);
if (filterKeys.length === 0) return where;
const whereCopy = Object.assign(where)
const whereCopy = Object.assign(where);
filterKeys.forEach((key) => {
whereCopy[key] = filter[key];
});
@ -147,31 +149,42 @@ export default (serverEndpoint, headers, config) => {
break;
case 'GET_MANY':
// select multiple within where clause
finalQuery = cloneQuery(selectQuery);
finalQuery.args.table = {'name': tableName, 'schema': schema};
finalQuery.args.where = {};
finalQuery.args.where[primaryKey] = { '$in': params.ids };
finalQuery.args.where = addFilters(finalQuery.args.where, params.filter);
break;
case 'GET_MANY_REFERENCE':
// select multiple with relations
const finalManyQuery = cloneQuery(selectQuery);
const finalManyCountQuery = cloneQuery(countQuery);
finalManyQuery.args.table = {'name': tableName, 'schema': schema};
finalManyQuery.args.limit = params.pagination.perPage;
finalManyQuery.args.offset = (params.pagination.page * params.pagination.perPage) - params.pagination.perPage;
finalManyQuery.args.where = { [params.target]: params.id };
finalManyQuery.args.where = {};
finalManyQuery.args.where[primaryKey] = { '$in': params.ids };
finalManyQuery.args.where = addFilters(finalManyQuery.args.where, params.filter);
finalManyQuery.args.order_by = {column: params.sort.field || primaryKey, type: params.sort.order.toLowerCase()};
finalManyCountQuery.args.table = {'name': tableName, 'schema': schema};;
finalManyCountQuery.args.where = {};
finalManyCountQuery.args.where[primaryKey] = { '$ne': null };
finalManyCountQuery.args.where = addFilters(finalManyQuery.args.where, params.filter);
finalManyCountQuery.args.where = addFilters(finalManyCountQuery.args.where, params.filter);
finalQuery = cloneQuery(bulkQuery);
finalQuery.args.push(finalManyQuery);
finalQuery.args.push(finalManyCountQuery);
break;
case 'GET_MANY_REFERENCE':
// select multiple with relations
const finalManyRefQuery = cloneQuery(selectQuery);
const finalManyRefCountQuery = cloneQuery(countQuery);
finalManyRefQuery.args.table = {'name': tableName, 'schema': schema};
finalManyRefQuery.args.limit = params.pagination.perPage;
finalManyRefQuery.args.offset = (params.pagination.page * params.pagination.perPage) - params.pagination.perPage;
finalManyRefQuery.args.where = { [params.target]: params.id };
finalManyRefQuery.args.where = addFilters(finalManyRefQuery.args.where, params.filter);
finalManyRefQuery.args.order_by = {column: params.sort.field || primaryKey, type: params.sort.order.toLowerCase()};
finalManyRefCountQuery.args.table = {'name': tableName, 'schema': schema};;
finalManyRefCountQuery.args.where = {};
finalManyRefCountQuery.args.where[primaryKey] = { '$ne': null };
finalManyRefCountQuery.args.where = addFilters(finalManyRefQuery.args.where, params.filter);
finalQuery = cloneQuery(bulkQuery);
finalQuery.args.push(finalManyRefQuery);
finalQuery.args.push(finalManyRefCountQuery);
break;
default:
throw new Error(`Unsupported type ${type}`);
};
@ -190,7 +203,7 @@ export default (serverEndpoint, headers, config) => {
if (Array.isArray(response[0])) {
response[0].forEach((res) => {
res[DEFAULT_PRIMARY_KEY] = res[primaryKey];
})
});
} else {
response[0][DEFAULT_PRIMARY_KEY] = response[0][primaryKey];
}
@ -235,7 +248,7 @@ export default (serverEndpoint, headers, config) => {
};
case 'GET_MANY':
return {
data: response
data: response[0]
};
case 'GET_MANY_REFERENCE':
return {
@ -255,7 +268,13 @@ export default (serverEndpoint, headers, config) => {
);
options.method = 'POST';
options.headers = headers;
if (typeof (httpClient) === 'function') { // support httpClient argument
return httpClient(serverEndpoint + '/v1/query', options).then(response =>
convertHTTPResponse(response.json, type, resource, params)
);
}
options.headers = httpClient; // backwards compatible static header object
return fetch(serverEndpoint + '/v1/query', options).then(function (response) {
return response.json().then((data) => {
return convertHTTPResponse(data, type, resource, params);