Skip to content

Commit b0657db

Browse files
authored
Merge pull request #21 from syarul/getDie
added caching
2 parents 0dd6d4b + cc03acf commit b0657db

17 files changed

+610
-486
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,6 @@ node_modules
22
.env
33
.npmignore
44
coverage
5-
.npmrc
5+
.npmrc
6+
.tmp
7+
test/data.json

README.md

Lines changed: 42 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -12,46 +12,51 @@ Lightweight driven query language
1212

1313
Here's the first example to get you started. [Try it here](https://codepen.io/syarul/pen/xxmLMVP)—no build step required!
1414

15-
## Comparison to graphQL
15+
## Features
1616

17-
| Feature | GraphQL | reQurse |
18-
| --------------- | --------------------------------------------------------- | ----------------------------------------------------- |
19-
| Query Syntax | ✅ Declarative queries as strings (SDL) | ✅ JSON object-based queries |
20-
| Schema & Typing | ✅ Strongly typed schemas | ❌ No type enforcement (support GraphQL schema) |
21-
| Resolvers | ✅ Defined server-side logic for each field |`methods` functions resolve keys |
22-
| API Federation | ✅ Built-in with tools like Apollo Federation | ✅ Can compose multiple `rq()` into one federated API |
23-
| Use Cases | ✅ Full APIs, strongly validated, efficient data fetching | ✅ Full APIs, Lightweight, data orchestration |
24-
| Runtime | ✅ Client-server over HTTP | ✅ Client-server over HTTP/In-process library calls |
25-
| Complexity | ❌ Requires schema + resolvers + server setup | ✅ Very light; no server setup |
17+
| Feature | reQurse |
18+
| --------------- | ----------------------------------------------------------------- |
19+
| Query Syntax | ✅ JSON object-based queries |
20+
| Schema & Typing | ❌ No type enforcement (support GraphQL schema) |
21+
| Resolvers |`methods` functions resolve keys |
22+
| API Federation | ✅ Can compose multiple instance of `rq()` into one federated API |
23+
| Caching | ✅ Implemented (pass in options) |
24+
| Use Cases | ✅ Full APIs, Lightweight, data orchestration |
25+
| Runtime | ✅ Client-server over HTTP/In-process library calls |
26+
| Complexity | ✅ Very light; no server setup |
2627

27-
As example using the [RandomDie](https://www.graphql-js.org/docs/object-types/) sample
28+
As direct comparison with graphQL using the [RandomDie](https://www.graphql-js.org/docs/object-types/) sample
2829

2930
```js
3031
// graphQL
3132
const RandomDie = new GraphQLObjectType({
3233
name: "RandomDie",
33-
fields: {
34-
numSides: {
35-
type: new GraphQLNonNull(GraphQLInt),
36-
resolve: (die) => die.numSides,
37-
},
38-
rollOnce: {
39-
type: new GraphQLNonNull(GraphQLInt),
40-
resolve: (die) => 1 + Math.floor(Math.random() * die.numSides),
41-
},
42-
roll: {
43-
type: new GraphQLList(GraphQLInt),
44-
args: {
45-
numRolls: { type: new GraphQLNonNull(GraphQLInt) },
34+
fields: () => {
35+
const fields = {
36+
numSides: {
37+
type: new GraphQLNonNull(GraphQLInt),
38+
resolve: (die) => die.numSides,
4639
},
47-
resolve: (die, { numRolls }) => {
48-
const output = [];
49-
for (let i = 0; i < numRolls; i++) {
50-
output.push(1 + Math.floor(Math.random() * die.numSides));
51-
}
52-
return output;
40+
rollOnce: {
41+
type: new GraphQLNonNull(GraphQLInt),
42+
resolve: (die) => 1 + Math.floor(Math.random() * die.numSides),
5343
},
54-
},
44+
roll: {
45+
type: new GraphQLList(GraphQLInt),
46+
args: {
47+
numRolls: { type: new GraphQLNonNull(GraphQLInt) },
48+
},
49+
resolve: (die, { numRolls }, ctx, info) => {
50+
const rollOnceResolver = fields.rollOnce.resolve;
51+
const output = [];
52+
for (let i = 0; i < numRolls; i++) {
53+
output.push(rollOnceResolver(die, {}, ctx, info));
54+
}
55+
return output;
56+
},
57+
},
58+
};
59+
return fields;
5560
},
5661
});
5762

@@ -117,7 +122,7 @@ class RandomDie extends RqExtender {
117122
for (let i = 0; i < numRolls; i++) {
118123
// reuse rollOnce here
119124
// context is using rq context
120-
// {
125+
// {
121126
// query, // combine queryResult
122127
// computes, // computed fields
123128
// }
@@ -184,12 +189,16 @@ A basic usage of reQurse.
184189
```javascript
185190
import rq from "requrse";
186191

187-
rq(query, { methods, config });
192+
rq(query, { methods, config, dataUrl, rootKey, cache, cacheDir });
188193
```
189194

190195
- **query**: _(object)_ **_required_** JSON like query.
191196
- **methods**: _(object)_ **_required_** define methods/computed fields that exist in the query.
192197
- **config**: _(object)_ **_optional_** extend and added parameterize control over methods.
198+
- **dataUrl**: _(string)_ **_optional_** resolve result to data url path.
199+
- **rootKey**: _(string)_ **_optional_** graphQL root key if using graphQL query, default to 'data' if not given.
200+
- **cache**: _(number)_ **_optional_** cache result in second(s).
201+
- **cacheDir**: _(string)_ **_optional_** custom caching directory default is '.tmp'.
193202

194203
```js
195204
await rq(

README_OLD.md

Lines changed: 0 additions & 255 deletions
This file was deleted.

libs/arrayToObject.cjs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ const checkEntry = require("./checkEntry.cjs");
77
* @returns {Boolean}
88
*/
99
const checkUniq = (arr) => {
10-
const check = arr.map((i) => i && i?.[0]).filter((f) => f !== undefined);
10+
const check = arr
11+
.map((/** @type {any[]} */ i) => i && i?.[0])
12+
.filter((/** @type {any} */ f) => f !== undefined);
1113
return check.length === [...new Set(check)].length;
1214
};
1315

@@ -17,7 +19,10 @@ const checkUniq = (arr) => {
1719
* @returns
1820
*/
1921
const reducer = (unique) => {
20-
return (acc, item) => {
22+
return (
23+
/** @type {Record<String, any>} */ acc,
24+
/** @type {any[]} */ item,
25+
) => {
2126
if (item && item[0]) {
2227
if (unique) {
2328
acc[item[0]] = arrayToObject(item[1]);

0 commit comments

Comments
 (0)