GraphQL

index

Description

GraphQL Backend Service (for Pagination purposes) to get data from a backend server with the help of GraphQL.

Demo

Demo Page / Demo ViewModel

Note

You can use it when you need to support Pagination (though you could disable Pagination if you wish), that is when your dataset is rather large and has typically more than 5k rows, with a GraphQL endpoint. If your dataset is small (less than 5k rows), then you might be better off with regular grid with the "dataset.bind" property. SlickGrid can easily handle million of rows using a DataView object, but personally when the dataset is known to be large, I usually use a backend service (OData or GraphQL) and when it's small I go with a regular grid.

Implementation

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

TypeScript Signature

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

Typescript GraphQL Service Options

You can also pass certain options to the backendServiceApi through the options property. The list of options is the following

Grid Definition & call of backendServiceApi

Notes

  • Pagination is optional and if not defined, it will use what is set in the Slickgrid-Universal - 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

Code

Extra Query Arguments

You can pass extra query arguments to the GraphQL query via the extraQueryArguments property defined in the backendServiceApi.options. For example let say you have a list of users and your GraphQL query accepts an optional userId, you can write it in code this way:

The GraphQL query built with these options will be

Changing/Updating Options Dynamically

You might want to change certain options dynamically, for example passing new set of values to extraQueryArguments. For that you will have to first keep a reference to your GraphqlService instance and then you can call the updateOptions method.

Code Example

GraphQL without Pagination

By default, the Pagination is enabled and will produce a GraphQL query which includes page related information but you could also use the GraphQL Service without Pagination if you wish by disabling the flag enablePagination: false in the Grid Options. However please note that the GraphQL Query will be totally different since it won't include any page related information.

Code Example

Query Change Example

If we take for example a GrahQL Query that includes Pagination versus without Pagination, you will see a much simpler query string. Also, note that the filtering and sorting won't be affected, they will remain as query input.

with Pagination

  1. query{ users(first:20, offset:40){ totalCount, nodes{ id, field1, field2 }}}

  2. query{ users(first:20, offset:40, filterBy: [{ field: field1, value: 'test', operator: StartsWith }]){ totalCount, nodes{ id, field1, field2 }}}

without Pagination

  1. query{ users{ id, field1, field2 }}

  2. query{ users(filterBy: [{ field: field1, value: 'test', operator: StartsWith }]){ id, field1, field2 }}

GraphQL Server Definitions

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.

Last updated