import { Query, DataManager, UrlAdaptor } from "@syncfusion/ej2-data";
/**
* GraphQL Adaptor with Essentail JS2 Grid
*/
export class CustomGraphQLAdaptor extends UrlAdaptor {
public opt: IGraphQLAdaptorOptions;
public schema: { result: string, count: string };
public query: string;
public getVariables: Function;
public getQuery: Function;
constructor(options: IGraphQLAdaptorOptions) {
super();
this.opt = options;
this.schema = this.opt.response;
this.query = this.opt.query;
// tslint:disable-next-line:no-empty
this.getVariables = () => { };
this.getQuery = () => this.query;
}
processQuery(datamanager: DataManager, query: Query) {
var result = super.processQuery.apply(this, arguments);
var tmp = JSON.parse(result.data);
var vars = this.getVariables() || {};
vars['datamanager'] = tmp;
var data = JSON.stringify({
query: this.getQuery(),
variables: vars
});
result.data = data;
return result; // form query based on your need
}
processResponse(data: any, context: any, query: any) {
return data.data.orders; // should return result and count
}
generateData(inserted: { data: string }, action: string) {
let parsed = JSON.parse(inserted.data);
inserted.data = JSON.stringify({
query: this.opt.getMutation(action),
variables: parsed
});
return inserted;
}
}
let data: Object = new DataManager({
url: 'http://localhost:4000/graphql',
adaptor: new CustomGraphQLAdaptor({
response: {
result: 'orders',
count: 'count'
},
query: `query Order($datamanager: DataManager) { // initial loading
orders(datamanager: $datamanager) {
result { // column names are defined
CustomerID,
ShipName,
Freight
},
count
}
}`
})
});
. . . . .
export interface IGraphQLAdaptorOptions {
response: { result: string, count: string };
query: string;
getQuery?: () => string;
getVariables?: () => Object;
getMutation?: (action: string) => string;
}
|
|
|
<ejs-grid #grid [dataSource]='data | async' allowPaging='true' [editSettings]='editSettings' allowGrouping='true'
allowReordering='true' (actionFailure)="actionComplete($event)" [toolbar]='toolbar' (dataStateChange)='dataStateChange($event)'>
<e-columns>
<e-column field='id' headerText='Customer ID' width='120' textAlign='Right' isPrimaryKey='true'></e-column>
<e-column field="name" headerText="City" foreignKeyField="name" foreignKeyValue="ShipCity" [dataSource]='colData'
width="150"></e-column>
</e-columns>
</ejs-grid>
public ngOnInit(): void {
. . . . .
this.crudService.execute(state);
const fData: Object[] = [
{ id: 1, name: 'HANAR', ShipCity: 'Germany' },
{ id: 2, name: 'VINER', ShipCity: 'Berlin' },
. . . . .
] // you can get the foreign key data and assign to data manager
this.colData = new DataManager({
json: fData
});
}
|
<ejs-grid #Grid [dataSource]='data | async' allowPaging= 'true' [pageSettings]='pageOptions' allowSorting= 'true' allowGrouping= 'true' (dataStateChange)= 'dataStateChange($event)' (actionFailure)='fail($event)'>
<e-columns>
. . . . .
<e-column field= "ShipCity" headerText="Ship City" width="150" foreignKeyField="ShipCity" foreignKeyValue="OrderID" [dataSource]='colData'></e-column>
</e-columns>
</ejs-grid>
|
export class AsyncPipeComponent {
. . . . .
public dataStateChange(state: DataStateChangeEventArgs): void {
this.service.execute(state);
}
public colData: any;
public ngOnInit(): void {
. . . . .
this.service.subscribe((args)=>{
// you can directly assign json result to colData or assign to data manager
this.colData = new DataManager({json: (args as any).result});
});
}
|
class CustomGraphQLAdaptor extends UrlAdaptor {
processResponse(): Object {
let original: Object[] = super.processResponse.apply(this, arguments);
return original;
}
}
export class AppComponent {
public eventSettings: EventSettingsModel = {
dataSource: new DataManager({
url: 'http://localhost:54738/Home/LoadData', // Here need to mention the web api sample running path
crudUrl: 'http://localhost:54738/Home/UpdateData',
crossDomain: true,
adaptor: new CustomGraphQLAdaptor
}),
};
} |
I ran into the same problem. We use Axios for our requests, which includes middleware to handle authentication (token refreshing and whatnot). We want to reuse these Axios instances to fetch data. This seemed like a very common use-case.
I've tried to extend DataManager so that I can avoid Syncfusion from doing any HTTP calls by itself, but it is unclear what `DataManager.executeQuery` expects its implementation to be returning exactly. It seems it needs some sort of Response, but it is unclear whether that's a custom Syncfusion response, a fetch response or a XMLHttp response. Whatever the case, I don't have access to any of those responses, as I'm using Axios.
The documentation is lacking in this regard.
Hi Paul Dreef,
Greetings from Syncfusion support,
We understand that you want to integrate Axios with Syncfusion's DataManager in the ej2 Grid to handle HTTP requests, especially with your custom authentication setup. We can suggest using Syncfusion’s custom binding (Observable Binding )features to achieve this seamlessly.
When using Observable Binding for remote data, you can create your own service to handle the querying part and resolve the query on the server side. When performing Grid actions such as Paging, Filtering, Sorting, etc., the dataStateChange event will be triggered. In this event, you have to query and resolve data using Observables based on the state arguments. This technique can be used when you need full control over the generated query.
You can find documentation for Observable Binding linked below, which also contains a working sample:
Documentation: https://ej2.syncfusion.com/angular/documentation/grid/data-binding/remote-data
You also mentioned being unclear about the executeQuery method. The executeQuery method in Syncfusion's DataManager executes the given query with either a local or remote data source. It performs this operation asynchronously and returns a Promise object, which will be resolved or rejected after the action is completed.
Please refer the below Api for more reference,
Api: https://ej2.syncfusion.com/documentation/api/data/dataManager/#executequery
Also, we have logged the improvement task internally (“Add the documentation on AXIOS with Syncfusion grid and datamanager ”) for the UG documentation, and it will be refreshed online ASAP.
Please get back to us if you need further assistance.
Regards,
Vikram S