Skip to content
Ghislain B edited this page Oct 25, 2017 · 51 revisions

GraphQL Backend Service to get data from a backend server with the help of GraphQL.

Implementation

To connect a backend service into Angular-Slickgrid, you simply need to modify your gridOptions and add a declaration of onBackendEventChanged. See below for the signature and an example further down below.

TypeScript Signature

onBackendEventApi: {
  onInit?: (query: string) => Promise<any> | Observable<any>;
  preProcess?: () => void;
  process: (query: string) => Promise<any> | Observable<any>;
  postProcess: (response: any) => void;
  service: BackendService;
  filterTypingDebounce?: number;
}

As you can see, you mainly need to define which service to use (GridODataService or GraphQLService) and finally add the process and postProcess callback.

App Module

Modify your app.module.ts to include the GraphqlService

import { AngularSlickgridModule, GraphqlService } from 'angular-slickgrid';
// ...

@NgModule({
  declarations: [
    AppComponent,
    // ...
  ],
  imports: [
    AppRoutingRoutingModule,
    // ...
    AngularSlickgridModule
  ],
  providers: [GraphqlService],
  bootstrap: [AppComponent]
})
export class AppModule { }

Grid Definition & call of onBackendEventApi

Note:

  • Pagination is optional and if not defined, it will use what is set in the Angular-Slickgrid - Global Options
  • onInit is optional and is there to initialize the grid with data on first page load (typically the same call as process)
    • you could load the grid yourself outside of the gridOptions which is why it's optional
  • filterTypingDebounce is a timer (in milliseconds) that waits for user input pause before querying the backend server
    • this is meant to throttle the amount of requests sent to the backend (we don't really want to query every keystroke)
    • 700ms is the default when not provided
import { Component, Injectable, OnInit } from '@angular/core';
import { GraphqlService } from 'angular-slickgrid';

@Injectable()

export class MyComponent implements OnInit {
  columnDefinitions: Column[];
  gridOptions: GridOption;
  dataset = [];

  constructor(private http: HttpClient, private graphqlService: GraphqlService) {
  }
  ngOnInit(): void {
    this.columnDefinitions = [
      // your column definitions
    ];

    this.gridOptions = {
      enableFiltering: true,
      enablePagination: true,
      pagination: {
        pageSizes: [10, 15, 20, 25, 30, 40, 50, 75, 100],
        pageSize: defaultPageSize,
        totalItems: 0
      },
      onBackendEventApi: {
        preProcess: () => this.displaySpinner(true),
        process: (query) => this.getCustomerApiCall(query),
        postProcess: (response) => {
          this.displaySpinner(false);
          this.getCustomerCallback(response);
        },
        filterTypingDebounce: 700,
        service: this.graphqlService
      }
    };
  }

  // Web API call
  getCustomerApiCall(graphqlQuery) {
    return this.http.get(`/api/getCustomers?${graphqlQuery}`).toPromise();
  }

GraphQL Server Definition

For the implementation of all 3 actions (filtering, sorting, pagination) with your GraphQL Server, please refer to the sections below to configure your GraphQL Schema accordingly.

Contents

Clone this wiki locally