https://github.com/miniql/miniql-lazy
Creates a MiniQL query resolver for lazily loaded data.
https://github.com/miniql/miniql-lazy
Last synced: 15 days ago
JSON representation
Creates a MiniQL query resolver for lazily loaded data.
- Host: GitHub
- URL: https://github.com/miniql/miniql-lazy
- Owner: miniql
- License: mit
- Created: 2020-08-11T06:44:56.000Z (almost 6 years ago)
- Default Branch: master
- Last Pushed: 2022-05-30T09:20:53.000Z (about 4 years ago)
- Last Synced: 2025-02-25T23:34:31.452Z (over 1 year ago)
- Language: TypeScript
- Size: 68.4 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# @miniql/lazy
A [MiniQL](https://github.com/miniql/miniql) query resolver lazily loaded data. This MiniQL plugin is designed to be used to build other plugins.
Any problems? Please log an issue on this repo.
Love this? Please [star the repo](https://github.com/miniql/miniql) and [support my work](https://www.codecapers.com.au/about#support-my-work)
## Using it
Install the modules in your Node.js project:
```bash
npm install --save miniql
npm install --save @miniql/lazy
```
Import the modules (JavaScript):
```javascript
const { miniql } = require("miniql");
const { createQueryResolver } = require("@miniql/lazy");
```
Import the modules (TypeScript):
```typescript
import { miniql } from "miniql";
import { createQueryResolver } from "@miniql/lazy";
```
Then create a configuration for your data:
```javascript
//
// Configures the query resolver.
//
const queryConfig = {
species: {
primaryKey: "name",
nested: {
homeworld: {
parentKey: "homeworld",
from: "planet",
},
},
},
planet: {
primaryKey: "name",
nested: {
species: {
foreignKey: "homeworld",
},
},
},
};
```
Now create a lazy data loader:
```javascript
const dataLoader = {
//
// Loads a single entity.
//
loadSingleEntity = async (entityTypeName: string, primaryKey: string, entityId: string): Promise => {
const entity = // Load a single of type 'entityTypeName' with a value in its field 'primaryKey' of value 'entityId'.
return entity;
},
//
// Load the set of entities.
//
async loadEntities(entityTypeName: string): Promise {
const entities = // Load all entities of type 'entityTypeName'.
return entities;
},
};
```
Finally create a lazy query resolver with your configuration and data loader:
```javascript
//
// Creates a query resolver for lazy loadeed data.
//
const queryResolver = await createQueryResolver(queryConfig, dataLoader);
```
Now you can make queries against the lazily loaded dataset, for example:
```javascript
const query = {
get: {
species: { // Query for "species" entity.
// No arguments gets all entities.
resolve: {
homeworld: { // Resolves the homeworld of each species as a nested lookup.
},
}
},
},
};
// Invokes MiniQL.
const result = await miniql(query, queryResolver, {});
// Displays the query result.
console.log(JSON.stringify(result, null, 4));
```
Please see [MiniQL](https://github.com/miniql/miniql) for more information on how to make queries.
Don't forget to [star the repo](https://github.com/miniql/miniql) and [follow the developer on Twitter](https://twitter.com/codecapers).